Getting comments from YouTube via Python’s gdata.youtube

Posted on

I would like to download comments from YouTube. This is possible via the gdata.youtube Python module. python-gdata is a Debian/Ubuntu module of GData but may not include the most recent additions, such as the youtube module, so it may be necessary to download the gdata-python-client package with something like:

wget http://gdata-python-client.googlecode.com/files/gdata-2.0.2.tar.gz
tar vfxz gdata-2.0.2.tar.gz
cd gdata-2.0.2
python setup.py install --home=~/python

With some help provided by the python code of Giles Bowkett it is now possible to download some of the comments to a video on YouTube with the following lines of Python code:

import gdata.youtube.service
yts = gdata.youtube.service.YouTubeService()
urlpattern = 'http://gdata.youtube.com/feeds/api/videos/' +
       'JE5kkyucts8/comments?start-index=%d&max-results=25'
index = 1
url = urlpattern % index
comments = []
while url:
  ytfeed = yts.GetYouTubeVideoCommentFeed(uri=url)
  comments.extend([ comment.content.text for comment in ytfeed.entry ])
  url = ytfeed.GetNextLink().href
  print url

It seems only to be possible to download 1000 comments, – see also Stephen Mesa’s comment. So the small script will error after 1000 comments have been downloaded…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s