|
1 | 1 | import requests |
2 | | -import json |
3 | 2 |
|
4 | 3 | from cloudbot import hook |
5 | | -from cloudbot.util import formatting |
| 4 | +from cloudbot.util import formatting, web |
6 | 5 |
|
7 | 6 | base_url = 'https://www.googleapis.com/books/v1/' |
8 | 7 | book_search_api = base_url + 'volumes?' |
9 | 8 |
|
10 | 9 |
|
11 | | -# shrt by petermanser - https://github.com/petermanser/shrt/blob/master/shrt.py |
12 | | -def shrt(url): |
13 | | - r = requests.post('https://www.googleapis.com/urlshortener/v1/url', |
14 | | - data=json.dumps({"longUrl": url}), |
15 | | - headers={'content-type': 'application/json'}) |
16 | | - |
17 | | - content = json.loads(r.content.decode("UTF-8")) |
18 | | - if r.status_code == 200: |
19 | | - return content['id'] |
20 | | - else: |
21 | | - return "%s: %s" % (content['code'], content['message']) |
22 | | - |
23 | | - |
24 | 10 | @hook.on_start() |
25 | 11 | def load_key(bot): |
26 | 12 | global dev_key |
27 | | - dev_key = bot.config.get("api_keys", {}).get("google_dev_key") |
| 13 | + dev_key = bot.config.get("api_keys", {}).get("google_dev_key", None) |
28 | 14 |
|
29 | 15 |
|
30 | 16 | @hook.command("books", "gbooks") |
31 | 17 | def books(text): |
32 | 18 | """books <query> -- Searches Google Books for <query>.""" |
| 19 | + if not dev_key: |
| 20 | + return "No Google API key." |
| 21 | + |
33 | 22 | json = requests.get(book_search_api, params={"q": text, "key": dev_key}).json() |
34 | 23 |
|
35 | 24 | if 'error' in json: |
@@ -58,6 +47,6 @@ def books(text): |
58 | 47 | except KeyError: |
59 | 48 | pages = '' |
60 | 49 |
|
61 | | - link = shrt(book['infoLink']) |
| 50 | + link = web.shorten(book['infoLink'], service="goo.gl") |
62 | 51 |
|
63 | 52 | return "\x02{}\x02 by \x02{}\x02 ({}){} - {} - {}".format(title, author, year, pages, description, link) |
0 commit comments