Skip to content

Instantly share code, notes, and snippets.

@xu-cheng
Created August 25, 2013 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xu-cheng/6334393 to your computer and use it in GitHub Desktop.
Save xu-cheng/6334393 to your computer and use it in GitHub Desktop.
Get all YouTube videos of a channel.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2013 by Xu Cheng (xucheng@me.com)
#
import urllib2
import json
def set_proxy(proxy=None):
if proxy is None:
proxy = {}
proxy_handler = urllib2.ProxyHandler(proxy)
opener = urllib2.build_opener(proxy_handler)
urllib2.install_opener(opener)
def list_videos(channel):
url = 'http://gdata.youtube.com/feeds/api/users/%s/uploads?start-index=%%d&max-results=50&alt=json' % channel
videos = []
index = 1
while True:
url_ = url % index
try:
resp_ = urllib2.urlopen(url_)
resp_data = json.load(resp_)
resp_.close()
except Exception as e:
print '!!ERROR', e
break
entries = resp_data['feed']['entry']
for video in entries:
videos.append(video)
if len(entries) < 50:
break
index += 50
return videos
# ---- #
# Test #
# ---- #
if __name__ == '__main__':
set_proxy({'http': '127.0.0.1:8087'})
videos = list_videos('CinemaSins')
for video in videos:
print '-' * 50
print video['title']['$t'] # title
print video['link'][0]['href'] # url
print '-' * 50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment