Compare commits

..

No commits in common. "master" and "master" have entirely different histories.

2 changed files with 37 additions and 38 deletions

View File

@ -1,8 +1,6 @@
> note: merged into https://git.vvvvvvaria.org/varia/bots
# logbot # logbot
A small XMPP bot written in Python (using the slixmpp library) that logs all images and messages with the mentioning of *@bot* to an HTML page, to allow collaborative log writing over time. A small XMPP bot written in Python (using the slixmpp library), that logs all images and messages with the mentioning of *@bot*.
To run it: To run it:
@ -10,7 +8,7 @@ To run it:
Dependencies: Dependencies:
$ sudo pip3 install slixmpp beautifulsoup4 $ sudo pip3 install slixmpp
--- ---

View File

@ -82,8 +82,8 @@ class MUCBot(slixmpp.ClientXMPP):
f.write(u.read()) # write image to file f.write(u.read()) # write image to file
f.close() # close the output file f.close() # close the output file
# Add the image to the log # Add image to log
img = '<img class="image" src="{}">'.format(filename) img = '<img class="image" src="{}">'.format(msg['oob']['url'])
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+')
@ -91,7 +91,7 @@ class MUCBot(slixmpp.ClientXMPP):
f.close() f.close()
# Include messages in the log (only when '@bot' is used in the message) # Include messages in the log (only when '#publish' is used in the message)
if '@bot' in msg['body']: if '@bot' in msg['body']:
# reply from the bot # reply from the bot
@ -99,7 +99,7 @@ class MUCBot(slixmpp.ClientXMPP):
mbody="Noted! And added to the log. Thanks {}!".format(msg['mucnick']), mbody="Noted! And added to the log. Thanks {}!".format(msg['mucnick']),
mtype='groupchat') mtype='groupchat')
# Add the message to the log! # Add message to log
message = '<p class="message">{}</p>'.format(msg['body'].replace('@bot','')) message = '<p class="message">{}</p>'.format(msg['body'].replace('@bot',''))
log = 'log.html' log = 'log.html'
log_path = os.path.join(self.output, log) log_path = os.path.join(self.output, log)
@ -107,55 +107,56 @@ class MUCBot(slixmpp.ClientXMPP):
f.write(message+'\n') f.write(message+'\n')
f.close() f.close()
if '/book' in msg['body']: # Check if this is a book ... if '/book' in msg['body']: # Check if this is a book
self.send_message(mto=self.room, self.send_message(mto=self.room,
mbody="Oh a book, that's cool! Thanks {}!".format(msg['mucnick']), mbody="Oh a book, that's cool! Thanks {}!".format(msg['mucnick']),
mtype='groupchat') mtype='groupchat')
# Start of book feature
from bs4 import BeautifulSoup
import re
book = msg['body'].replace('@bot', '').replace('/book', '') from bs4 import BeautifulSoup
book = re.sub(' +', ' ', book) # remove double spaces import requests
book = book.lstrip().rstrip() # remove spaces at the beginning and at the end import re
book = book.replace(' ', '+').lower() # turn space into + and lowercase
page_link = 'https://www.worldcat.org/search?q={}&qt=results_page'.format(book) 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_response = requests.get(page_link, timeout=5) page_link = 'https://www.worldcat.org/search?q={}&qt=results_page'.format(book)
page_content = BeautifulSoup(page_response.content, "html.parser") page_response = requests.get(page_link, timeout=5)
try: page_content = BeautifulSoup(page_response.content, "html.parser")
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 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
book_found = True response = '<b>BOOK</b>: ' + book_title + ' ' + book_author + ' ' + book_publisher
except IndexError: book_found = True
book_found = False except IndexError:
if book_found: book_found = False
# Add message to log if book_found:
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') # 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()
else: 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='Sorry, no book found!', mtype='groupchat') else:
self.send_message(mto=self.room, mbody='Sorry, no book found!', mtype='groupchat')