13. How to extract information from YouTube in Python

13.1. gdata and the developer key

YouTube belongs to Google, and Google supplies an API for it by way of its general Python package, gdata. Gdata is available for download and installation using pip – just remember to prefix the command below with sudo if you are on a Mac:

$ pip install -U gdata

That was deceptively easy. Gdata has three versions of the YouTube API. Normally we would use the most recent one, but it is still under development and cannot do the one thing that we are most interested in, namely extract the comments from a video feed. Only the first version does that, which makes us uncomfortable. Google could deprecate it (withdraw it from usage) at any moment. Yet at the time of writing of these lines, we have no other alternative.

Like many other sites that share its API with the outside world, Google limits the number of calls that can be made to it in a certain period of time. This quota is raised for those who request a developer key. We have found out the hard way that the task that we want to accomplish hits the quota limit quite quickly, so we will need a key. But before requesting one, we need a place to put it.

Open a new Python file in Spyder, fill in the header information, and save it to YOUR PYTHON FOLDER with a name like “youtube.py”. The first line should have the utf-8 encoding string as usual. Then import the YouTube service from gdata and assign your developer key to it, so that your new script begins as so:

# -*- coding: utf-8 -*-
"""
your header info here
"""
import gdata.youtube.service as gys
service = gys.YouTubeService()
service.developer_key = ' '

To reduce typing further on, we abbreviate “gdata.youtube.service” as “gys” and the general YouTubeService call as “service”. Your developer key will go into the space between the two apostrophes in service.developer_key = ' '.

To get it, point your web browser at <http://code.google.com/apis/youtube/dashboard/gwt/index.html>. You will have to create a Google account there if you do not already have one. Once that is done, you will be redirected to ‘Register A New Product’ on the Google Code site. As the red star indicates, all you need to do is fill in a name. Click ‘save’ and copy the huge alphanumeric string that appears in the ‘Developer Key’ box to your computer’s clipboard and then paste into the space between the two apostrophes in service.developer_key = ' '. Now, every time that service is called, your dveloper key goes with it, so that you are much less likely to bump up against a quota limit.

13.2. Give it a REST?

Table Of Contents

Previous topic

12. How to get text from Twitter feeds

Next topic

14. Glossary

This Page