Accessing the YouTube REST API


In the last blog post (“YouTube Metadata meet Web Log Files”) I wrote for the series “Will they blend?” on the KNIME web site, I blended metadata from YouTube videos and data from a web log file. In this blog post here I will focus on how to get video metadata from the YouTube REST API.

YouTube offers an access REST API service as part of the Google API. As for all Google APIs, you do not need a full account if all you want to do is search; a key API is enough. You can request your own key API directly on the Google API Console. Remember to enable the key for the YouTube API services. The available services and the procedure to get a key API are described in these 2 introductory links:
https://developers.google.com/apis-explorer/?hl=en_US#p/youtube/v3/
https://developers.google.com/youtube/v3/getting-started#before-you-start

If you want to get a list of videos on YouTube tagged with a given keyword, you should use the REST API service named “search”:
https://www.googleapis.com/youtube/v3/search?q=<keyword>&part=id&maxResults=50&videoDuration=any&key=<your-key-API>
where:

  • q=<keyword> identifies the keyword to search;
  • maxResults=50 allows for a maximum of 50 videos in the response list;
  • key=<your-key-API> requires the key API you should have gotten when signing up in the Google Console.

If you want to get the stats for a YouTube video, as identified by its videoID, you should use the REST API service named “videos”:
https://www.googleapis.com/youtube/v3/videos?id=<videoID>&part=snippet,statistics,contentDetails&key=<your-key-API>
where:

  • id=<videoID> identifies the video we are interested in;
  • part= defines the kind of response. Here we set details, such as duration, permission flags, and statistics, such as total number of views, likes, and other video related actions;
  • key=<your-key-API> requires the key API you should have gotten when signing up in the Google Console.

If you want to retrieve the comments to a specific video, as identified by its videoID, you should use the REST API service named “commentThreads”:
https://www.googleapis.com/youtube/v3/commentThreads?videoId=<videoID>&part=snippet,id&key=<your-key-API>
where:

  • id=<videoID> identifies the video we are interested in;
  • part= defines the kind of response. Here we set snippet to get the comment texts and the posting date. and id to get the author ID;
  • key=<your-key-API> requires the key API you should have gotten when signing up in the Google Console.

A variation of the same metanode, with name starting with “YouTube API”, is used to invoke all three YouTube REST API services. All metanodes have the same structure:

  1. First the REST query is built as a String, as described above, through a String Manipulation node;
  2. then the query is sent to the REST server through a GET Request node;
  3. the list of videos or comments or stats is extracted from the JSON formatted response with a JSON Path node;
  4. the same list is ungrouped to place each JSON-structured item in a table row;
  5. finally the interesting values are extracted from each JSON-structured item.

Figure 1. Sub-workflow in metanode to access YouTube REST API.

Running the search for “Star+Wars” and sorting the results for the top most viewed 5 YouTube videos, we get the following results (Fig. 2and 3).

Note. If multiple words are inserted for the keyword search, you need to join them with a “+” sign and not a space.

The full workflow is shown in figure 4 and can be downloaded from the KNIME EXAMPLES Server under:
01_Data_Access/05_REST_Web_Services/03_Access_YouTube_REST_API.
Of course, you will need to get your own API key.

Figure 2. Top 5 most viewed YouTube videos related to keyword “Star+Wars”.

Picture


Figure 3. Comment cloud for the YouTube videos retrieved from the YouTube REST API service as related to keyword “Star+Wars”.

Picture


Figure 4. Full Workflow to access YouTube API services. Search keyword(s) is set in the first wrapped node.

Picture
This entry was posted in Data Mining. Bookmark the permalink.