-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyoutube_api_init.py
42 lines (31 loc) · 1.05 KB
/
youtube_api_init.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
from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser
# Add your developer key here
DEVELOPER_KEY = "XXXXXX"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
def youtube_search(q=None, type="video", channelId=None, max_results=50,order="viewCount", token=None, location=None, location_radius=None):
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
developerKey=DEVELOPER_KEY)
search_response = youtube.search().list(
q=q,
type=type,
pageToken=token,
order = order,
part="id,snippet",
channelId=channelId,
maxResults=max_results,
location=location,
locationRadius=location_radius
).execute()
videos = []
for search_result in search_response.get("items", []):
if search_result["id"]["kind"] == "youtube#video":
videos.append(search_result)
try:
nexttok = search_response["nextPageToken"]
return(nexttok, videos)
except Exception as e:
nexttok = "last_page"
return(nexttok, videos)