From 48ddeba22cc2458cf848186572932faa4e37c068 Mon Sep 17 00:00:00 2001 From: lowrussia Date: Thu, 2 May 2019 23:13:28 +0200 Subject: [PATCH 1/2] find book on worldcat --- logbot.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 7 deletions(-) diff --git a/logbot.py b/logbot.py index dbe4dd0..c8bb62f 100644 --- a/logbot.py +++ b/logbot.py @@ -14,7 +14,7 @@ import ssl, os, requests, urllib class MUCBot(slixmpp.ClientXMPP): """ - A simple Slixmpp bot that will save images + A simple Slixmpp bot that will save images and messages that are marked with @bot to a folder. """ @@ -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: @@ -72,13 +72,13 @@ class MUCBot(slixmpp.ClientXMPP): self.send_message(mto=self.room, mbody="Super, our log is growing. Your image is added!", mtype='groupchat') - + # Save the image to the output folder url = msg['oob']['url'] # grep the url in the message filename = os.path.basename(url) # grep the filename in the url output_path = os.path.join(self.output, filename) u = urllib.request.urlopen(url) # read the image data - f = open(output_path, 'wb') # open the output file + f = open(output_path, 'wb') # open the output file f.write(u.read()) # write image to file f.close() # close the output file @@ -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 = 'BOOK: ' + book_title + ' ' + book_author + ' ' + book_publisher + + book_found = True + + except IndexError: + + book_found = False + + if book_found: + + # Add message to log + message = 'BOOK: ' + 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() @@ -130,8 +184,8 @@ if __name__ == '__main__': help="MUC nickname") # output folder for images - parser.add_argument("-o", "--output", dest="output", - help="output folder, this is where the files are stored", + parser.add_argument("-o", "--output", dest="output", + help="output folder, this is where the files are stored", type=str) args = parser.parse_args() @@ -163,4 +217,3 @@ if __name__ == '__main__': # Connect to the XMPP server and start processing XMPP stanzas. xmpp.connect() xmpp.process() - From 4d7d3cf17c0f7edc256c8857ddb05d0ebe459a21 Mon Sep 17 00:00:00 2001 From: lowrussia Date: Thu, 2 May 2019 23:15:02 +0200 Subject: [PATCH 2/2] find book on worldcat --- logbot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logbot.py b/logbot.py index c8bb62f..104f28b 100644 --- a/logbot.py +++ b/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: