we fixed the logbot book error in Bucharest

This commit is contained in:
manetta 2019-07-17 18:36:06 +02:00
parent dedfdd114a
commit c68376e373

View File

@ -114,49 +114,49 @@ class MUCBot(slixmpp.ClientXMPP):
mtype='groupchat') mtype='groupchat')
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import requests import requests
import re import re
book = msg['body'].replace('@bot', '').replace('/book', '') book = msg['body'].replace('@bot', '').replace('/book', '')
book = re.sub(' +', ' ', book) # remove double spaces book = re.sub(' +', ' ', book) # remove double spaces
book = book.lstrip().rstrip() # remove spaces at the beginning and at the end book = book.lstrip().rstrip() # remove spaces at the beginning and at the end
book = book.replace(' ', '+').lower() # turn space into + and lowercase book = book.replace(' ', '+').lower() # turn space into + and lowercase
page_link = 'https://www.worldcat.org/search?q={}&qt=results_page'.format(book) page_link = 'https://www.worldcat.org/search?q={}&qt=results_page'.format(book)
page_response = requests.get(page_link, timeout=5) page_response = requests.get(page_link, timeout=5)
page_content = BeautifulSoup(page_response.content, "html.parser") page_content = BeautifulSoup(page_response.content, "html.parser")
try: try:
book_title = page_content.findAll("div", {"class": "name"})[0].text book_title = page_content.findAll("div", {"class": "name"})[0].text
book_author = page_content.findAll("div", {"class": "author"})[0].text book_author = page_content.findAll("div", {"class": "author"})[0].text
book_publisher = page_content.findAll("div", {"class": "publisher"})[0].text book_publisher = page_content.findAll("div", {"class": "publisher"})[0].text
response = '<b>BOOK</b>: ' + book_title + ' ' + book_author + ' ' + book_publisher response = '<b>BOOK</b>: ' + book_title + ' ' + book_author + ' ' + book_publisher
book_found = True book_found = True
except IndexError: except IndexError:
book_found = False book_found = False
if book_found: if book_found:
# Add message to log # Add message to log
message = '<b>BOOK</b>: ' + book_title + ' ' + book_author + ' ' + book_publisher message = '<b>BOOK</b>: ' + book_title + ' ' + book_author + ' ' + book_publisher
log = 'log.html' log = 'log.html'
log_path = os.path.join(self.output, log) log_path = os.path.join(self.output, log)
f = open(log_path, 'a+') f = open(log_path, 'a+')
f.write(message+'\n') f.write(message+'\n')
f.close() 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') 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: else:
self.send_message(mto=self.room, mbody='Sorry, no book found!', mtype='groupchat') self.send_message(mto=self.room, mbody='Sorry, no book found!', mtype='groupchat')