|
| 1 | +import os |
| 2 | +import urllib |
| 3 | +import webapp2 |
| 4 | +import jinja2 |
| 5 | + |
| 6 | +from apiclient.discovery import build |
| 7 | +from optparse import OptionParser |
| 8 | + |
| 9 | +import json |
| 10 | + |
| 11 | +JINJA_ENVIRONMENT = jinja2.Environment( |
| 12 | + loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), |
| 13 | + extensions=['jinja2.ext.autoescape']) |
| 14 | + |
| 15 | +REGISTRATION_INSTRUCTIONS = """ |
| 16 | + You must set up a project and get an API key to run this code. Please see |
| 17 | + the instructions for creating a project and a key at <a |
| 18 | + href="https://developers.google.com/youtube/registering_an_application" |
| 19 | + >https://developers.google.com/youtube/registering_an_application</a>. |
| 20 | + <br><br> |
| 21 | + Make sure that you have enabled the YouTube Data API (v3) and the Freebase |
| 22 | + API for your project.""" |
| 23 | + |
| 24 | +# Set API_KEY to the "API key" value from the "Access" tab of the |
| 25 | +# Google APIs Console http://code.google.com/apis/console#access |
| 26 | +# Please ensure that you have enabled the YouTube Data API and Freebase API |
| 27 | +# for your project. |
| 28 | +API_KEY = "REPLACE_ME" |
| 29 | +YOUTUBE_API_SERVICE_NAME = "youtube" |
| 30 | +YOUTUBE_API_VERSION = "v3" |
| 31 | +FREEBASE_SEARCH_URL = "https://www.googleapis.com/freebase/v1/search?%s" |
| 32 | +QUERY_TERM = "dog" |
| 33 | + |
| 34 | +class MainHandler(webapp2.RequestHandler): |
| 35 | + |
| 36 | + def get(self): |
| 37 | + if API_KEY == 'REPLACE_ME': |
| 38 | + self.response.write(REGISTRATION_INSTRUCTIONS) |
| 39 | + else: |
| 40 | + # Present a list of Freebase topic IDs for the query term |
| 41 | + self.list_topics(QUERY_TERM) |
| 42 | + |
| 43 | + def list_topics(self, QUERY_TERM): |
| 44 | + # Retrieve a list of Freebase topics associated with the query term |
| 45 | + freebase_params = dict(query=QUERY_TERM, key=API_KEY) |
| 46 | + freebase_url = FREEBASE_SEARCH_URL % urllib.urlencode(freebase_params) |
| 47 | + freebase_response = json.loads(urllib.urlopen(freebase_url).read()) |
| 48 | + |
| 49 | + if len(freebase_response["result"]) == 0: |
| 50 | + exit("No matching terms were found in Freebase.") |
| 51 | + |
| 52 | + # Create a page that shows a select box listing the topics. |
| 53 | + # When the user selects a topic and submits the form, the |
| 54 | + # 'post' method below will handle the form submission and |
| 55 | + # retrieve videos for the selected topic. |
| 56 | + select_topic_page = (''' |
| 57 | + <html> |
| 58 | + <body> |
| 59 | + <p>The following topics were found:</p> |
| 60 | + <form method="post"> |
| 61 | + <select name="topic"> |
| 62 | + ''') |
| 63 | + for result in freebase_response["result"]: |
| 64 | + select_topic_page += ('<option value="' + result["mid"] + '">' + |
| 65 | + result.get("name", "Unknown") + '</option>') |
| 66 | + |
| 67 | + select_topic_page += ''' |
| 68 | + </select> |
| 69 | + <p><input type="submit" /></p> |
| 70 | + </form> |
| 71 | + </body> |
| 72 | + </html> |
| 73 | + ''' |
| 74 | + |
| 75 | + # Display the HTML page listing the topic choices. |
| 76 | + self.response.out.write(select_topic_page) |
| 77 | + |
| 78 | + def post(self): |
| 79 | + topic_id = self.request.get('topic') |
| 80 | + |
| 81 | + # Service for calling the YouTube API |
| 82 | + youtube = build(YOUTUBE_API_SERVICE_NAME, |
| 83 | + YOUTUBE_API_VERSION, |
| 84 | + developerKey=API_KEY) |
| 85 | + |
| 86 | + # Execute the search request using default query term and retrieved topic. |
| 87 | + search_response = youtube.search().list( |
| 88 | + part = 'id,snippet', |
| 89 | + type = 'video', |
| 90 | + topicId = topic_id |
| 91 | + ).execute() |
| 92 | + |
| 93 | + videos = [] |
| 94 | + |
| 95 | + for search_result in search_response.get("items", []): |
| 96 | + videos.append(search_result) |
| 97 | + |
| 98 | + template_values = { |
| 99 | + 'videos': videos |
| 100 | + } |
| 101 | + |
| 102 | + self.response.headers['Content-type'] = 'text/html' |
| 103 | + template = JINJA_ENVIRONMENT.get_template('index.html') |
| 104 | + self.response.write(template.render(template_values)) |
| 105 | + |
| 106 | +app = webapp2.WSGIApplication([ |
| 107 | + ('/.*', MainHandler), |
| 108 | +], debug=True) |
0 commit comments