|
| 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.request_channel() |
| 42 | + |
| 43 | + def request_channel(self): |
| 44 | + # Display a text box where the user can enter a channel name or |
| 45 | + # channel ID. |
| 46 | + select_channel_page = ''' |
| 47 | + <html> |
| 48 | + <body> |
| 49 | + <p>Which channel's videos do you want to see?</p> |
| 50 | + <form method="post"> |
| 51 | + <p> |
| 52 | + <select name="channel_type"> |
| 53 | + <option value="id">Channel ID</option> |
| 54 | + <option value="name">Channel name</option> |
| 55 | + </select> |
| 56 | + <input name="channel" size="30"> |
| 57 | + </p> |
| 58 | + <p><input type="submit" /></p> |
| 59 | + </form> |
| 60 | + </body> |
| 61 | + </html> |
| 62 | + ''' |
| 63 | + |
| 64 | + # Display the HTML page that shows the form. |
| 65 | + self.response.out.write(select_channel_page) |
| 66 | + |
| 67 | + def post(self): |
| 68 | + # Service for calling the YouTube API |
| 69 | + youtube = build(YOUTUBE_API_SERVICE_NAME, |
| 70 | + YOUTUBE_API_VERSION, |
| 71 | + developerKey=API_KEY) |
| 72 | + |
| 73 | + # Use form inputs to create request params for channel details |
| 74 | + channel_type = self.request.get('channel_type') |
| 75 | + channels_response = None |
| 76 | + if channel_type == 'id': |
| 77 | + channels_response = youtube.channels().list( |
| 78 | + id=self.request.get('channel'), |
| 79 | + part='snippet,contentDetails' |
| 80 | + ).execute() |
| 81 | + else: |
| 82 | + channels_response = youtube.channels().list( |
| 83 | + forUsername=self.request.get('channel'), |
| 84 | + part='snippet,contentDetails' |
| 85 | + ).execute() |
| 86 | + |
| 87 | + channel_name = '' |
| 88 | + videos = [] |
| 89 | + |
| 90 | + for channel in channels_response['items']: |
| 91 | + uploads_list_id = channel['contentDetails']['relatedPlaylists']['uploads'] |
| 92 | + channel_name = channel['snippet']['title'] |
| 93 | + |
| 94 | + next_page_token = '' |
| 95 | + while next_page_token is not None: |
| 96 | + playlistitems_response = youtube.playlistItems().list( |
| 97 | + playlistId=uploads_list_id, |
| 98 | + part='snippet', |
| 99 | + maxResults=50, |
| 100 | + pageToken=next_page_token |
| 101 | + ).execute() |
| 102 | + |
| 103 | + for playlist_item in playlistitems_response['items']: |
| 104 | + videos.append(playlist_item) |
| 105 | + |
| 106 | + next_page_token = playlistitems_response.get('tokenPagination', {}).get( |
| 107 | + 'nextPageToken') |
| 108 | + |
| 109 | + if len(videos) > 100: |
| 110 | + break |
| 111 | + |
| 112 | + template_values = { |
| 113 | + 'channel_name': channel_name, |
| 114 | + 'videos': videos |
| 115 | + } |
| 116 | + |
| 117 | + self.response.headers['Content-type'] = 'text/html' |
| 118 | + template = JINJA_ENVIRONMENT.get_template('index.html') |
| 119 | + self.response.write(template.render(template_values)) |
| 120 | + |
| 121 | +app = webapp2.WSGIApplication([ |
| 122 | + ('/.*', MainHandler), |
| 123 | +], debug=True) |
0 commit comments