Master release candidate 1, added alias to bbj, added RSS support for tilde.news links/comments

This commit is contained in:
aewens 2018-09-15 21:27:56 -04:00
parent 746ed85333
commit 97cdb98542
3 changed files with 96 additions and 4 deletions

View File

@ -1,4 +1,5 @@
from coroutines.bbj import BBJ
from coroutines.rss import RSS
# {
# "worker": test,
@ -12,8 +13,29 @@ coroutines = [
"worker": lambda state: BBJ(state).start(),
"interval": 5,
"state": {
"alias": "bbj",
"source": "http://localhost:7099/api",
"channels": ["#insane"], #team
"channels": ["#bots"] #team
}
},
{
"worker": lambda state: RSS(state).start(),
"interval": 6,
"state": {
"alias": "title",
"source": "https://tilde.news/newest.rss",
"use": "title",
"channels": ["#bots"] # "#meta", "#tildeverse"
}
},
{
"worker": lambda state: RSS(state).start(),
"interval": 8,
"state": {
"alias": "links-comments",
"source": "https://tilde.news/comments.rss",
"use": "summary",
"channels": ["#bots"] #tildeverse
}
}
]

View File

@ -7,13 +7,13 @@ from re import sub
class BBJ:
def __init__(self, state):
self.name = "bbj"
self.name = "BBJ"
self.bot = state["bot"]
self.alias = state["alias"]
self.source = state["source"]
self.channels = state["channels"]
self.memory = state.get("memory", {
"initialized": False,
# "timestamp": datetime.now().timestamp(),
"known": dict()
})
@ -27,6 +27,7 @@ class BBJ:
self.fetch(self.mirror)
return {
"bot": self.bot,
"alias": self.alias,
"source": self.source,
"channels": self.channels,
"memory": self.memory
@ -51,7 +52,7 @@ class BBJ:
link = "{}?thread_id={}".format(php, thread_id)
for channel in self.channels:
response = "'{}' ({}) : {} <{}>".format(title, username, body, link)
message = "[{}] {}".format(self.name, response)
message = "[{}] {}".format(self.alias, response)
self.bot.send_message(channel, message)
def get_thread(self, thread_id, callback):

View File

@ -0,0 +1,69 @@
from xml.etree import ElementTree as etree
from urllib.request import Request, urlopen
from urllib.error import HTTPError
from json import loads, dumps
from re import sub
class RSS:
def __init__(self, state):
self.name = "RSS"
self.bot = state["bot"]
self.alias = state["alias"]
self.use = state["use"]
self.source = state["source"]
self.channels = state["channels"]
self.memory = state.get("memory", {
"initialized": False,
"known": list()
})
def start(self):
if not self.memory["initialized"]:
self.memory["initialized"] = True
self.fetch(self.cache)
return self.run()
def run(self):
self.fetch(self.mirror)
return {
"bot": self.bot,
"alias": self.alias,
"use": self.use,
"source": self.source,
"channels": self.channels,
"memory": self.memory
}
def cache(self, item):
guid = item.findtext("guid", None)
if guid is not None:
self.memory["known"].append(guid)
def mirror(self, item):
guid = item.findtext("guid", None)
if guid is None:
return
if guid in self.memory["known"]:
return
self.memory["known"].append(guid)
use = sub(r"(<\/?[^>]+>)|\\n", "", item.findtext([self.use], ""))
user = item.findtext("author", "").split("@")[0]
post = "{} (posted by {}) <{}>".format(use, user, guid)
response = "[{}] {}".format(self.alias, post)
for channel in self.channels:
self.bot.send_message(channel, response)
def fetch(self, callback):
req = Request(self.source)
try:
response = urlopen(req).read()
except HTTPError:
return
feed = etree.fromstring(response)
items = feed.findall("channel/item")
for item in items:
callback(item)