- Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathmain.py
123 lines (103 loc) · 3.88 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
importos
importurllib
importwebapp2
importjinja2
fromapiclient.discoveryimportbuild
fromoptparseimportOptionParser
importjson
JINJA_ENVIRONMENT=jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'])
REGISTRATION_INSTRUCTIONS="""
You must set up a project and get an API key to run this code. Please see
the instructions for creating a project and a key at <a
href="https://developers.google.com/youtube/registering_an_application"
>https://developers.google.com/youtube/registering_an_application</a>.
<br><br>
Make sure that you have enabled the YouTube Data API (v3) and the Freebase
API for your project."""
# Set API_KEY to the "API key" value from the Google Developers Console:
# https://console.developers.google.com/project/_/apiui/credential
# Please ensure that you have enabled the YouTube Data API and Freebase API
# for your project.
API_KEY="REPLACE_ME"
YOUTUBE_API_SERVICE_NAME="youtube"
YOUTUBE_API_VERSION="v3"
FREEBASE_SEARCH_URL="https://www.googleapis.com/freebase/v1/search?%s"
QUERY_TERM="dog"
classMainHandler(webapp2.RequestHandler):
defget(self):
ifAPI_KEY=='REPLACE_ME':
self.response.write(REGISTRATION_INSTRUCTIONS)
else:
# Present a list of Freebase topic IDs for the query term
self.request_channel()
defrequest_channel(self):
# Display a text box where the user can enter a channel name or
# channel ID.
select_channel_page='''
<html>
<body>
<p>Which channel's videos do you want to see?</p>
<form method="post">
<p>
<select name="channel_type">
<option value="id">Channel ID</option>
<option value="name">Channel name</option>
</select>
<input name="channel" size="30">
</p>
<p><input type="submit" /></p>
</form>
</body>
</html>
'''
# Display the HTML page that shows the form.
self.response.out.write(select_channel_page)
defpost(self):
# Service for calling the YouTube API
youtube=build(YOUTUBE_API_SERVICE_NAME,
YOUTUBE_API_VERSION,
developerKey=API_KEY)
# Use form inputs to create request params for channel details
channel_type=self.request.get('channel_type')
channels_response=None
ifchannel_type=='id':
channels_response=youtube.channels().list(
id=self.request.get('channel'),
part='snippet,contentDetails'
).execute()
else:
channels_response=youtube.channels().list(
forUsername=self.request.get('channel'),
part='snippet,contentDetails'
).execute()
channel_name=''
videos= []
forchannelinchannels_response['items']:
uploads_list_id=channel['contentDetails']['relatedPlaylists']['uploads']
channel_name=channel['snippet']['title']
next_page_token=''
whilenext_page_tokenisnotNone:
playlistitems_response=youtube.playlistItems().list(
playlistId=uploads_list_id,
part='snippet',
maxResults=50,
pageToken=next_page_token
).execute()
forplaylist_iteminplaylistitems_response['items']:
videos.append(playlist_item)
next_page_token=playlistitems_response.get('tokenPagination', {}).get(
'nextPageToken')
iflen(videos) >100:
break
template_values= {
'channel_name': channel_name,
'videos': videos
}
self.response.headers['Content-type'] ='text/html'
template=JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render(template_values))
app=webapp2.WSGIApplication([
('/.*', MainHandler),
], debug=True)