Browse Source

find book on worldcat

master
lowrussia 6 years ago
parent
commit
48ddeba22c
  1. 57
      logbot.py

57
logbot.py

@ -55,7 +55,7 @@ class MUCBot(slixmpp.ClientXMPP):
def muc_message(self, msg):
# Some inspection commands
# print('Message: {}'.format(msg))
print('Message: {}'.format(msg))
# Always check that a message is not the bot itself, otherwise you will create an infinite loop responding to your own messages.
if msg['mucnick'] != self.nick:
@ -107,6 +107,60 @@ class MUCBot(slixmpp.ClientXMPP):
f.write(message+'\n')
f.close()
if '/book' in msg['body']: # Check if this is a book
self.send_message(mto=self.room,
mbody="Oh a book, that's cool! Thanks {}!".format(msg['mucnick']),
mtype='groupchat')
from bs4 import BeautifulSoup
import requests
import re
book = msg['body'].replace('@bot', '').replace('/book', '')
book = re.sub(' +', ' ', book) # remove double spaces
book = book.lstrip().rstrip() # remove spaces at the beginning and at the end
book = book.replace(' ', '+').lower() # turn space into + and lowercase
page_link = 'https://www.worldcat.org/search?q={}&qt=results_page'.format(book)
page_response = requests.get(page_link, timeout=5)
page_content = BeautifulSoup(page_response.content, "html.parser")
try:
book_title = page_content.findAll("div", {"class": "name"})[0].text
book_author = page_content.findAll("div", {"class": "author"})[0].text
book_publisher = page_content.findAll("div", {"class": "publisher"})[0].text
response = '<b>BOOK</b>: ' + book_title + ' ' + book_author + ' ' + book_publisher
book_found = True
except IndexError:
book_found = False
if book_found:
# Add message to log
message = '<b>BOOK</b>: ' + book_title + ' ' + book_author + ' ' + book_publisher
log = 'log.html'
log_path = os.path.join(self.output, log)
f = open(log_path, 'a+')
f.write(message+'\n')
f.close()
self.send_message(mto=self.room, mbody='Hope this was the book you were looking for: ' + book_title + ' ' + book_author + ' ' + book_publisher, mtype='groupchat')
else:
self.send_message(mto=self.room, mbody='Sorry, no book found!', mtype='groupchat')
if __name__ == '__main__':
# Setup the command line arguments.
parser = ArgumentParser()
@ -163,4 +217,3 @@ if __name__ == '__main__':
# Connect to the XMPP server and start processing XMPP stanzas.
xmpp.connect()
xmpp.process()

Loading…
Cancel
Save