PR feedback: Source !latest from RSS feed

This commit is contained in:
Alexis Marie Wright 2018-12-07 16:52:16 -05:00
parent f07ff9ddbb
commit 137e1f87ab
3 changed files with 317 additions and 21 deletions

40
bot.py
View File

@ -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()

View File

@ -1 +1,288 @@
{"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"}
{
"url": "https://cosmic.voyage/rss.xml",
"known": [
{
"guid": "https://cosmic.voyage/anon.penet.fi/2-vulnerability.html",
"title": "anon.penet.fi - VULNERABILITY",
"link": "https://cosmic.voyage/anon.penet.fi/2-vulnerability.html",
"date": "2018-12-07 21:20:29"
},
{
"guid": "https://cosmic.voyage/Malkonkordo/0001.html",
"title": "Malkonkordo - Formal Request For Assistance",
"link": "https://cosmic.voyage/Malkonkordo/0001.html",
"date": "2018-12-07 21:55:55"
},
{
"guid": "https://cosmic.voyage/Hoffnung/007.html",
"title": "Hoffnung - violence begets violence",
"link": "https://cosmic.voyage/Hoffnung/007.html",
"date": "2018-12-07 13:12:04"
},
{
"guid": "https://cosmic.voyage/Voortrekker/8-get-your-house-in-order.html",
"title": "Voortrekker - Get your house in order",
"link": "https://cosmic.voyage/Voortrekker/8-get-your-house-in-order.html",
"date": "2018-12-07 04:20:44"
},
{
"guid": "https://cosmic.voyage/Voortrekker/7-key-compromise.html",
"title": "Voortrekker - Key compromise",
"link": "https://cosmic.voyage/Voortrekker/7-key-compromise.html",
"date": "2018-12-07 04:20:44"
},
{
"guid": "https://cosmic.voyage/Malkonkordo/0000.html",
"title": "Malkonkordo - PRIMARY COMMUNICATIONS DOWN",
"link": "https://cosmic.voyage/Malkonkordo/0000.html",
"date": "2018-12-06 23:30:49"
},
{
"guid": "https://cosmic.voyage/Murmur%20Den/in_signal_promise.html",
"title": "Murmur Den - In signal: promise",
"link": "https://cosmic.voyage/Murmur%20Den/in_signal_promise.html",
"date": "2018-12-06 03:40:53"
},
{
"guid": "https://cosmic.voyage/Erinnerung/init.html",
"title": "Erinnerung - 12B01",
"link": "https://cosmic.voyage/Erinnerung/init.html",
"date": "2018-12-05 02:18:03"
},
{
"guid": "https://cosmic.voyage/Hoffnung/006a.html",
"title": "Hoffnung - inter ship relay for Voortrekker",
"link": "https://cosmic.voyage/Hoffnung/006a.html",
"date": "2018-12-04 18:21:40"
},
{
"guid": "https://cosmic.voyage/Hoffnung/006.html",
"title": "Hoffnung - preparing for an escalation",
"link": "https://cosmic.voyage/Hoffnung/006.html",
"date": "2018-12-02 11:27:28"
},
{
"guid": "https://cosmic.voyage/Thrix%20Memoriam/report.html",
"title": "Thrix Memoriam - First report.",
"link": "https://cosmic.voyage/Thrix%20Memoriam/report.html",
"date": "2018-12-05 02:23:26"
},
{
"guid": "https://cosmic.voyage/Dei%20Genetrix/3220120400.html",
"title": "Dei Genetrix - QEC transmission #1",
"link": "https://cosmic.voyage/Dei%20Genetrix/3220120400.html",
"date": "2018-12-04 14:10:01"
},
{
"guid": "https://cosmic.voyage/The%20Cosmic Hummingbird/automatic-diagnostics.html",
"title": "The Cosmic Hummingbird - AUTOMATED-LOG_001",
"link": "https://cosmic.voyage/The%20Cosmic Hummingbird/automatic-diagnostics.html",
"date": "2018-12-04 05:35:01"
},
{
"guid": "https://cosmic.voyage/The%20Cosmic Hummingbird/waking-up.html",
"title": "The Cosmic Hummingbird - Report-001-Pilot_Anderson",
"link": "https://cosmic.voyage/The%20Cosmic Hummingbird/waking-up.html",
"date": "2018-12-04 04:43:42"
},
{
"guid": "https://cosmic.voyage/Voortrekker/6-these-things-i-wish-you-to.html",
"title": "Voortrekker - These things I wish you to know.",
"link": "https://cosmic.voyage/Voortrekker/6-these-things-i-wish-you-to.html",
"date": "2018-12-07 04:35:24"
},
{
"guid": "https://cosmic.voyage/Murmur%20Den/Sweet_pain_of_luxury_agony.html",
"title": "Murmur Den - Sweet_pain_of_luxury_agony",
"link": "https://cosmic.voyage/Murmur%20Den/Sweet_pain_of_luxury_agony.html",
"date": "2018-12-04 03:30:12"
},
{
"guid": "https://cosmic.voyage/Melchizedek/006.html",
"title": "Melchizedek - The dark night of the soul",
"link": "https://cosmic.voyage/Melchizedek/006.html",
"date": "2018-12-04 02:31:28"
},
{
"guid": "https://cosmic.voyage/Persephone%20Prime/lm002.html",
"title": "Persephone Prime - Best Wishes",
"link": "https://cosmic.voyage/Persephone%20Prime/lm002.html",
"date": "2018-12-04 00:46:31"
},
{
"guid": "https://cosmic.voyage/Hoffnung/005a.html",
"title": "Hoffnung - inter ship relay for Voortrekker",
"link": "https://cosmic.voyage/Hoffnung/005a.html",
"date": "2018-12-04 18:21:33"
},
{
"guid": "https://cosmic.voyage/Hoffnung/005.html",
"title": "Hoffnung - fascist pigs",
"link": "https://cosmic.voyage/Hoffnung/005.html",
"date": "2018-12-04 18:13:56"
},
{
"guid": "https://cosmic.voyage/Melchizedek/005.html",
"title": "Melchizedek - Message to Voortrekker GmbH",
"link": "https://cosmic.voyage/Melchizedek/005.html",
"date": "2018-12-03 19:35:51"
},
{
"guid": "https://cosmic.voyage/Voortrekker/5-re-re-hoffnung.html",
"title": "Voortrekker - Re: Re: Hoffnung",
"link": "https://cosmic.voyage/Voortrekker/5-re-re-hoffnung.html",
"date": "2018-12-07 04:34:21"
},
{
"guid": "https://cosmic.voyage/Persephone%20Prime/la001.html",
"title": "Persephone Prime - StatRep Lombardia Azul",
"link": "https://cosmic.voyage/Persephone%20Prime/la001.html",
"date": "2018-12-03 13:06:58"
},
{
"guid": "https://cosmic.voyage/Hoffnung/004.html",
"title": "Hoffnung - I am awake",
"link": "https://cosmic.voyage/Hoffnung/004.html",
"date": "2018-12-03 07:30:02"
},
{
"guid": "https://cosmic.voyage/anon.penet.fi/1-some-dipshit-on-a-spaceship.html",
"title": "anon.penet.fi - SOME_DIPSHIT_ON_A_SPACESHIP_WASNT_CAREFUL_ENOUGH.txt",
"link": "https://cosmic.voyage/anon.penet.fi/1-some-dipshit-on-a-spaceship.html",
"date": "2018-12-03 03:53:38"
},
{
"guid": "https://cosmic.voyage/Murmur%20Den/Ours_is_this_chimera.html",
"title": "Murmur Den - Ours is this chimera",
"link": "https://cosmic.voyage/Murmur%20Den/Ours_is_this_chimera.html",
"date": "2018-12-03 02:28:41"
},
{
"guid": "https://cosmic.voyage/Voortrekker/4-re-hoffnung.html",
"title": "Voortrekker - Re: Hoffnung",
"link": "https://cosmic.voyage/Voortrekker/4-re-hoffnung.html",
"date": "2018-12-03 03:13:47"
},
{
"guid": "https://cosmic.voyage/Persephone%20Prime/lm001.html",
"title": "Persephone Prime - Missing In Action",
"link": "https://cosmic.voyage/Persephone%20Prime/lm001.html",
"date": "2018-12-03 13:13:47"
},
{
"guid": "https://cosmic.voyage/Hoffnung/003.html",
"title": "Hoffnung - everything has changed",
"link": "https://cosmic.voyage/Hoffnung/003.html",
"date": "2018-12-02 10:51:36"
},
{
"guid": "https://cosmic.voyage/Melchizedek/004.html",
"title": "Melchizedek - Time and space",
"link": "https://cosmic.voyage/Melchizedek/004.html",
"date": "2018-12-02 00:42:12"
},
{
"guid": "https://cosmic.voyage/Hoffnung/002a.html",
"title": "Hoffnung - inter ship relay for Garnet Star",
"link": "https://cosmic.voyage/Hoffnung/002a.html",
"date": "2018-12-02 10:51:28"
},
{
"guid": "https://cosmic.voyage/Hoffnung/002.html",
"title": "Hoffnung - cryo systems manipulated",
"link": "https://cosmic.voyage/Hoffnung/002.html",
"date": "2018-12-02 10:51:17"
},
{
"guid": "https://cosmic.voyage/Garnet%20Star/001.html",
"title": "Garnet Star - Good news, bad news.",
"link": "https://cosmic.voyage/Garnet%20Star/001.html",
"date": "2018-11-30 05:48:27"
},
{
"guid": "https://cosmic.voyage/Voortrekker/3-listen-im-really-sorry-but.html",
"title": "Voortrekker - listen. i'm really sorry but",
"link": "https://cosmic.voyage/Voortrekker/3-listen-im-really-sorry-but.html",
"date": "2018-12-07 04:32:24"
},
{
"guid": "https://cosmic.voyage/Voortrekker/2-were-you-going-to-tell-me.html",
"title": "Voortrekker - Were you going to tell me?",
"link": "https://cosmic.voyage/Voortrekker/2-were-you-going-to-tell-me.html",
"date": "2018-12-07 04:31:41"
},
{
"guid": "https://cosmic.voyage/Hoffnung/001.html",
"title": "Hoffnung - all systems nominal",
"link": "https://cosmic.voyage/Hoffnung/001.html",
"date": "2018-12-02 10:51:09"
},
{
"guid": "https://cosmic.voyage/Franciscus/navlog001.html",
"title": "Franciscus - A wing and a prayer",
"link": "https://cosmic.voyage/Franciscus/navlog001.html",
"date": "2018-11-29 03:58:51"
},
{
"guid": "https://cosmic.voyage/Voortrekker/1-made-it.html",
"title": "Voortrekker - Made it...",
"link": "https://cosmic.voyage/Voortrekker/1-made-it.html",
"date": "2018-12-07 04:30:33"
},
{
"guid": "https://cosmic.voyage/Melchizedek/003.html",
"title": "Melchizedek - The dark and cold",
"link": "https://cosmic.voyage/Melchizedek/003.html",
"date": "2018-11-29 01:26:46"
},
{
"guid": "https://cosmic.voyage/Franciscus/log001.html",
"title": "Franciscus - Lost Shepherds",
"link": "https://cosmic.voyage/Franciscus/log001.html",
"date": "2018-11-28 05:04:37"
},
{
"guid": "https://cosmic.voyage/Garnet%20Star/000.html",
"title": "Garnet Star - Danger evaluation and decisions",
"link": "https://cosmic.voyage/Garnet%20Star/000.html",
"date": "2018-11-24 01:25:34"
},
{
"guid": "https://cosmic.voyage/Excelsior/001.html",
"title": "Excelsior - Systems Admin Report - Beta Delta 21 Epsilon",
"link": "https://cosmic.voyage/Excelsior/001.html",
"date": "2018-12-02 00:04:18"
},
{
"guid": "https://cosmic.voyage/Melchizedek/002.html",
"title": "Melchizedek - Crew and assignments",
"link": "https://cosmic.voyage/Melchizedek/002.html",
"date": "2018-11-29 00:25:14"
},
{
"guid": "https://cosmic.voyage/Starbloom/001.html",
"title": "Starbloom - Pleasant vibes and best wishes",
"link": "https://cosmic.voyage/Starbloom/001.html",
"date": "2018-11-23 13:24:50"
},
{
"guid": "https://cosmic.voyage/adrestia/hello.html",
"title": "adrestia - awakening and approaching",
"link": "https://cosmic.voyage/adrestia/hello.html",
"date": "2018-11-21 06:49:23"
},
{
"guid": "https://cosmic.voyage/Melchizedek/001.html",
"title": "Melchizedek - Cryosleep interrupted",
"link": "https://cosmic.voyage/Melchizedek/001.html",
"date": "2018-11-23 02:44:15"
},
{
"guid": "https://cosmic.voyage/Starbloom/002.html",
"title": "Starbloom - There is no such thing as bad news",
"link": "https://cosmic.voyage/Starbloom/002.html",
"date": "2018-12-07 17:40:51"
}
],
"channel": "#cosmic"
}

View File

@ -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"]))