Oct. 22, 2012

Using the YouTube API in Python

Overview

In this post we will be looking on how to use the YouTube API in Python. 
This program will show how we can use the API to retrieve feeds from YouTube. 

This particular script will show the most popular videos on YouTube right now. 

Standard Feeds

Some of the most popular YouTube feeds are:

most_recent
most_viewed
top_rated
most_discussed
top_favorites
most_linked
recently_featured
most_responded

Getting Started

To start getting the data that we want from the YouTube feeds, we begin by
importing the necessary modules.
import requests
import json
We make it a bit "prettier" by printing out what the program does. Then we get
the feed by using the requests module. I used to use the urllib2 module to open
the URL, but ever since Kenneth Reitz gave us the Requests module, I'm letting
that module do most of my HTTP tasks. 

urllib2 vs requests 
r = requests.get("http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?v=2&alt=jsonc")
r.text
After we have got the feed and saved it to the variable "r", we convert it into a Python dictionary. 
 data = json.loads(r.text)
Now, we have a python dictionary and we can use a for loop to go through the data
for item in data['data']['items']:
This can sometimes be the tricky part and you need to look carefully how the
structure is presented to you. 

Using a JSON editor will make it easier.

Using the YouTube API to get data

This script will show the most popular videos on YouTube.
# Import the modules
import requests
import json

# Make it a bit prettier..
print "-" * 30
print "This will show the Most Popular Videos on YouTube"
print "-" * 30

# Get the feed
r = requests.get("http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?v=2&alt=jsonc")
r.text

# Convert it to a Python dictionary
data = json.loads(r.text)

# Loop through the result. 
for item in data['data']['items']:

    print "Video Title: %s" % (item['title'])

    print "Video Category: %s" % (item['category'])

    print "Video ID: %s" % (item['id'])

    print "Video Rating: %f" % (item['rating'])

    print "Embed URL: %s" % (item['player']['default'])

    print

Using string formatting makes it a lot easier to view the code.
# Sample Output

------------------------------
This will show the Most Popular Videos on YouTube
------------------------------

Video Title: PSY - GANGNAM STYLE (?????) M/V
Video Category: Music
Video ID: 9bZkp7q19f0
Video Rating: 4.614460
Embed URL: http://www.youtube.com/watch?v=9bZkp7q19f0&feature=youtube_gdata_player

Video Title: PSY - GENTLEMAN M/V
Video Category: Music
Video ID: ASO_zypdnsQ
Video Rating: 4.372500
Embed URL: http://www.youtube.com/watch?v=ASO_zypdnsQ&feature=youtube_gdata_player

Video Title: MACKLEMORE & RYAN LEWIS - THRIFT SHOP FEAT. WANZ (OFFICIAL VIDEO)
Video Category: Music
Video ID: QK8mJJJvaes
Video Rating: 4.857624
Embed URL: http://www.youtube.com/watch?v=QK8mJJJvaes&feature=youtube_gdata_player

How to use other YouTube feeds

To use another YouTube standard feed, simple replace the feed in the URL:
http://gdata.youtube.com/feeds/api/standardfeeds/most_responded?v=2&alt=jsonc

You may have to modify the script to get the expected result.

Recommended Python Training – DataCamp

For Python training, our top recommendation is DataCamp.

Datacamp provides online interactive courses that combine interactive coding challenges with videos from top instructors in the field.

Datacamp has beginner to advanced Python training that programmers of all levels benefit from.

 



Read more about:
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, PythonForBeginners.com only recommend products or services that we try personally and believe will add value to our readers.