Skip to content

Commit c57f6fc

Browse files
author
Foxlet
committed
youtube.py - Check if API is off
1 parent b32fc7a commit c57f6fc

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

plugins/youtube.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717
search_api_url = base_url + 'search?part=id&maxResults=1'
1818
playlist_api_url = base_url + 'playlists?part=snippet%2CcontentDetails%2Cstatus'
1919
video_url = "http://youtu.be/%s"
20+
err_noapi = "You did not turn on the Youtube API in the Google Developers Console."
2021

2122

2223
def get_video_description(video_id, key):
2324
json = requests.get(api_url.format(video_id, key)).json()
2425

2526
if json.get('error'):
26-
return
27+
if json['error']['code'] == 403:
28+
return err_noapi
29+
else:
30+
return
2731

2832
data = json['items']
2933
snippet = data[0]['snippet']
@@ -83,8 +87,11 @@ def youtube(text):
8387

8488
json = requests.get(search_api_url, params={"q": text, "key": dev_key}).json()
8589

86-
if 'error' in json:
87-
return 'Error performing search.'
90+
if json.get('error'):
91+
if json['error']['code'] == 403:
92+
return err_noapi
93+
else:
94+
return 'Error performing search.'
8895

8996
if json['pageInfo']['totalResults'] == 0:
9097
return 'No results found.'
@@ -102,8 +109,11 @@ def youtime(text):
102109

103110
json = requests.get(search_api_url, params={"q": text, "key": dev_key}).json()
104111

105-
if 'error' in json:
106-
return 'Error performing search.'
112+
if json.get('error'):
113+
if json['error']['code'] == 403:
114+
return err_noapi
115+
else:
116+
return 'Error performing search.'
107117

108118
if json['pageInfo']['totalResults'] == 0:
109119
return 'No results found.'
@@ -142,8 +152,11 @@ def ytplaylist_url(match):
142152
location = match.group(4).split("=")[-1]
143153
json = requests.get(playlist_api_url, params={"id": location, "key": dev_key}).json()
144154

145-
if 'error' in json:
146-
return 'Error looking up playlist.'
155+
if json.get('error'):
156+
if json['error']['code'] == 403:
157+
return err_noapi
158+
else:
159+
return 'Error looking up playlist.'
147160

148161
data = json['items']
149162
snippet = data[0]['snippet']

0 commit comments

Comments
 (0)