From 662eb5645d90f05f6de5f311ae8f41a4cb95dda8 Mon Sep 17 00:00:00 2001 From: manetta Date: Sun, 10 Jan 2021 17:10:38 +0100 Subject: [PATCH] updating readme, bot and template files --- RECbot/README.md | 21 ++- RECbot/RECbot.py | 219 +++++++++++++++++--------------- RECbot/templates/index.html | 14 +- RECbot/templates/stylesheet.css | 3 +- 4 files changed, 144 insertions(+), 113 deletions(-) diff --git a/RECbot/README.md b/RECbot/README.md index d2d4003..2d1df6f 100644 --- a/RECbot/README.md +++ b/RECbot/README.md @@ -16,13 +16,13 @@ The bot is used in group chats, where it includes all images that are send to th * check if `RECbot` is one of the participants in the groupchat! * send an image to the groupchat **OR** use one of the `__ACTION WORDS__` below -* the bot replies and thanks you kindly +* the bot confirms your contribution and writes the message to a file * check the output of RECbot (locally or online, for example: ) -RECbot works with `__ACTION WORDS__` and unique `:HANDLES`. +RECbot works with `__ACTION WORDS__` and unique `` codes. * `__ADD__` RECbot entries with `__ADD__ `, for example: `__ADD__ Logging as a form of stretching time.` or `__ADD__ https://nicelink.org` -* `__DELETE__` RECbot entries with `__DELETE__ :HANDLE`, for example: `__DELETE__ :~+*/+-` (\*spark) +* `__DELETE__` RECbot entries with `__DELETE__ `, for example: `__DELETE__ ~+*/+-` (\*spark) * `__BOOK__` (\*sparks) ## Install RECbot @@ -76,9 +76,22 @@ These modes can be changed at any moment. [\*] These are standalone scripts. They can be used on any set of files in a folder and generate HTML pages with customizable styling. +``` + RECbot + + │ --distribusi > distribusi.py + │ [output folder] --log > log.py [output_folder/index.html] + │ (saved as files) --stream > stream.py (saved as index.html + stylesheet.css) + │ --xxx > xxx.py + │ + └── stores text/media + files in output folder + (local/server) +``` + ------------ -How can `__ACTION WORDS__` become `__MAGIC WORDS__` ??? +How can `__ACTION WORDS__` become magical `__MAGIC WORDS__` ??? ------------ diff --git a/RECbot/RECbot.py b/RECbot/RECbot.py index 9dbf8fa..9815b50 100644 --- a/RECbot/RECbot.py +++ b/RECbot/RECbot.py @@ -52,7 +52,7 @@ def write_to_log(self, entry): # check if file exists, if not: write it! if not os.path.isfile(log_path): - html_template = open('templates/log.html', 'r').read() + html_template = open('templates/index.html', 'r').read() css_template = open('templates/stylesheet.css', 'r').read() with open(log_path, 'w') as l: l.write(html_template) @@ -71,7 +71,6 @@ def write_to_log(self, entry): { handle } { entry } Added on { now } - Tags: ''' print(f'Post: { post }') with open(log_path, 'a+') as l: @@ -81,34 +80,37 @@ def write_to_log(self, entry): h.write(handle) print('added to the .handles file!') -def find_in_soup(self, handle, tag): - print('--------ADD TAG ---------') - print(f'handle: { handle }') - log = 'index.html' - log_path = os.path.join(self.output, log) - html = open(log_path, 'r').read() - soup = BeautifulSoup(html, 'html.parser') - # print(soup.prettify()) - post = soup.find(id=handle) - # print(f'posts: { posts }') - # for post in posts: - print(f'post: { post }') - if post: - # tagcontainer = post.findChildren(id="tagcontainer", recursive=True)[0] - # print(f'tagcontainer: { tagcontainer }') - # print(f'tagcontainer.contents: { tagcontainer.contents }') - # tagcontainer.contents.append(f'{ tag }') - # print(f'tagcontainer.contents: { tagcontainer.contents }') +# *spark +# add annotations + +# def find_in_soup(self, handle, annotation): +# print('--------ADD ANNOTATION ---------') +# print(f'handle: { handle }') +# log = 'index.html' +# log_path = os.path.join(self.output, log) +# html = open(log_path, 'r').read() +# soup = BeautifulSoup(html, 'html.parser') +# # print(soup.prettify()) +# post = soup.find(id=handle) +# # print(f'posts: { posts }') +# # for post in posts: +# print(f'post: { post }') +# if post: +# # annotationcontainer = post.findChildren(id="annotationcontainer", recursive=True)[0] +# # print(f'annotationcontainer: { annotationcontainer }') +# # print(f'annotationcontainer.contents: { annotationcontainer.contents }') +# # annotationcontainer.contents.append(f'{ annotation }') +# # print(f'annotationcontainer.contents: { annotationcontainer.contents }') - # new_tag = soup.new_tag("a", href="http://www.example.com") - new_tag = soup.new_tag("span") - new_tag.append(tag) - soup.find(id=handle).find(class_="tagcontainer").append(new_tag) - print(f'new soup: { str(soup) } ') +# # new_annotation = soup.new_annotation("a", href="http://www.example.com") +# new_annotation = soup.new_annotation("span") +# new_annotation.append(annotation) +# soup.find(id=handle).find(class_="annotationcontainer").append(new_annotation) +# print(f'new soup: { str(soup) } ') - # write soup to file - with open(log_path, 'w') as l: - l.write(str(soup)) +# # write soup to file +# with open(log_path, 'w') as l: +# l.write(str(soup)) class MUCBot(slixmpp.ClientXMPP): @@ -148,10 +150,6 @@ class MUCBot(slixmpp.ClientXMPP): # password=the_room_password, wait=True) - # NOTE(luke): disabled for now. We'll make it possible to speak to logbot privately later - # Send a message to the room - # self.send_message(mto=self.groupchat, mbody='Hello! RECbot here. I\'m new :). You can log text/image/sound/video messages, by including @bot in your message. Happy logging! PS. you can access the logs at https://vvvvvvaria.org/logs/', mtype='groupchat') - def muc_message(self, msg): # Some inspection commands #print('Message: {}'.format(msg)) @@ -159,99 +157,118 @@ class MUCBot(slixmpp.ClientXMPP): # 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: - # Check if output folder exists - if not os.path.exists(self.output): - os.mkdir(self.output) + if '@bot' in msg['body']: + # Send some info about this bot. + self.send_message( + mto=self.groupchat, + mbody='''Hello! RECbot here. I\'m a new version of logbot. - # Check if an OOB URL is included in the stanza (which is how an image is sent) - # (OOB object - https://xmpp.org/extensions/xep-0066.html#x-oob) - if len(msg['oob']['url']) > 0: +You can log type of text messages, by including __ADD__ in your message. Or, you can send an image/sound(*spark)/video(*spark) file to this chat and it will be logged for you. - # Send a reply - self.send_message(mto=self.groupchat, - mbody="Super, our log is growing. Your image is added!", - mtype='groupchat') +Items in the log can also be deleted again, by using the unique HANDLE of each post. You can find these handles in the generated HTML page, they look like this: +//-*. - # 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.write(u.read()) # write image to file - f.close() # close the output file +Happy logging! - # Add the image to the log - img = f'
' - write_to_log(self, img) +PS. you can access these logs at https://vvvvvvaria.org/logs/.''', + mtype='groupchat' + ) - # Include a new post in the log (only when '__ADD__' is used in the message) - if '__ADD__' in msg['body']: + else: + # Respond to incoming __ACTION_WORDS__! - # reply from the bot - self.send_message(mto=self.groupchat, - mbody=f'Noted! And added to the log. Thanks { msg["mucnick"] }!', - mtype='groupchat') + # Check if output folder exists + if not os.path.exists(self.output): + os.mkdir(self.output) - # Add the message to the log! - message = msg['body'].replace('__ADD__','') - message = f'
{ message }
' - write_to_log(self, message) + # Check if an OOB URL is included in the stanza (which is how an image is sent) + # (OOB object - https://xmpp.org/extensions/xep-0066.html#x-oob) + if len(msg['oob']['url']) > 0: - # Include a new post in the log (only when '__ADD__' is used in the message) - if '__ANNOTATE__' in msg['body']: + # Send a reply + self.send_message(mto=self.groupchat, + mbody="Super, our log is growing. Your image is added!", + mtype='groupchat') - handle = msg['body'].split()[1] - annotation = msg['body'].replace('__ANNOTATE__', '').replace(handle, '') - post = find_in_soup(self, handle, annotation) + # 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.write(u.read()) # write image to file + f.close() # close the output file - # reply from the bot - self.send_message(mto=self.groupchat, - mbody="Thanks!", - mtype='groupchat') + # Add the image to the log + img = f'
' + write_to_log(self, img) - # Check if this is a book ... - if '__BOOK__' in msg['body']: + # Include a new post in the log (only when '__ADD__' is used in the message) + if '__ADD__' in msg['body']: - self.send_message(mto=self.groupchat, - mbody="Oh a book, that's cool! Thanks {}!".format(msg['mucnick']), - mtype='groupchat') + # reply from the bot + self.send_message(mto=self.groupchat, + mbody=f'Noted! And added to the log. Thanks { msg["mucnick"] }!', + mtype='groupchat') - # Start of book feature - 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 + # Add the message to the log! + message = msg['body'].replace('__ADD__','') + message = f'
{ message }
' + write_to_log(self, message) - 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") + # Include a new post in the log (only when '__ADD__' is used in the message) + if '__ANNOTATE__' in msg['body']: - 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 + handle = msg['body'].split()[1] + annotation = msg['body'].replace('__ANNOTATE__', '').replace(handle, '') + post = find_in_soup(self, handle, annotation) - response = 'BOOK: ' + book_title + ' ' + book_author + ' ' + book_publisher + # reply from the bot + self.send_message(mto=self.groupchat, + mbody="Thanks!", + mtype='groupchat') - book_found = True + # Check if this is a book ... + if '__BOOK__' in msg['body']: - except IndexError: + self.send_message(mto=self.groupchat, + mbody="Oh a book, that's cool! Thanks {}!".format(msg['mucnick']), + mtype='groupchat') - book_found = False + # Start of book feature + 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 - if book_found: + 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") - # Add message to log - message = 'BOOK: ' + book_title + ' ' + book_author + ' ' + book_publisher - message = f'
{ message }
' - write_to_log(self, message) + 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 + message = f'
{ message }
' + write_to_log(self, message) - self.send_message(mto=self.groupchat, mbody='Hope this was the book you were looking for: ' + book_title + ' ' + book_author + ' ' + book_publisher, mtype='groupchat') + self.send_message(mto=self.groupchat, 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.groupchat, mbody='Sorry, no book found!', mtype='groupchat') + self.send_message(mto=self.groupchat, mbody='Sorry, no book found!', mtype='groupchat') if __name__ == '__main__': diff --git a/RECbot/templates/index.html b/RECbot/templates/index.html index 4488ad7..b609caa 100644 --- a/RECbot/templates/index.html +++ b/RECbot/templates/index.html @@ -8,7 +8,7 @@

Welcome to this Log!

-

This Log file is written through logbot and chat messages exchanged in a XMPP groupchat.

+

This Log file is based on chat messages exchanged in a XMPP groupchat and is written by RECbot.


For the writers of this log, you can:
@@ -19,19 +19,19 @@ __ADD__ a message,

- __DELETE__ it by using the ~HANDLE on the left (*spark), -
-
- __ANNOTATE__ something using the ~HANDLE, + __DELETE__ it by using the HANDLE on the left (*spark),

+ + + - __BOOK__ (*spark, almost there), + Request information about a __BOOK__ by sending a TITLE (*spark, almost there),

- or, ... (*spark) + ... (*spark)

diff --git a/RECbot/templates/stylesheet.css b/RECbot/templates/stylesheet.css index 267e8b0..455f568 100644 --- a/RECbot/templates/stylesheet.css +++ b/RECbot/templates/stylesheet.css @@ -17,7 +17,8 @@ div#welcome{ padding:0; } div#welcome hr{ - border:1px dotted blue; + border:0; + border-bottom:1px dotted blue; margin:2em 0; } div#echo{