Skip to content

Commit 4506765

Browse files
committed
Merge pull request #69 from FurCode/python3.4
Thanks! Closes #68
2 parents b32fc7a + 6ae05f7 commit 4506765

3 files changed

Lines changed: 30 additions & 11 deletions

File tree

plugins/books.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ def books(text):
2121

2222
json = requests.get(book_search_api, params={"q": text, "key": dev_key}).json()
2323

24-
if 'error' in json:
25-
return 'Error performing search.'
24+
if json.get('error'):
25+
if json['error']['code'] == 403:
26+
return "The Books API is off in the Google Developers Console."
27+
else:
28+
return 'Error performing search.'
2629

2730
if json['totalItems'] == 0:
2831
return 'No results found.'

plugins/google_translate.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ def goog_trans(api_key, text, source, target):
2525
request = requests.get(url, params=params)
2626
parsed = request.json()
2727

28-
if 'error' in parsed:
29-
return "Google API error."
28+
if parsed.get('error'):
29+
if parsed['error']['code'] == 403:
30+
return "The Translate API is off in the Google Developers Console."
31+
else:
32+
return "Google API error."
3033

3134
if not source:
3235
return '(%(detectedSourceLanguage)s) %(translatedText)s' % (parsed['data']['translations'][0])

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 = "The YouTube API is off 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)