Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add book module #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion bot.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
41 changes: 41 additions & 0 deletions modules/book.py
Original file line number Diff line number Diff line change
@@ -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