[rssnotify] Update and fix a few things
parent
5fd4be8693
commit
6eee4e7567
47
rssnotify.py
47
rssnotify.py
|
@ -47,6 +47,7 @@ def resolve_channels(phenny, l):
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
class RssNotify():
|
class RssNotify():
|
||||||
|
MAX_MESSAGES = 6
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self.config = config
|
self.config = config
|
||||||
self.last_updated = {}
|
self.last_updated = {}
|
||||||
|
@ -61,27 +62,31 @@ class RssNotify():
|
||||||
print("[RssNotify]: Checking RSS feeds...")
|
print("[RssNotify]: Checking RSS feeds...")
|
||||||
for fid, feedspec in enumerate(self.config["feeds"]):
|
for fid, feedspec in enumerate(self.config["feeds"]):
|
||||||
feed = feedparser.parse(feedspec[0], agent="Mozilla/5.0 (compatible; MinetestBot)")
|
feed = feedparser.parse(feedspec[0], agent="Mozilla/5.0 (compatible; MinetestBot)")
|
||||||
updated = 0
|
if self.firstrun:
|
||||||
|
self.last_updated[fid] = max((to_unix_time(e.updated) for e in feed.entries), default=0)
|
||||||
|
continue
|
||||||
|
new = []
|
||||||
for entry in feed.entries:
|
for entry in feed.entries:
|
||||||
if self.firstrun:
|
|
||||||
break
|
|
||||||
if self.last_updated[fid] >= to_unix_time(entry.updated):
|
if self.last_updated[fid] >= to_unix_time(entry.updated):
|
||||||
continue
|
continue
|
||||||
|
new.append(entry)
|
||||||
|
if len(new) == 0:
|
||||||
|
continue
|
||||||
|
print("[RssNotify]: Found %d update(s) for '%s'" % (len(new), feedspec[0]))
|
||||||
|
self.last_updated[fid] = max(to_unix_time(e.updated) for e in feed.entries)
|
||||||
|
new.reverse()
|
||||||
|
if self.config["logfile"] is not None:
|
||||||
|
with open(self.config["logfile"], "a", encoding="utf-8") as f:
|
||||||
|
for entry in new:
|
||||||
|
message = self._format_msg(entry, log_format=True)
|
||||||
|
f.write(message + "\n")
|
||||||
|
for entry in new[:RssNotify.MAX_MESSAGES]:
|
||||||
message = self._format_msg(entry)
|
message = self._format_msg(entry)
|
||||||
self._announce(phenny, message, feedspec[1])
|
self._announce(phenny, message, feedspec[1])
|
||||||
if self.config["logfile"] is not None:
|
if len(new) > RssNotify.MAX_MESSAGES:
|
||||||
with open(self.config["logfile"], "a", encoding="utf-8") as f:
|
message = self._get_cutoff_message(len(new) - RssNotify.MAX_MESSAGES)
|
||||||
message = self._format_msg(entry, log_format=True)
|
self._announce(phenny, message, feedspec[1])
|
||||||
f.write(message)
|
self.firstrun = False
|
||||||
f.write("\n")
|
|
||||||
updated += 1
|
|
||||||
new_time = max((to_unix_time(e.updated) for e in feed.entries), default=0)
|
|
||||||
if new_time > self.last_updated[fid]:
|
|
||||||
self.last_updated[fid] = new_time
|
|
||||||
if updated > 0:
|
|
||||||
print("[RssNotify]: Found %d update(s) for '%s'" % (updated, feedspec[0]))
|
|
||||||
if self.firstrun:
|
|
||||||
self.firstrun = False
|
|
||||||
print("[RssNotify]: Checked %d RSS feeds in %0.3f seconds" % (len(self.config["feeds"]), time.time()-start))
|
print("[RssNotify]: Checked %d RSS feeds in %0.3f seconds" % (len(self.config["feeds"]), time.time()-start))
|
||||||
def _shorten(self, link):
|
def _shorten(self, link):
|
||||||
# We can utilitze git.io to shorten *.github.com links
|
# We can utilitze git.io to shorten *.github.com links
|
||||||
|
@ -103,10 +108,10 @@ class RssNotify():
|
||||||
f_all = "\x0302[git]\x0f %s -> \x0303%s\x0f: \x02%s\x0f \x0313%s\x0f %s (\x0315%s\x0f)"
|
f_all = "\x0302[git]\x0f %s -> \x0303%s\x0f: \x02%s\x0f \x0313%s\x0f %s (\x0315%s\x0f)"
|
||||||
committer_realname = feed_entry.authors[0].name
|
committer_realname = feed_entry.authors[0].name
|
||||||
if committer_realname == "":
|
if committer_realname == "":
|
||||||
try:
|
try:
|
||||||
committer_realname = feed_entry.authors[0].email
|
committer_realname = feed_entry.authors[0].email
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
committer_realname = ""
|
committer_realname = ""
|
||||||
try:
|
try:
|
||||||
committer = feed_entry.authors[0].href.replace('https://github.com/',"")
|
committer = feed_entry.authors[0].href.replace('https://github.com/',"")
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
@ -127,6 +132,8 @@ class RssNotify():
|
||||||
else:
|
else:
|
||||||
committer_final = f_clong % (committer, committer_realname)
|
committer_final = f_clong % (committer, committer_realname)
|
||||||
return f_all % (committer_final, repo_name, commit_text, commit_hash, commit_link, commit_time)
|
return f_all % (committer_final, repo_name, commit_text, commit_hash, commit_link, commit_time)
|
||||||
|
def _get_cutoff_message(self, left):
|
||||||
|
return "\x0302[git]\x0f (%d newer commits not shown)" % left
|
||||||
def _announce(self, phenny, message, chans):
|
def _announce(self, phenny, message, chans):
|
||||||
chans = resolve_channels(phenny, chans)
|
chans = resolve_channels(phenny, chans)
|
||||||
for ch in chans:
|
for ch in chans:
|
||||||
|
|
Loading…
Reference in New Issue