Finally fix rssnotify

(and convert spaces to tabs)
master
sfan5 2014-07-23 10:17:18 +02:00
parent cedd28c2f6
commit dca95a9911
1 changed files with 105 additions and 101 deletions

View File

@ -3,28 +3,30 @@
rssnotify.py - Phenny RssNotify Module rssnotify.py - Phenny RssNotify Module
Copyright 2013, sfan5 Copyright 2013, sfan5
""" """
import time, urllib.parse, re import time
import re
import web
import feedparser # sudo pip install feedparser import feedparser # sudo pip install feedparser
rssnotify = {} rssnotify = {}
def to_unix_time(st): # not really accurate, but works def to_unix_time(st): # not really accurate, but works
reg = re.compile("([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})") reg = re.compile("([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})")
g = reg.match(st).groups(1) g = reg.match(st).groups(1)
t = 0 t = 0
t += int(g[5]) t += int(g[5])
t += int(g[4]) * 60 t += int(g[4]) * 60
t += int(g[3]) * 60 * 60 t += int(g[3]) * 60 * 60
t += int(g[2]) * 60 * 60 * 24 t += int(g[2]) * 60 * 60 * 24
t += int(g[1]) * 60 * 60 * 24 * 30 t += int(g[1]) * 60 * 60 * 24 * 30
t += int(g[0]) * 60 * 60 * 24 * 30 * 12 t += int(g[0]) * 60 * 60 * 24 * 30 * 12
return t return t
def excepta(arr, exclude): def excepta(arr, exclude):
o = [] o = []
for el in arr: for el in arr:
if not el in exclude: if not el in exclude:
o.append(el) o.append(el)
return o return o
rssnotify["last_updated_feeds"] = {} rssnotify["last_updated_feeds"] = {}
@ -36,91 +38,93 @@ rssnotify["last_update"] = time.time() - rssnotify["update_cooldown"]
def rsscheck(phenny, input): def rsscheck(phenny, input):
t = time.time() t = time.time()
if rssnotify["last_update"] > t-rssnotify["update_cooldown"]: if rssnotify["last_update"] > t-rssnotify["update_cooldown"]:
return return
rssnotify["last_update"] = t rssnotify["last_update"] = t
print("[RssNotify]: Checking RSS Feeds...") print("[RssNotify]: Checking RSS Feeds...")
start = time.time() start = time.time()
allchans = excepta(phenny.bot.channels, ['##minebest']) allchans = excepta(phenny.bot.channels, ['##minebest'])
feeds = [ feeds = [
('https://github.com/minetest/minetest/commits/master.atom', allchans), ('https://github.com/minetest/minetest/commits/master.atom', allchans),
('https://github.com/minetest/minetest_game/commits/master.atom', allchans), ('https://github.com/minetest/minetest_game/commits/master.atom', allchans),
('https://github.com/minetest/minetestmapper/commits/master.atom', allchans), ('https://github.com/minetest/minetestmapper/commits/master.atom', allchans),
('https://github.com/minetest/master-server/commits/master.atom', allchans), ('https://github.com/minetest/master-server/commits/master.atom', allchans),
('https://github.com/Uberi/MineTest-WorldEdit/commits/master.atom', allchans), ('https://github.com/Uberi/MineTest-WorldEdit/commits/master.atom', allchans),
('https://github.com/Jeija/minetest-mod-mesecons/commits/master.atom', allchans), ('https://github.com/Jeija/minetest-mod-mesecons/commits/master.atom', allchans),
] ]
for v in range(0, len(feeds)): for v in range(0, len(feeds)):
url = feeds[v][0] url = feeds[v][0]
feednum = v feednum = v
options = { options = {
'agent': 'Mozilla/5.0 (MinetestBot)', 'agent': 'Mozilla/5.0 (MinetestBot)',
'referrer': 'http://minetest.net' 'referrer': 'http://minetest.net'
} }
feed = feedparser.parse(url, **options) feed = feedparser.parse(url, **options)
updcnt = 0 updcnt = 0
for feed_entry in feed.entries: for feed_entry in feed.entries:
if not feednum in rssnotify["last_updated_feeds"].keys(): if not feednum in rssnotify["last_updated_feeds"].keys():
rssnotify["last_updated_feeds"][feednum] = -1 rssnotify["last_updated_feeds"][feednum] = -1
if rssnotify["last_updated_feeds"][feednum] < to_unix_time(feed_entry.updated): if rssnotify["last_updated_feeds"][feednum] < to_unix_time(feed_entry.updated):
commiter_realname = feed_entry.authors[0].name commiter_realname = feed_entry.authors[0].name
if commiter_realname == "": if commiter_realname == "":
try: try:
commiter_realname = feed_entry.authors[0].email commiter_realname = feed_entry.authors[0].email
except AttributeError: except AttributeError:
commiter_realname = "Unknown" commiter_realname = "Unknown"
try: try:
commiter = feed_entry.authors[0].href.replace('https://github.com/',"") commiter = feed_entry.authors[0].href.replace('https://github.com/',"")
except AttributeError: except AttributeError:
commiter = commiter_realname # This will only use the realname if the nickname couldn't be obtained commiter = commiter_realname # This will only use the realname if the nickname couldn't be obtained
reponame = url.replace("https://github.com/","").replace("/commits/master.atom","") reponame = url.replace("https://github.com/","").replace("/commits/master.atom","")
commit_hash = feed_entry.links[0].href.replace("https://github.com/" + reponame + "/commit/","")[:7] commit_hash = feed_entry.links[0].href.replace("https://github.com/" + reponame + "/commit/","")[:7]
commit_time = feed_entry.updated commit_time = feed_entry.updated
updcnt += 1 updcnt += 1
if rssnotify["dont_print_first_message"]: if rssnotify["dont_print_first_message"]:
continue continue
if rssnotify["show_commit_link"]: if rssnotify["show_commit_link"]:
if rssnotify["use_git.io"]: if rssnotify["use_git.io"]:
params = urllib.parse.urlencode({'url' : feed_entry.link}) # Side note: git.io only works with *.github.com links
# Side note: git.io only works with *.github.com links l, code = web.post("http://git.io/create", {'url': feed_entry.link})
u = urllib.urlopen("http://git.io/create", params) if code == 200:
l = u.read() l = str(l, 'utf-8')
if len(l.strip()) == 6: if len(l.strip()) == 6:
commit_link = "http://git.io/" + l commit_link = "http://git.io/" + l
else: else:
commit_link = feed_entry.link commit_link = feed_entry.link
else: else:
commit_link = feed_entry.link commit_link = feed_entry.link
else: else:
commit_link = "" commit_link = feed_entry.link
else:
commit_link = ""
chans = [] chans = []
if feeds[v][1] == '*': if feeds[v][1] == '*':
chans = phenny.bot.channels chans = phenny.bot.channels
elif type(feeds[v][1]) == type([]): elif type(feeds[v][1]) == type([]):
chans = feeds[v][1] chans = feeds[v][1]
else: else:
print("[RssNotify]: Something went wrong!") print("[RssNotify]: Something went wrong!")
for ch in chans: for ch in chans:
if commiter.lower() != commiter_realname.lower(): if commiter.lower() != commiter_realname.lower():
#phenny.say("GIT: %s (%s) commited to %s: %s %s %s" % (commiter,commiter_realname,reponame,feed_entry.title,commit_hash,commit_time)) #phenny.say("GIT: %s (%s) commited to %s: %s %s %s" % (commiter,commiter_realname,reponame,feed_entry.title,commit_hash,commit_time))
phenny.write(['PRIVMSG', ch],"GIT: %s (%s) commited to %s: %s %s %s %s" % (commiter, commiter_realname, reponame, feed_entry.title, commit_hash, commit_time, commit_link)) phenny.write(['PRIVMSG', ch],"GIT: %s (%s) commited to %s: %s %s %s %s" % (commiter, commiter_realname, reponame, feed_entry.title, commit_hash, commit_time, commit_link))
else: else:
#phenny.say("GIT: %s commited to %s: %s %s %s" % (commiter,reponame,feed_entry.title,commit_hash,commit_time)) #phenny.say("GIT: %s commited to %s: %s %s %s" % (commiter,reponame,feed_entry.title,commit_hash,commit_time))
phenny.write(['PRIVMSG', ch],"GIT: %s commited to %s: %s %s %s %s" % (commiter, reponame, feed_entry.title, commit_hash, commit_time, commit_link)) phenny.write(['PRIVMSG', ch],"GIT: %s commited to %s: %s %s %s %s" % (commiter, reponame, feed_entry.title, commit_hash, commit_time, commit_link))
if len(feed.entries) > 0: if len(feed.entries) > 0:
m = -1 m = -1
for i in range(0, len(feed.entries)): for i in range(0, len(feed.entries)):
if to_unix_time(feed.entries[i].updated) > m: if to_unix_time(feed.entries[i].updated) > m:
m = to_unix_time(feed.entries[i].updated) m = to_unix_time(feed.entries[i].updated)
rssnotify["last_updated_feeds"][feednum] = m rssnotify["last_updated_feeds"][feednum] = m
if updcnt > 0: if updcnt > 0:
print("[RssNotify]: Found %i RSS Update(s) for URL '%s'" % (updcnt, url)) print("[RssNotify]: Found %i RSS Update(s) for URL '%s'" % (updcnt, url))
end = time.time() end = time.time()
if rssnotify["dont_print_first_message"]: if rssnotify["dont_print_first_message"]:
rssnotify["dont_print_first_message"] = False rssnotify["dont_print_first_message"] = False
print("[RssNotify]: Checked " + str(len(feeds)) + " RSS Feeds in %0.3f seconds" % (end-start)) print("[RssNotify]: Checked " + str(len(feeds)) + " RSS Feeds in %0.3f seconds" % (end-start))
rsscheck.priority = 'medium' rsscheck.priority = 'medium'
rsscheck.rule = r'.*' rsscheck.rule = r'.*'
@ -128,4 +132,4 @@ rsscheck.event = '*'
rsscheck.nohook = True rsscheck.nohook = True
if __name__ == '__main__': if __name__ == '__main__':
print(__doc__.strip()) print(__doc__.strip())