aboutsummaryrefslogtreecommitdiff
path: root/modules/isbn.py
blob: 678705200307ee42929917d5bc2b94da5f445442 (about) (plain) (blame)
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
import json
import Utils

URL_GOOGLEBOOKS = "https://www.googleapis.com/books/v1/volumes"

class Module(object):
    _name = "ISBN"
    def __init__(self, bot):
        self.bot = bot
        bot.events.on("received").on("command").on("isbn").hook(
            self.isbn, help="Get book information from a provided ISBN",
            min_args=1)

    def isbn(self, event):
        isbn = event["args_split"][0]
        if len(isbn) == 10:
            isbn = "978%s" % isbn
        isbn = isbn.replace("-", "")
        page = Utils.get_url(URL_GOOGLEBOOKS, get_params={
            "q": "isbn:%s" % isbn, "country": "us"}, json=True)
        if page:
            if page["totalItems"] > 0:
                book = page["items"][0]["volumeInfo"]
                title = book["title"]
                sub_title = book["subtitle"]
                authors = ", ".join(book["authors"])
                date = book["publishedDate"]
                rating = book["averageRating"]
                #language = book["language"]
                event["stdout"].write("%s - %s (%s), %s (%s/5.0)" % (
                    title, authors, date, sub_title, rating))
            else:
                event["stderr"].write("Unable to find book")
        else:
            event["stderr"].write("Failed to load results")