Compare commits

...

15 Commits

Author SHA1 Message Date
3ffccff799
Woops, fixed URL 2020-02-06 15:32:56 +01:00
521cf77c30
Point to bots repository 2020-02-06 15:32:56 +01:00
mb
6f0dd64061 Update 'README.md' 2019-09-11 22:34:07 +02:00
mb
708283a4cc Update 'README.md' 2019-09-11 22:29:20 +02:00
d6bf65db6f testing the hook :) 2019-07-17 19:01:25 +02:00
ef3a6f2f7c testing the hook :) 2019-07-17 19:00:28 +02:00
e481171c1b testing the hook :) 2019-07-17 18:57:12 +02:00
62ddb363da testing the hook :) 2019-07-17 18:56:29 +02:00
9bc42a9010 testing the hook :) 2019-07-17 18:55:45 +02:00
fe8fddd6fb changing the path of the links to relative links 2019-07-17 18:52:55 +02:00
06046e121b testing the git hook again 2019-07-17 18:47:19 +02:00
a0d7665677 testing the git hook 2019-07-17 18:41:51 +02:00
c68376e373 we fixed the logbot book error in Bucharest 2019-07-17 18:36:06 +02:00
mb
dedfdd114a Update 'README.md' 2019-05-03 13:33:07 +02:00
mb
8e743178f1 Merge branch 'master' of s/logbot into master
super! nice feature
2019-05-03 13:25:03 +02:00
2 changed files with 38 additions and 37 deletions

View File

@ -1,6 +1,8 @@
> note: merged into https://git.vvvvvvaria.org/varia/bots
# logbot
A small XMPP bot written in Python (using the slixmpp library), that logs all images and messages with the mentioning of *@bot*.
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.
To run it:
@ -8,7 +10,7 @@ To run it:
Dependencies:
$ sudo pip3 install slixmpp
$ sudo pip3 install slixmpp beautifulsoup4
---

View File

@ -82,8 +82,8 @@ class MUCBot(slixmpp.ClientXMPP):
f.write(u.read()) # write image to file
f.close() # close the output file
# Add image to log
img = '<img class="image" src="{}">'.format(msg['oob']['url'])
# Add the image to the log
img = '<img class="image" src="{}">'.format(filename)
log = 'log.html'
log_path = os.path.join(self.output, log)
f = open(log_path, 'a+')
@ -91,7 +91,7 @@ class MUCBot(slixmpp.ClientXMPP):
f.close()
# Include messages in the log (only when '#publish' is used in the message)
# Include messages in the log (only when '@bot' is used in the message)
if '@bot' in msg['body']:
# reply from the bot
@ -99,7 +99,7 @@ class MUCBot(slixmpp.ClientXMPP):
mbody="Noted! And added to the log. Thanks {}!".format(msg['mucnick']),
mtype='groupchat')
# Add message to log
# Add the message to the log!
message = '<p class="message">{}</p>'.format(msg['body'].replace('@bot',''))
log = 'log.html'
log_path = os.path.join(self.output, log)
@ -107,56 +107,55 @@ class MUCBot(slixmpp.ClientXMPP):
f.write(message+'\n')
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,
mbody="Oh a book, that's cool! Thanks {}!".format(msg['mucnick']),
mtype='groupchat')
# Start of book feature
from bs4 import BeautifulSoup
import re
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
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_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:
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
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
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
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()
# 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')
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')