use of com.google.api.services.youtube.model.Playlist in project opencast by opencast.
the class YouTubeAPIVersion3ServiceImpl method createPlaylist.
@Override
public Playlist createPlaylist(final String title, final String description, final String... tags) throws IOException {
final PlaylistSnippet playlistSnippet = new PlaylistSnippet();
playlistSnippet.setTitle(title);
playlistSnippet.setDescription(description);
if (tags.length > 0) {
playlistSnippet.setTags(Collections.list(tags));
}
// Playlists are always public. The videos therein might be private.
final PlaylistStatus playlistStatus = new PlaylistStatus();
playlistStatus.setPrivacyStatus("public");
// Create playlist with metadata and status.
final Playlist youTubePlaylist = new Playlist();
youTubePlaylist.setSnippet(playlistSnippet);
youTubePlaylist.setStatus(playlistStatus);
// The first argument tells the API what to return when a successful insert has been executed.
final YouTube.Playlists.Insert command = youTube.playlists().insert("snippet,status", youTubePlaylist);
return execute(command);
}
use of com.google.api.services.youtube.model.Playlist in project opencast by opencast.
the class YouTubeV3PublicationServiceImpl method publish.
/**
* Publishes the element to the publication channel and returns a reference to the published version of the element.
*
* @param job
* the associated job
* @param mediaPackage
* the mediapackage
* @param elementId
* the mediapackage element id to publish
* @return the published element
* @throws PublicationException
* if publication fails
*/
private Publication publish(final Job job, final MediaPackage mediaPackage, final String elementId) throws PublicationException {
if (mediaPackage == null) {
throw new IllegalArgumentException("Mediapackage must be specified");
} else if (elementId == null) {
throw new IllegalArgumentException("Mediapackage ID must be specified");
}
final MediaPackageElement element = mediaPackage.getElementById(elementId);
if (element == null) {
throw new IllegalArgumentException("Mediapackage element must be specified");
}
if (element.getIdentifier() == null) {
throw new IllegalArgumentException("Mediapackage element must have an identifier");
}
if (element.getMimeType().toString().matches("text/xml")) {
throw new IllegalArgumentException("Mediapackage element cannot be XML");
}
try {
// create context strategy for publication
final YouTubePublicationAdapter c = new YouTubePublicationAdapter(mediaPackage, workspace);
final File file = workspace.get(element.getURI());
final String episodeName = c.getEpisodeName();
final UploadProgressListener operationProgressListener = new UploadProgressListener(mediaPackage, file);
final String privacyStatus = makeVideosPrivate ? "private" : "public";
final VideoUpload videoUpload = new VideoUpload(truncateTitleToMaxFieldLength(episodeName, false), c.getEpisodeDescription(), privacyStatus, file, operationProgressListener, tags);
final Video video = youTubeService.addVideoToMyChannel(videoUpload);
final int timeoutMinutes = 60;
final long startUploadMilliseconds = new Date().getTime();
while (!operationProgressListener.isComplete()) {
Thread.sleep(POLL_MILLISECONDS);
final long howLongWaitingMinutes = (new Date().getTime() - startUploadMilliseconds) / 60000;
if (howLongWaitingMinutes > timeoutMinutes) {
throw new PublicationException("Upload to YouTube exceeded " + timeoutMinutes + " minutes for episode " + episodeName);
}
}
String playlistName = StringUtils.trimToNull(truncateTitleToMaxFieldLength(mediaPackage.getSeriesTitle(), true));
playlistName = (playlistName == null) ? this.defaultPlaylist : playlistName;
final Playlist playlist;
final Playlist existingPlaylist = youTubeService.getMyPlaylistByTitle(playlistName);
if (existingPlaylist == null) {
playlist = youTubeService.createPlaylist(playlistName, c.getContextDescription(), mediaPackage.getSeries());
} else {
playlist = existingPlaylist;
}
youTubeService.addPlaylistItem(playlist.getId(), video.getId());
// Create new publication element
final URL url = new URL("http://www.youtube.com/watch?v=" + video.getId());
return PublicationImpl.publication(UUID.randomUUID().toString(), CHANNEL_NAME, url.toURI(), MimeTypes.parseMimeType(MIME_TYPE));
} catch (Exception e) {
logger.error("failed publishing to Youtube", e);
logger.warn("Error publishing {}, {}", element, e.getMessage());
if (e instanceof PublicationException) {
throw (PublicationException) e;
} else {
throw new PublicationException("YouTube publish failed on job: " + ToStringBuilder.reflectionToString(job, ToStringStyle.MULTI_LINE_STYLE), e);
}
}
}
use of com.google.api.services.youtube.model.Playlist in project opencast by opencast.
the class YouTubeV3PublicationServiceImplTest method testPublishNewPlaylist.
@Test
public void testPublishNewPlaylist() throws Exception {
final File baseDir = new File(this.getClass().getResource("/mediapackage").toURI());
final String xml = FileUtils.readFileToString(new File(baseDir, "manifest.xml"));
final MediaPackage mediaPackage = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().loadFromXml(xml);
//
expect(youTubeService.getMyPlaylistByTitle(mediaPackage.getTitle())).andReturn(null).once();
expect(youTubeService.createPlaylist(mediaPackage.getSeriesTitle(), null, mediaPackage.getSeries())).andReturn(new Playlist()).once();
expect(youTubeService.addVideoToMyChannel(anyObject(VideoUpload.class))).andReturn(new Video()).once();
expect(youTubeService.addPlaylistItem(anyObject(String.class), anyObject(String.class))).andReturn(new PlaylistItem()).once();
expect(registry.createJob(anyObject(String.class), anyObject(String.class), anyObject(List.class), anyObject(Float.class))).andReturn(new JobImpl()).once();
replay(youTubeService, orgDirectory, security, registry, userDirectoryService, workspace);
service.updated(getServiceProperties());
service.publish(mediaPackage, mediaPackage.getTracks()[0]);
}
use of com.google.api.services.youtube.model.Playlist in project api-samples by youtube.
the class PlaylistLocalizations method getPlaylistLocalization.
/**
* Returns localized metadata for a playlist in a selected language.
* If the localized text is not available in the requested language,
* this method will return text in the default language.
*
* @param playlistId The id parameter specifies the playlist ID for the resource
* that is being updated.
* @param language The language of the localized metadata
* @throws IOException
*/
private static void getPlaylistLocalization(String playlistId, String language) throws IOException {
// Call the YouTube Data API's playlists.list method to retrieve playlists.
PlaylistListResponse playlistListResponse = youtube.playlists().list("snippet").setId(playlistId).set("hl", language).execute();
// Since the API request specified a unique playlist ID, the API
// response should return exactly one playlist. If the response does
// not contain a playlist, then the specified playlist ID was not found.
List<Playlist> playlistList = playlistListResponse.getItems();
if (playlistList.isEmpty()) {
System.out.println("Can't find a playlist with ID: " + playlistId);
return;
}
Playlist playlist = playlistList.get(0);
// Print information from the API response.
System.out.println("\n================== Playlist ==================\n");
System.out.println(" - ID: " + playlist.getId());
System.out.println(" - Title(" + language + "): " + playlist.getLocalizations().get(language).getTitle());
System.out.println(" - Description(" + language + "): " + playlist.getLocalizations().get(language).getDescription());
System.out.println("\n-------------------------------------------------------------\n");
}
use of com.google.api.services.youtube.model.Playlist in project api-samples by youtube.
the class PlaylistLocalizations method listPlaylistLocalizations.
/**
* Returns a list of localized metadata for a playlist.
*
* @param playlistId The id parameter specifies the playlist ID for the resource
* that is being updated.
* @throws IOException
*/
private static void listPlaylistLocalizations(String playlistId) throws IOException {
// Call the YouTube Data API's playlists.list method to retrieve playlists.
PlaylistListResponse playlistListResponse = youtube.playlists().list("snippet,localizations").setId(playlistId).execute();
// Since the API request specified a unique playlist ID, the API
// response should return exactly one playlist. If the response does
// not contain a playlist, then the specified playlist ID was not found.
List<Playlist> playlistList = playlistListResponse.getItems();
if (playlistList.isEmpty()) {
System.out.println("Can't find a playlist with ID: " + playlistId);
return;
}
Playlist playlist = playlistList.get(0);
Map<String, PlaylistLocalization> localizations = playlist.getLocalizations();
// Print information from the API response.
System.out.println("\n================== Playlist ==================\n");
System.out.println(" - ID: " + playlist.getId());
for (String language : localizations.keySet()) {
System.out.println(" - Title(" + language + "): " + localizations.get(language).getTitle());
System.out.println(" - Description(" + language + "): " + localizations.get(language).getDescription());
}
System.out.println("\n-------------------------------------------------------------\n");
}
Aggregations