rss.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #! /usr/bin/python3
  2. """
  3. RSS news
  4. """
  5. import time
  6. import traceback
  7. import feedparser
  8. import redis
  9. import webhook
  10. import conf
  11. now = time.time()
  12. wh = webhook.Webhook(conf.url_discord_webhook_news)
  13. r = redis.StrictRedis(host='localhost', charset="utf-8", decode_responses=True, db=1)
  14. r.zremrangebyscore("bot:rss", "-inf", now-(60*60*24*30*3)) # 3 meses de caché
  15. url_feeds = [
  16. "http://www.wowhead.com/news&rss",
  17. "http://www.mmo-champion.com/external.php?do=rss&type=newcontent&sectionid=1&days=120&count=5"
  18. ]
  19. for url in url_feeds:
  20. try:
  21. feed = feedparser.parse(url)
  22. if "items" not in feed or len(feed["items"]) < 3:
  23. continue
  24. for i in [0, 1, 2]: # last 3 news
  25. entry = feed["items"][i]
  26. fid = "{} {}".format(feed["feed"]["title"][0:20], entry["published"])
  27. if r.zadd("bot:rss", now, fid) == 0:
  28. continue
  29. if "wowhead.com/" in entry["link"]:
  30. icon = conf.icon_wowhead
  31. elif "mmo-champion.com/" in entry["link"]:
  32. icon = conf.icon_mmoc
  33. else:
  34. icon = ":newspaper2:"
  35. wh.send("{2} [{0}](<{1}>)".format(entry["title"], entry["link"], icon))
  36. time.sleep(2)
  37. except:
  38. print(url)
  39. traceback.print_exc()