forked from khuxkm/cosmicbot
PR feedback: Source !latest from RSS feed
This commit is contained in:
parent
f07ff9ddbb
commit
710c2f1fc7
40
bot.py
40
bot.py
|
@ -1,13 +1,22 @@
|
||||||
import teambot,tasks,rss,time,sys,subprocess,re
|
import teambot,tasks,rss,time,sys,subprocess,re
|
||||||
|
|
||||||
|
import teambot,tasks,rss,time,sys,subprocess,re
|
||||||
|
|
||||||
|
feed_url = "https://cosmic.voyage/rss.xml"
|
||||||
|
bot_op = "khuxkm@sudoers.tilde.team"
|
||||||
|
|
||||||
def unhighlight_nick (nick):
|
def unhighlight_nick (nick):
|
||||||
return "_{}_".format(nick)
|
return "_{}_".format(nick)
|
||||||
|
|
||||||
def newpost(post):
|
def datetimejoin(joiner, parts):
|
||||||
|
return joiner.join([str(x).rjust(2, '0') for x in parts])
|
||||||
|
|
||||||
|
def newpost(it):
|
||||||
return dict(
|
return dict(
|
||||||
title = post.title,
|
guid = it.guid,
|
||||||
link = post.link,
|
title = it.title,
|
||||||
published = post.published
|
link = it.link,
|
||||||
|
date = datetimejoin('-', it.published_parsed[0:3]) + ' ' + datetimejoin(':', it.published_parsed[3:6])
|
||||||
)
|
)
|
||||||
|
|
||||||
class CosmicBot(teambot.Handler):
|
class CosmicBot(teambot.Handler):
|
||||||
|
@ -15,9 +24,8 @@ class CosmicBot(teambot.Handler):
|
||||||
super(CosmicBot,self).__init__(bot)
|
super(CosmicBot,self).__init__(bot)
|
||||||
self.prefix = "!"
|
self.prefix = "!"
|
||||||
self.tasks = tasks.TaskPool()
|
self.tasks = tasks.TaskPool()
|
||||||
self.tasks.add_coroutine(self.check_rss,12,dict(url="https://cosmic.voyage/rss.xml",known=[],channel="#cosmic"))
|
self.tasks.add_coroutine(self.check_rss,12,dict(url=feed_url,known=[],channel="#cosmic"))
|
||||||
self.tasks.load_state(0)
|
self.tasks.load_state(0)
|
||||||
self.known_posts = dict() # to be populated by check_rss
|
|
||||||
self.commands = dict()
|
self.commands = dict()
|
||||||
self.register_command("botlist",self.on_botlist)
|
self.register_command("botlist",self.on_botlist)
|
||||||
self.register_command("roster",self.on_roster)
|
self.register_command("roster",self.on_roster)
|
||||||
|
@ -28,22 +36,19 @@ class CosmicBot(teambot.Handler):
|
||||||
def on_connection_established(self):
|
def on_connection_established(self):
|
||||||
self.tasks.run()
|
self.tasks.run()
|
||||||
def check_rss(self,state,base_state):
|
def check_rss(self,state,base_state):
|
||||||
memory = state["known"]
|
newtrans = rss.fetchNew(state["url"],[x["guid"] for x in state["known"]])
|
||||||
newtrans = rss.fetchNew(state["url"],memory)
|
|
||||||
if newtrans:
|
if newtrans:
|
||||||
memory.extend(x.guid for x in newtrans)
|
state["known"].extend(newpost(x) for x in newtrans)
|
||||||
self.known_posts.extend(newpost(x) for x in newtrans)
|
|
||||||
for trans in newtrans:
|
for trans in newtrans:
|
||||||
self.say(state["channel"],"Transmission received: {transmission.title} ({transmission.link})".format(transmission=trans))
|
self.say(state["channel"],"Transmission received: {transmission.title} ({transmission.link})".format(transmission=trans))
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
state["known"]=memory
|
|
||||||
return state
|
return state
|
||||||
def on_pubmsg(self,channel,nick,text):
|
def on_pubmsg(self,channel,nick,text):
|
||||||
if not text.startswith(self.prefix):
|
if not text.startswith(self.prefix):
|
||||||
return
|
return
|
||||||
args = text[len(self.prefix):].strip().split()
|
args = text[len(self.prefix):].strip().split()
|
||||||
cmd = args.pop(0)
|
cmd = args.pop(0)
|
||||||
is_admin = self.event.source.userhost == "khuxkm@sudoers.tilde.team"
|
is_admin = self.event.source.userhost == bot_op
|
||||||
if cmd in self.commands:
|
if cmd in self.commands:
|
||||||
if (not self.commands[cmd]["is_admin"]) or (self.commands[cmd]["is_admin"] and is_admin):
|
if (not self.commands[cmd]["is_admin"]) or (self.commands[cmd]["is_admin"] and is_admin):
|
||||||
try:
|
try:
|
||||||
|
@ -67,14 +72,15 @@ class CosmicBot(teambot.Handler):
|
||||||
line = re.sub("\s+"," ",line).split(" ",1)
|
line = re.sub("\s+"," ",line).split(" ",1)
|
||||||
self.say(channel,"{}: {} (by {})".format(nick,line[1],unhighlight_nick(line[0])))
|
self.say(channel,"{}: {} (by {})".format(nick,line[1],unhighlight_nick(line[0])))
|
||||||
def on_latest(self,channel,nick,count=5):
|
def on_latest(self,channel,nick,count=5):
|
||||||
loglist = subprocess.check_output(["cat", "/var/gopher/listing.gophermap"]).decode("ascii").split("\n");
|
|
||||||
capped = False
|
|
||||||
count = int(count)
|
count = int(count)
|
||||||
|
if count < 1:
|
||||||
|
count = 1 # ...nice try, smartass
|
||||||
if count > 5:
|
if count > 5:
|
||||||
count = 5 # don't spam the channel
|
count = 5 # don't spam the channel
|
||||||
self.say(channel, "{}: Latest {} entries. (See cosmic.voyage for more!)".format(nick, count))
|
items = self.tasks.states[0]["known"][:count]
|
||||||
for logline in loglist[:count]:
|
self.say(channel, "{}: Latest {} {}. (See cosmic.voyage for more!)".format(nick, count, (count == 1 and "entry" or "entries")))
|
||||||
self.say(channel, "{}: {}".format(nick, logline.split("\t")[0][1:]))
|
for item in items:
|
||||||
|
self.say(channel, "{}: {} ({})".format(nick, item["title"], item["link"]))
|
||||||
|
|
||||||
if __name__=="__main__":
|
if __name__=="__main__":
|
||||||
channels = "#cosmic".split()
|
channels = "#cosmic".split()
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
{"url": "https://cosmic.voyage/rss.xml", "known": ["https://cosmic.voyage/Melchizedek/004.html", "https://cosmic.voyage/Hoffnung/002a.html", "https://cosmic.voyage/Hoffnung/002.html", "https://cosmic.voyage/Garnet%20Star/001.html", "https://cosmic.voyage/Voortrekker/3-listen-im-really-sorry-but.html", "https://cosmic.voyage/Voortrekker/2-were-you-going-to-tell-me.html", "https://cosmic.voyage/Hoffnung/001.html", "https://cosmic.voyage/Franciscus/navlog001.html", "https://cosmic.voyage/Voortrekker/1-made-it.html", "https://cosmic.voyage/Melchizedek/003.html", "https://cosmic.voyage/Franciscus/log001.html", "https://cosmic.voyage/Garnet%20Star/000.html", "https://cosmic.voyage/Excelsior/001.html", "https://cosmic.voyage/Melchizedek/002.html", "https://cosmic.voyage/Starbloom/001.html", "https://cosmic.voyage/adrestia/hello.html", "https://cosmic.voyage/Melchizedek/001.html"], "channel": "#cosmic"}
|
|
9
tasks.py
9
tasks.py
|
@ -46,6 +46,9 @@ class TaskPool:
|
||||||
json.dump(self.states[index],f)
|
json.dump(self.states[index],f)
|
||||||
|
|
||||||
def load_state(self, index):
|
def load_state(self, index):
|
||||||
with open("state.{}.json".format(self.coroutines[index]["name"])) as f:
|
try:
|
||||||
self.states[index] = json.load(f)
|
with open("state.{}.json".format(self.coroutines[index]["name"])) as f:
|
||||||
self.coroutines[index]["state"] = self.states[index]
|
self.states[index] = json.load(f)
|
||||||
|
self.coroutines[index]["state"] = self.states[index]
|
||||||
|
except:
|
||||||
|
print("state.{}.json not found or couldn't be opened; using default".format(self.coroutines[index]["name"]))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user