|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +from apiclient.discovery import build |
| 4 | +from apiclient.errors import HttpError |
| 5 | +from oauth2client.tools import argparser |
| 6 | + |
| 7 | +import json |
| 8 | +import urllib |
| 9 | + |
| 10 | + |
| 11 | +# Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps |
| 12 | +# tab of |
| 13 | +# https://cloud.google.com/console |
| 14 | +# Please ensure that you have enabled the YouTube Data API for your project. |
| 15 | +DEVELOPER_KEY = "REPLACE_ME" |
| 16 | +YOUTUBE_API_SERVICE_NAME = "youtube" |
| 17 | +YOUTUBE_API_VERSION = "v3" |
| 18 | +FREEBASE_SEARCH_URL = "https://www.googleapis.com/freebase/v1/search?%s" |
| 19 | + |
| 20 | +def get_topic_id(options): |
| 21 | + # Retrieve a list of Freebase topics associated with the provided query term. |
| 22 | + freebase_params = dict(query=options.query, key=DEVELOPER_KEY) |
| 23 | + freebase_url = FREEBASE_SEARCH_URL % urllib.urlencode(freebase_params) |
| 24 | + freebase_response = json.loads(urllib.urlopen(freebase_url).read()) |
| 25 | + |
| 26 | + if len(freebase_response["result"]) == 0: |
| 27 | + exit("No matching terms were found in Freebase.") |
| 28 | + |
| 29 | + # Display the list of matching Freebase topics. |
| 30 | + mids = [] |
| 31 | + index = 1 |
| 32 | + print "The following topics were found:" |
| 33 | + for result in freebase_response["result"]: |
| 34 | + mids.append(result["mid"]) |
| 35 | + print " %2d. %s (%s)" % (index, result.get("name", "Unknown"), |
| 36 | + result.get("notable", {}).get("name", "Unknown")) |
| 37 | + index += 1 |
| 38 | + |
| 39 | + # Display a prompt for the user to select a topic and return the topic ID |
| 40 | + # of the selected topic. |
| 41 | + mid = None |
| 42 | + while mid is None: |
| 43 | + index = raw_input("Enter a topic number to find related YouTube %ss: " % |
| 44 | + options.type) |
| 45 | + try: |
| 46 | + mid = mids[int(index) - 1] |
| 47 | + except ValueError: |
| 48 | + pass |
| 49 | + return mid |
| 50 | + |
| 51 | + |
| 52 | +def youtube_search(mid, options): |
| 53 | + youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, |
| 54 | + developerKey=DEVELOPER_KEY) |
| 55 | + |
| 56 | + # Call the search.list method to retrieve results associated with the |
| 57 | + # specified Freebase topic. |
| 58 | + search_response = youtube.search().list( |
| 59 | + topicId=mid, |
| 60 | + type=options.type, |
| 61 | + part="id,snippet", |
| 62 | + maxResults=options.max_results |
| 63 | + ).execute() |
| 64 | + |
| 65 | + # Print the title and ID of each matching resource. |
| 66 | + for search_result in search_response.get("items", []): |
| 67 | + if search_result["id"]["kind"] == "youtube#video": |
| 68 | + print "%s (%s)" % (search_result["snippet"]["title"], |
| 69 | + search_result["id"]["videoId"]) |
| 70 | + elif search_result["id"]["kind"] == "youtube#channel": |
| 71 | + print "%s (%s)" % (search_result["snippet"]["title"], |
| 72 | + search_result["id"]["channelId"]) |
| 73 | + elif search_result["id"]["kind"] == "youtube#playlist": |
| 74 | + print "%s (%s)" % (search_result["snippet"]["title"], |
| 75 | + search_result["id"]["playlistId"]) |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + argparser.add_argument("--query", help="Freebase search term", default="Google") |
| 80 | + argparser.add_argument("--max-results", help="Max YouTube results", |
| 81 | + default=25) |
| 82 | + argparser.add_argument("--type", |
| 83 | + help="YouTube result type: video, playlist, or channel", default="channel") |
| 84 | + args = argparser.parse_args() |
| 85 | + |
| 86 | + mid = get_topic_id(args) |
| 87 | + try: |
| 88 | + youtube_search(mid, args) |
| 89 | + except HttpError, e: |
| 90 | + print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content) |
0 commit comments