From 710c2f1fc7d052a497e9c8f9181e9f2cc10da804 Mon Sep 17 00:00:00 2001 From: Alexis Marie Wright Date: Fri, 7 Dec 2018 16:52:16 -0500 Subject: [PATCH] PR feedback: Source !latest from RSS feed --- bot.py | 40 +++++++++++++++++++++++----------------- state.a.json | 1 - tasks.py | 9 ++++++--- 3 files changed, 29 insertions(+), 21 deletions(-) delete mode 100644 state.a.json diff --git a/bot.py b/bot.py index 9479db7..6ccfbe3 100644 --- a/bot.py +++ b/bot.py @@ -1,13 +1,22 @@ 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): 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( - title = post.title, - link = post.link, - published = post.published + guid = it.guid, + title = it.title, + link = it.link, + date = datetimejoin('-', it.published_parsed[0:3]) + ' ' + datetimejoin(':', it.published_parsed[3:6]) ) class CosmicBot(teambot.Handler): @@ -15,9 +24,8 @@ class CosmicBot(teambot.Handler): super(CosmicBot,self).__init__(bot) self.prefix = "!" 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.known_posts = dict() # to be populated by check_rss self.commands = dict() self.register_command("botlist",self.on_botlist) self.register_command("roster",self.on_roster) @@ -28,22 +36,19 @@ class CosmicBot(teambot.Handler): def on_connection_established(self): self.tasks.run() def check_rss(self,state,base_state): - memory = state["known"] - newtrans = rss.fetchNew(state["url"],memory) + newtrans = rss.fetchNew(state["url"],[x["guid"] for x in state["known"]]) if newtrans: - memory.extend(x.guid for x in newtrans) - self.known_posts.extend(newpost(x) for x in newtrans) + state["known"].extend(newpost(x) for x in newtrans) for trans in newtrans: self.say(state["channel"],"Transmission received: {transmission.title} ({transmission.link})".format(transmission=trans)) time.sleep(1) - state["known"]=memory return state def on_pubmsg(self,channel,nick,text): if not text.startswith(self.prefix): return args = text[len(self.prefix):].strip().split() 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 (not self.commands[cmd]["is_admin"]) or (self.commands[cmd]["is_admin"] and is_admin): try: @@ -67,14 +72,15 @@ class CosmicBot(teambot.Handler): line = re.sub("\s+"," ",line).split(" ",1) self.say(channel,"{}: {} (by {})".format(nick,line[1],unhighlight_nick(line[0]))) 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) + if count < 1: + count = 1 # ...nice try, smartass if count > 5: count = 5 # don't spam the channel - self.say(channel, "{}: Latest {} entries. (See cosmic.voyage for more!)".format(nick, count)) - for logline in loglist[:count]: - self.say(channel, "{}: {}".format(nick, logline.split("\t")[0][1:])) + items = self.tasks.states[0]["known"][:count] + self.say(channel, "{}: Latest {} {}. (See cosmic.voyage for more!)".format(nick, count, (count == 1 and "entry" or "entries"))) + for item in items: + self.say(channel, "{}: {} ({})".format(nick, item["title"], item["link"])) if __name__=="__main__": channels = "#cosmic".split() diff --git a/state.a.json b/state.a.json deleted file mode 100644 index 006be34..0000000 --- a/state.a.json +++ /dev/null @@ -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"} \ No newline at end of file diff --git a/tasks.py b/tasks.py index 161a4a7..edcf768 100644 --- a/tasks.py +++ b/tasks.py @@ -46,6 +46,9 @@ class TaskPool: json.dump(self.states[index],f) def load_state(self, index): - with open("state.{}.json".format(self.coroutines[index]["name"])) as f: - self.states[index] = json.load(f) - self.coroutines[index]["state"] = self.states[index] + try: + with open("state.{}.json".format(self.coroutines[index]["name"])) as f: + 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"]))