YouTube's API is a REST service that uses Google's Gdata format to enable you to perform operations such as searching for and retrieving information about videos as well as uploading videos to YouTube.
Unlike other services, such as those from Live.com or Amazon.com, you do not need to sign up for a developer account or an API key to read data from YouTube's API. Figure 4-4 shows the results from retrieving the top rated videos of all time on YouTube using Firefox.
Note
For full documentation on what services are available from YouTube, go to http://code.google.com/apis/youtube/developers_guide_protocol.html.
If you look closely at the URL in Figure 4-4's image, you'll notice that the Top Rated video feed is a "Standard Feed." YouTube defines a number of standard feeds, including:
You can further restrict the results from standard feeds by appending a time
parameter to the query string. For example, this parameter will only retrieve the most viewed videos this week:
http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed?time=this_week
The time
parameter can have the following values: today
, this_week
, this_month
, and all_time
. The default value is all_time
if no parameter is provided.
By default, YouTube makes user data (such as your favorite videos or video subscriptions) public and available via its API. To get a user's favorites or subscriptions, just replace the username in the following URLs with a valid YouTube username. Unlike the generic YouTube feeds, you cannot add a time parameter to filter user-specific feeds by a time interval.
Favorites by user: http://gdata.youtube.com/feeds/api/users/username/favorites
Subscriptions by user: http://gdata.youtube.com/feeds/api/users/username/subscriptions
YouTube also provides a search API that can be called by appending vq=searchterm
. For example, the search URL here will search YouTube for all Microsoft videos:
http://gdata.youtube.com/feeds/api/videos?vq=Microsoft
InnerTube's YouTube wrapper provides a simple implementation for its search, but the YouTube API adds additional search features, such as paging or ordering by appending query string values. The following is a sampling of query string parameters:
-
orderby
Specify how you want videos sorted—say, by relevance, rating, published date, or number of views. By default, this is set to relevance.
-
max-results
Specify the maximum number of results. By default, this is set to 25.
-
start-index
Enable paging through data by setting the
start-index
to the point from which you want the result set to start. If you set thestart-index
to 100, the API would retrieve the 100–125th search results. By default, this is set to 0.
Note
For a full list of parameters and options, see the YouTube Query Parameter Definitions at http://code.google.com/apis/youtube/reference.html#Query_parameter_definitions.
YouTube uses a number of other, well-defined URLs that we'll need to know for the purposes of building our application. Here are quick explanations for each:
- Watch
This is the link you commonly see when linking to a video: http://www.youtube.com/watch?v=VideoID
- Embed
This is the link used for embedding a video on another site, such as MySpace: http://www.youtube.com/v/VideoID
- Thumbnail
This is the link to the largest image thumbnail available for a video (425×344): http://img.youtube.com/vi/VideoID/0.jpg
- Download
This is the (hidden) link that we'll use to download a video: http://www.youtube.com/get_video?video_id=VideoID&t=sessiontoken
Note
The Download link to download a video will work only if you append a valid, time-based session token. This is done to try to prevent end users from downloading YouTube videos directly. We'll show how you can work around this restriction in the Downloading Videos from YouTube section, later in this chapter.
Get Coding4Fun now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.