com.google.api.services.youtube.model.Thumbnail Java Examples

The following examples show how to use com.google.api.services.youtube.model.Thumbnail. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: YouTubePlaylist.java    From SkyTube with GNU General Public License v3.0 6 votes vote down vote up
public YouTubePlaylist(Playlist playlist, YouTubeChannel channel) {
	id = playlist.getId();
	this.channel = channel;

	if(playlist.getSnippet() != null) {
		title = playlist.getSnippet().getTitle();
		description = playlist.getSnippet().getDescription();
		DateTime dt = playlist.getSnippet().getPublishedAt();
           publishTimestamp = dt != null ? dt.getValue() : null;

		if(playlist.getSnippet().getThumbnails() != null) {
			Thumbnail thumbnail = playlist.getSnippet().getThumbnails().getHigh();
			if(thumbnail != null)
				thumbnailUrl = thumbnail.getUrl();
		}
	}

	if(playlist.getContentDetails() != null) {
		videoCount = playlist.getContentDetails().getItemCount();
	}
}
 
Example #2
Source File: YoutubeVideoDeserializer.java    From streams with Apache License 2.0 6 votes vote down vote up
/**
 * Given the raw JsonNode, construct a video snippet object.
 * @param node JsonNode
 * @return VideoSnippet
 */
private VideoSnippet buildSnippet(JsonNode node) {
  VideoSnippet snippet = new VideoSnippet();
  JsonNode snippetNode = node.get("snippet");

  snippet.setChannelId(snippetNode.get("channelId").asText());
  snippet.setChannelTitle(snippetNode.get("channelTitle").asText());
  snippet.setDescription(snippetNode.get("description").asText());
  snippet.setTitle(snippetNode.get("title").asText());
  snippet.setPublishedAt(new DateTime(snippetNode.get("publishedAt").get("value").asLong()));

  ThumbnailDetails thumbnailDetails = new ThumbnailDetails();
  for (JsonNode t : snippetNode.get("thumbnails")) {
    Thumbnail thumbnail = new Thumbnail();

    thumbnail.setHeight(t.get("height").asLong());
    thumbnail.setUrl(t.get("url").asText());
    thumbnail.setWidth(t.get("width").asLong());

    thumbnailDetails.setDefault(thumbnail);
  }

  snippet.setThumbnails(thumbnailDetails);

  return snippet;
}
 
Example #3
Source File: YoutubeActivityUtil.java    From streams with Apache License 2.0 6 votes vote down vote up
/**
 * Given a video object, create the appropriate activity object with a valid image
 * (thumbnail) and video URL.
 * @param video Video
 * @return Activity Object with Video URL and a thumbnail image
 */
private static ActivityObject buildActivityObject(Video video) {
  ActivityObject activityObject = new ActivityObject();

  ThumbnailDetails thumbnailDetails = video.getSnippet().getThumbnails();
  Thumbnail thumbnail = thumbnailDetails.getDefault();

  if (thumbnail != null) {
    Image image = new Image();
    image.setUrl(thumbnail.getUrl());
    image.setHeight(thumbnail.getHeight());
    image.setWidth(thumbnail.getWidth());

    activityObject.setImage(image);
  }

  activityObject.setUrl("https://www.youtube.com/watch?v=" + video.getId());
  activityObject.setObjectType("video");

  return activityObject;
}
 
Example #4
Source File: YouTubeAPI.java    From UTubeTV with The Unlicense 6 votes vote down vote up
private String thumbnailURL(ThumbnailDetails details) {
  String result = null;

  if (details != null) {
    Thumbnail thumbnail = details.getMaxres();

    // is this necessary?  not sure
    if (thumbnail == null) {
      thumbnail = details.getHigh();
    }

    if (thumbnail == null) {
      thumbnail = details.getDefault();
    }

    if (thumbnail != null) {
      result = thumbnail.getUrl();
    }
  }

  return result;
}
 
Example #5
Source File: YoutubeChannelDeserializer.java    From streams with Apache License 2.0 5 votes vote down vote up
protected ThumbnailDetails setThumbnails(JsonNode node) {
  ThumbnailDetails details = new ThumbnailDetails();
  if (node == null) {
    return details;
  }
  details.setDefault(new Thumbnail().setUrl(node.get("default").get("url").asText()));
  details.setHigh(new Thumbnail().setUrl(node.get("high").get("url").asText()));
  details.setMedium(new Thumbnail().setUrl(node.get("medium").get("url").asText()));
  return details;
}
 
Example #6
Source File: YoutubeConnector.java    From search-youtube with Apache License 2.0 4 votes vote down vote up
private static List<VideoItem> setItemsList(Iterator<SearchResult> iteratorSearchResults) {

	        //temporary list to store the raw data from the returned results
			List<VideoItem> tempSetItems = new ArrayList<>();

	        //if no result then printing appropriate output
			if (!iteratorSearchResults.hasNext()) {
				System.out.println(" There aren't any results for your query.");
			}

	        //iterating through all search results
	        //hasNext() method returns true until it has no elements left to iterate
			while (iteratorSearchResults.hasNext()) {

	            //next() method returns single instance of current video item
	            //and returns next item everytime it is called
	            //SearchResult is Youtube's custom result type which can be used to retrieve data of each video item
				SearchResult singleVideo = iteratorSearchResults.next();

				//getId() method returns the resource ID of one video in the result obtained
				ResourceId rId = singleVideo.getId();

	            // Confirm that the result represents a video. Otherwise, the
	            // item will not contain a video ID.
	            //getKind() returns which type of resource it is which can be video, playlist or channel
				if (rId.getKind().equals("youtube#video")) {

	                //object of VideoItem class that can be added to array list
					VideoItem item = new VideoItem();

	                //getting High quality thumbnail object
	                //URL of thumbnail is in the heirarchy snippet/thumbnails/high/url
					Thumbnail thumbnail = singleVideo.getSnippet().getThumbnails().getHigh();

	                //retrieving title,description,thumbnail url, id from the heirarchy of each resource
	                //Video ID - id/videoId
	                //Title - snippet/title
	                //Description - snippet/description
	                //Thumbnail - snippet/thumbnails/high/url
					item.setId(singleVideo.getId().getVideoId());
					item.setTitle(singleVideo.getSnippet().getTitle());
					item.setDescription(singleVideo.getSnippet().getDescription());
					item.setThumbnailURL(thumbnail.getUrl());

	                //adding one Video item to temporary array list
					tempSetItems.add(item);

	                //for debug purpose printing one by one details of each Video that was found
					System.out.println(" Video Id" + rId.getVideoId());
					System.out.println(" Title: " + singleVideo.getSnippet().getTitle());
					System.out.println(" Thumbnail: " + thumbnail.getUrl());
					System.out.println(" Description: "+ singleVideo.getSnippet().getDescription());
					System.out.println("\n-------------------------------------------------------------\n");
				}
			}
			return tempSetItems;
		}
 
Example #7
Source File: YouTubeVideo.java    From SkyTube with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Constructor.
 */
public YouTubeVideo(Video video) {
	this.id = video.getId();

	VideoSnippet snippet = video.getSnippet();
	if (snippet != null) {
		this.title = snippet.getTitle();

		this.channel = new YouTubeChannel(snippet.getChannelId(), snippet.getChannelTitle());
		setPublishDate(snippet.getPublishedAt());

		if (snippet.getThumbnails() != null) {
			Thumbnail thumbnail = snippet.getThumbnails().getHigh();
			if (thumbnail != null) {
				this.thumbnailUrl = thumbnail.getUrl();
			}

			thumbnail = snippet.getThumbnails().getMaxres();
			if (thumbnail != null) {
				this.thumbnailMaxResUrl = thumbnail.getUrl();
			}
		}

		this.language = snippet.getDefaultAudioLanguage() != null ? snippet.getDefaultAudioLanguage()
				: (snippet.getDefaultLanguage());

		this.description = snippet.getDescription();
	}

	if (video.getContentDetails() != null) {
		setDuration(video.getContentDetails().getDuration());
		setIsLiveStream();
		setDurationInSeconds(video.getContentDetails().getDuration());
	}

	VideoStatistics statistics = video.getStatistics();
	if (statistics != null) {
		setLikeDislikeCount(statistics.getLikeCount() != null ? statistics.getLikeCount().longValue() : null, statistics.getDislikeCount() != null ? statistics.getDislikeCount().longValue() : null);

		setViewCount(statistics.getViewCount());
	}
}
 
Example #8
Source File: YoutubeFaviconFetcher.java    From commafeed with Apache License 2.0 4 votes vote down vote up
@Override
public Favicon fetch(Feed feed) {
	String url = feed.getUrl();

	if (!url.toLowerCase().contains("youtube.com/feeds/videos.xml")) {
		return null;
	}

	String googleAuthKey = config.getApplicationSettings().getGoogleAuthKey();
	if (googleAuthKey == null) {
		log.debug("no google auth key configured");
		return null;
	}

	byte[] bytes = null;
	String contentType = null;
	try {
		List<NameValuePair> params = URLEncodedUtils.parse(url.substring(url.indexOf("?") + 1), StandardCharsets.UTF_8);
		Optional<NameValuePair> userId = params.stream().filter(nvp -> nvp.getName().equalsIgnoreCase("user")).findFirst();
		Optional<NameValuePair> channelId = params.stream().filter(nvp -> nvp.getName().equalsIgnoreCase("channel_id")).findFirst();
		if (!userId.isPresent() && !channelId.isPresent()) {
			return null;
		}

		YouTube youtube = new YouTube.Builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(),
				new HttpRequestInitializer() {
					@Override
					public void initialize(HttpRequest request) throws IOException {
					}
				}).setApplicationName("CommaFeed").build();

		YouTube.Channels.List list = youtube.channels().list("snippet");
		list.setKey(googleAuthKey);
		if (userId.isPresent()) {
			list.setForUsername(userId.get().getValue());
		} else {
			list.setId(channelId.get().getValue());
		}

		log.debug("contacting youtube api");
		ChannelListResponse response = list.execute();
		if (response.getItems().isEmpty()) {
			log.debug("youtube api returned no items");
			return null;
		}

		Channel channel = response.getItems().get(0);
		Thumbnail thumbnail = channel.getSnippet().getThumbnails().getDefault();

		log.debug("fetching favicon");
		HttpResult iconResult = getter.getBinary(thumbnail.getUrl(), TIMEOUT);
		bytes = iconResult.getContent();
		contentType = iconResult.getContentType();
	} catch (Exception e) {
		log.debug("Failed to retrieve YouTube icon", e);
	}

	if (!isValidIconResponse(bytes, contentType)) {
		return null;
	}
	return new Favicon(bytes, contentType);
}