2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
|
|
|
|
2016-04-03 12:20:05 +00:00
|
|
|
import json, re
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
URL_GOOGLEBOOKS = "https://www.googleapis.com/books/v1/volumes"
|
2016-04-03 12:20:05 +00:00
|
|
|
URL_BOOKINFO = "https://books.google.co.uk/books?id=%s"
|
|
|
|
REGEX_BOOKID = re.compile("id=([\w\-]+)")
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2016-03-29 11:56:58 +00:00
|
|
|
_name = "ISBN"
|
|
|
|
|
2016-04-03 12:20:05 +00:00
|
|
|
def get_book(self, query, event):
|
2018-12-11 22:26:38 +00:00
|
|
|
page = utils.http.request(URL_GOOGLEBOOKS, get_params={
|
2016-04-03 12:20:05 +00:00
|
|
|
"q": query, "country": "us"}, json=True)
|
2016-03-29 11:56:58 +00:00
|
|
|
if page:
|
2018-12-11 22:26:38 +00:00
|
|
|
if page.data["totalItems"] > 0:
|
|
|
|
book = page.data["items"][0]["volumeInfo"]
|
2016-03-29 11:56:58 +00:00
|
|
|
title = book["title"]
|
2016-04-03 12:20:05 +00:00
|
|
|
sub_title = (", %s" % book.get("subtitle")
|
|
|
|
) if book.get("subtitle") else ""
|
2018-07-22 19:53:44 +00:00
|
|
|
|
|
|
|
authors = ", ".join(book.get("authors", []))
|
|
|
|
authors = " - %s" % authors if authors else ""
|
|
|
|
|
|
|
|
date = book.get("publishedDate", "")
|
|
|
|
date = " (%s)" % date if date else ""
|
|
|
|
|
|
|
|
rating = book.get("averageRating", -1)
|
|
|
|
rating = " (%s/5.0)" % rating if not rating == -1 else ""
|
|
|
|
|
2016-04-03 12:20:05 +00:00
|
|
|
id = re.search(REGEX_BOOKID, book["infoLink"]).group(1)
|
2018-07-22 19:53:44 +00:00
|
|
|
info_link = " %s" % (URL_BOOKINFO % id)
|
|
|
|
event["stdout"].write("%s%s%s%s%s%s" % (
|
2016-04-03 12:20:05 +00:00
|
|
|
title, authors, date, sub_title, info_link, rating))
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("Unable to find book")
|
|
|
|
else:
|
2018-10-20 19:51:29 +00:00
|
|
|
raise utils.EventsResultsError()
|
2016-04-03 12:20:05 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.isbn", min_args=1)
|
2016-04-03 12:20:05 +00:00
|
|
|
def isbn(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Get book information from a provided ISBN
|
|
|
|
:usage: <isbn>
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2016-04-03 12:20:05 +00:00
|
|
|
isbn = event["args_split"][0]
|
|
|
|
if len(isbn) == 10:
|
|
|
|
isbn = "978%s" % isbn
|
|
|
|
isbn = isbn.replace("-", "")
|
|
|
|
self.get_book("isbn:%s" % isbn, event)
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.book", min_args=1)
|
2016-04-03 12:20:05 +00:00
|
|
|
def book(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Get book information from a provided title
|
|
|
|
:usage: <book title>
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2016-04-03 12:20:05 +00:00
|
|
|
self.get_book(event["args"], event)
|