Browse Source

adding RSS support to logbot

master
manetta 4 years ago
parent
commit
30df62d1d5
  1. 18
      LogBot/feed.rss
  2. 29
      LogBot/logbot.py

18
LogBot/feed.rss

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>{{ title }}</title>
<link>{{ log_path }}</link>
<description>Collective log writing using XMPP chat groups and LogBot.</description>
<lastBuildDate>{{ date }}</lastBuildDate>
{% for post in db["messages"] %}
<item>
<title></title>
<link>{{ log_path }}#{{ x }}</link>
<description>{{ post }}</description>
<dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">LogBot</dc:creator>
<pubDate>{{ post.date }}</pubDate>
</item>
{% endfor %}
</channel>
</rss>

29
LogBot/logbot.py

@ -3,11 +3,10 @@ import re
import shutil import shutil
import urllib.request import urllib.request
from urllib.parse import urlparse from urllib.parse import urlparse
from datetime import datetime
import jinja2 import jinja2
from xbotlib import Bot from xbotlib import Bot
class Logbot(Bot): class Logbot(Bot):
help = """Oh dear, logbot is here! help = """Oh dear, logbot is here!
@ -106,10 +105,25 @@ class Logbot(Bot):
with open(log_path, "w") as out: with open(log_path, "w") as out:
html = template.render( html = template.render(
title=self.db[message.room]["title"], title=self.db[message.room]["title"],
db=self.db[message.room]["messages"], db=self.db[message.room]["messages"]
) )
out.write(html) out.write(html)
self.log.info(f"writing to: {log_path}") self.log.info(f"writing to: { log_path }")
def _generate_feed(self, message):
template = jinja2.Template(open("feed.rss").read())
room_name = self._parse_room_name(message.room)
feed_path = os.path.join(self.output, room_name, "feed.rss")
date = datetime.now()
with open(feed_path, "w") as out:
feed = template.render(
log_path=os.path.join(self.output, room_name, "index.html"),
title=self.db[message.room]["title"],
db=self.db[message.room],
date=date.strftime("%A, %d. %B %Y %I:%M%p")
)
out.write(feed)
self.log.info(f"writing to: { feed_path }")
def _add_to_db(self, message, media_post=None): def _add_to_db(self, message, media_post=None):
"""Save new entry to database.""" """Save new entry to database."""
@ -122,8 +136,8 @@ class Logbot(Bot):
if media_post: if media_post:
self.db[message.room]["messages"][new_key] = media_post self.db[message.room]["messages"][new_key] = media_post
else: else:
replaced = message.content.replace("@add", "") post = message.content.replace("@add ", "")
self.db[message.room]["messages"][new_key] = replaced self.db[message.room]["messages"][new_key] = post
self.db._dumps() self.db._dumps()
def _parse_room_name(self, room): def _parse_room_name(self, room):
@ -218,6 +232,9 @@ class Logbot(Bot):
# Regenerate the log webpage # Regenerate the log webpage
self._write_log(message) self._write_log(message)
# Regenerate the RSS feed
self._generate_feed(message)
# Reply to the groupchat # Reply to the groupchat
self.reply(reply, room=message.room) self.reply(reply, room=message.room)

Loading…
Cancel
Save