As part of my project requirement, I have to fetch and parse videos from YouTube in a Flash website. The video player itself should be simple enough since we’ve got YouTube player (https://developers.google.com/youtube/flash_api_reference#Retrieving_video_information), but what about information such as view count, rating, etc.? The YouTube player certainly doesn’t display those statistics out of the box for you, but luckily we’ve got another great tool from Google Developers: the YouTube API.
This is a robust system and can take many hours of typing (and reading too), so I think it’s best to break it apart into smaller sections. And today I’d love to talk more about “Retrieving view count for a single video”.
First thing’s first: here is your friend: https://gdata.youtube.com/feeds/api/videos/videoid?v=2 (replace ‘videoid’ with the actual YouTube Video ID of your target video)
The returned data is of type XML, and it has so much information it could give you a headache at first glance. So to save us from the time spent on looking into those XML contents, here I’ll list a few most-important fields that we as developers are often interested in:
* note that the below elements are direct children of the root node
- 1/ : this returns favoriteCount, viewCount as attributes
- 2/ : this returns numDislikes, numLikes as attributes
- 3/ : this returns average, max, min, numRaters as attributes
- 4/ : this is a large block so I suggest you see it for yourself, but here are some notable fields inside:
- 4/1/ : this returns seconds as an attribute
- 4/2/ : this returns the video title (input by the uploader) as a text node
- 4/3/ : there are usually more than 1 of this element, each one has information for a thumbnail of a specific size, you can use ‘width’, ‘height’, and ‘time’ to search for the thumbnail you are interested in
- 4/4/ : this returns the video description (input by the uploader) as a text node
Other information such as share settings, respond videos, etc. is also stored inside the node, so.. have fun parsing the data!
And that’s it! Hope it helps 🙂