# # # python yt_ranks.py < {a txt file with a list of terms} > {a tab delimited file of results} # # You'll must install the simpljson module to use it # import urllib import urllib2 import simplejson import sys # Read the terms we want to convert into URL from info redirected from the command line terms = sys.stdin.readlines() MAX_RESULTS = 10 print "%s\t%s\t%s\t%s\t%s\t%s" % ('term', 'title', 'seconds', 'view_count', 'rating', 'published') found = {} for term in terms: # Define the query to pass to Google Search API topic = urllib.urlencode({'q' : term.rstrip("\n")}) max_results = urllib.urlencode({'max-results' : MAX_RESULTS }) url = "http://gdata.youtube.com/feeds/api/videos?%s&%s&orderby=viewCount&alt=json" % (topic,max_results) # Fetch the results and convert to JSON format search_results = urllib2.urlopen(url) json = simplejson.loads(search_results.read()) # Process the results by pulling the first record, which has the best match for r in json['feed']['entry']: try: title = r['title']['$t'].encode('ascii', 'replace') rating= r['gd$rating']['average'] view_count = r['yt$statistics']['viewCount'] duration = r['media$group']['yt$duration']['seconds'] published = r['published']['$t'].encode('ascii', 'replace') id = r['$id']['$t'].encode('ascii', 'replace') # Print the results to stdout. Use redirect to capture the output print "%s\t%s\t%s\t%s\t%s\t%s" % (term.rstrip("\n"),title, duration, view_count, rating, published) except: pass