Created
August 25, 2013 15:19
-
-
Save xu-cheng/6334393 to your computer and use it in GitHub Desktop.
Get all YouTube videos of a channel.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
# Copyright (c) 2013 by Xu Cheng ([email protected]) | |
# | |
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