From 56bbf75c04c8bf18211f22b8807197d4eeb29bcb Mon Sep 17 00:00:00 2001 From: csdauth <37444011+csdauth@users.noreply.github.com> Date: Sat, 29 Jun 2019 12:22:59 +0300 Subject: [PATCH 1/2] Add book module --- modules/book.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 modules/book.py diff --git a/modules/book.py b/modules/book.py new file mode 100644 index 0000000..c3d3b71 --- /dev/null +++ b/modules/book.py @@ -0,0 +1,41 @@ +import os +from xml.etree import ElementTree +import config +import requests +import requests_cache +from templates.text import TextTemplate +from html2text import html2text + +import config + +GOODREADS_ACCESS_TOKEN = os.environ.get('GOODREADS_ACCESS_TOKEN', config.GOODREADS_ACCESS_TOKEN) + + +def process(message): + output = {} + try: + with requests_cache.enabled('book_cache', backend='sqlite', expire_after=86400): + response = requests.get('https://www.goodreads.com/book/title.xml?key=' + + GOODREADS_ACCESS_TOKEN + '&title=' + message) + data = ElementTree.fromstring(response.content) + book_node = data.find('book') + author = book_node.find('authors').find('author').find('name').text + title = book_node.find('title').text + description = html2text(book_node.find('description').text) + average_rating = book_node.find('average_rating').text + link = book_node.find('link').text + goodreads_attribution = '- Powered by Goodreads' + + output['output'] = TextTemplate('Title: ' + title + '\nAuthor: ' + author + '\nDescription: ' + description + + '\nAverage Rating: ' + average_rating + ' / 5' + '\n' + + goodreads_attribution).get_message() + output['success'] = True + except Except: + error_message = 'I couldn\'t find any book matching your query.' + error_message += '\nPlease ask me something else, like:' + error_message += '\n - book timeline' + error_message += '\n - harry potter book plot' + error_message += '\n - little women book rating' + output['error_msg'] = TextTemplate(error_message).get_message() + output['success'] = False + return output From 6130a92f99e6c45dac886227d82b7815479a9a2e Mon Sep 17 00:00:00 2001 From: csdauth <37444011+csdauth@users.noreply.github.com> Date: Sat, 29 Jun 2019 12:24:57 +0300 Subject: [PATCH 2/2] Add code to run boo module --- bot.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/bot.py b/bot.py index 24a2601..35d1732 100644 --- a/bot.py +++ b/bot.py @@ -1,7 +1,7 @@ import settings import os from modules import news, image - +from modules import book import discord from discord.ext import commands from modules import xkcd @@ -56,4 +56,17 @@ async def search_image(ctx, search_arg): print(e) await ctx.send("Sorry, something went wrong.") + +@bot.command(pass_context=True, name='book') +async def get_book(ctx, *, message): + try: + output = book.process(message) + r = str(output['output']).replace('{', '').replace('}', '') + r = r.replace('\'text\':', '').replace('\'', '') + r = r.replace('\\n', '\n').replace('"', '') + await ctx.send(r) + except Exception as e: + print(e) + await ctx.send("Sorry, something went wrong.") + bot.run(TOKEN)