discord-guild-notifications/rss.py

51 lines
1.3 KiB
Python
Raw Normal View History

2017-07-10 08:58:51 +00:00
#! /usr/bin/python3
2017-12-13 11:37:31 +00:00
"""
RSS news
"""
2017-07-10 08:58:51 +00:00
import time
import traceback
2017-12-13 11:37:31 +00:00
import feedparser
import redis
2017-07-10 08:58:51 +00:00
import webhook
import conf
now = time.time()
wh = webhook.Webhook(conf.url_discord_webhook_news)
2017-07-10 08:58:51 +00:00
r = redis.StrictRedis(host='localhost', charset="utf-8", decode_responses=True, db=1)
2017-07-10 08:58:51 +00:00
r.zremrangebyscore("bot:rss", "-inf", now-(60*60*24*30*3)) # 3 meses de caché
url_feeds = [
2017-12-13 11:37:31 +00:00
"http://www.wowhead.com/news&rss",
"http://www.mmo-champion.com/external.php?do=rss&type=newcontent&sectionid=1&days=120&count=5"
2017-07-10 08:58:51 +00:00
]
for url in url_feeds:
2017-12-13 11:37:31 +00:00
try:
feed = feedparser.parse(url)
if "items" not in feed or len(feed["items"]) < 3:
continue
for i in [0, 1, 2]: # last 3 news
entry = feed["items"][i]
fid = "{} {}".format(feed["feed"]["title"][0:20], entry["published"])
if r.zadd("bot:rss", now, fid) == 0:
continue
if "wowhead.com/" in entry["link"]:
icon = conf.icon_wowhead
elif "mmo-champion.com/" in entry["link"]:
icon = conf.icon_mmoc
else:
icon = ":newspaper2:"
wh.send("{2} [{0}](<{1}>)".format(entry["title"], entry["link"], icon))
time.sleep(2)
except:
print(url)
traceback.print_exc()