Skip to content

Commit d15c5fb

Browse files
Youtube API
1 parent 2b17fae commit d15c5fb

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

.env_sample

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ YELP_TOKEN_SECRET=
88
TWITTER_CONSUMER_KEY=
99
TWITTER_CONSUMER_SECRET=
1010
TWITTER_TOKEN=
11-
TWITTER_TOKEN_SECRET=
11+
TWITTER_TOKEN_SECRET=
12+
YOUTUBE_API_KEY=

api_youtube.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import os
2+
3+
from googleapiclient.discovery import build
4+
from googleapiclient.errors import HttpError
5+
from oauth2client.tools import argparser
6+
import pydash as _
7+
8+
9+
DEVELOPER_KEY = os.getenv('YOUTUBE_API_KEY')
10+
YOUTUBE_API_SERVICE_NAME = "youtube"
11+
YOUTUBE_API_VERSION = "v3"
12+
13+
def obtain(search_term, category):
14+
15+
try:
16+
youtube = build(
17+
YOUTUBE_API_SERVICE_NAME,
18+
YOUTUBE_API_VERSION,
19+
developerKey=DEVELOPER_KEY
20+
)
21+
except HttpError:
22+
return None
23+
24+
try:
25+
search_response = youtube.search().list(
26+
q=search_term,
27+
part="id,snippet",
28+
maxResults=1
29+
).execute()
30+
except HttpError:
31+
return None
32+
33+
video_list = search_response.get("items", [])
34+
if not video_list:
35+
return None
36+
37+
return {
38+
"metadata": {
39+
"type": "youtube-video",
40+
"term": search_term,
41+
"category": category
42+
},
43+
"data": {
44+
"name": _.get(video_list, '0.title', None),
45+
"link": "https://www.youtube.com/embed/%s" %
46+
_.get(video_list, '0.id.videoId')
47+
}
48+
}

requirements.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
1+
apiclient==1.0.3
12
beautifulsoup4==4.5.1
23
cffi==1.7.0
34
click==6.6
45
cryptography==1.4
56
enum34==1.1.6
67
future==0.15.2
8+
google-api-python-client==1.5.1
79
httplib2==0.9.2
810
idna==2.1
911
ipaddress==1.0.16
1012
ndg-httpsclient==0.4.2
1113
nltk==3.2.1
1214
oauth2==1.9.0.post1
15+
oauth2client==3.0.0
1316
oauthlib==1.1.2
1417
ordereddict==1.1
1518
pyasn1==0.1.9
19+
pyasn1-modules==0.0.8
1620
pycparser==2.14
1721
pydash==3.4.3
1822
pyOpenSSL==16.0.0
1923
python-dotenv==0.5.1
2024
python-twitter==3.1
2125
requests==2.10.0
2226
requests-oauthlib==0.6.2
27+
rsa==3.4.2
28+
simplejson==3.8.2
2329
six==1.10.0
30+
uritemplate==0.6
31+
urllib3==1.16
2432
wikipedia==1.4.0
2533
yelp==1.0.2

0 commit comments

Comments
 (0)