-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_youtube.py
46 lines (40 loc) · 1.01 KB
/
api_youtube.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
import os
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from oauth2client.tools import argparser
import pydash as _
def obtain(search_term, category):
DEVELOPER_KEY = os.getenv("YOUTUBE_API_KEY")
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
try:
youtube = build(
YOUTUBE_API_SERVICE_NAME,
YOUTUBE_API_VERSION,
developerKey=DEVELOPER_KEY
)
except HttpError:
return None
try:
search_response = youtube.search().list(
q=search_term,
part="id,snippet",
maxResults=1
).execute()
except HttpError as e:
return None
video_list = search_response.get("items", [])
if not video_list:
return None
return {
"metadata": {
"type": "youtube-video",
"term": search_term,
"category": category
},
"data": {
"name": _.get(video_list, '0.title', None),
"link": "https://www.youtube.com/embed/%s" %
_.get(video_list, '0.id.videoId')
}
}