streams.py 920 B

123456789101112131415161718192021222324252627282930313233343536
  1. #! /usr/bin/python3
  2. """
  3. Streams Cron
  4. """
  5. import time
  6. import sys
  7. import redis
  8. import requests
  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. twitch_api = "https://api.twitch.tv/kraken/streams/followed?oauth_token={0}".format(conf.twitch_token)
  15. r.zremrangebyscore("bot:twitch", "-inf", now-(60*15))
  16. t = requests.get(url=twitch_api).json()
  17. if "streams" not in t:
  18. print(t)
  19. sys.exit()
  20. for stream in t["streams"]:
  21. if r.zadd("bot:twitch", now, stream["channel"]["name"]) == 0:
  22. continue
  23. game = stream["channel"]["game"] if "game" in stream["channel"] else "ahora"
  24. wh.send("{3} **{0}** está stremeando {4}: [{2}](<{1}>)".format(stream["channel"]["name"], stream["channel"]["url"], stream["channel"]["status"], conf.icon_twitch, game))
  25. time.sleep(2)