guild.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #! /usr/bin/python3
  2. import time
  3. import sys
  4. import redis
  5. import requests
  6. import webhook
  7. import conf
  8. now = time.time()
  9. wh = webhook.Webhook(conf.url_discord_webhook_guild)
  10. r = redis.StrictRedis(host='localhost', charset="utf-8", decode_responses=True, db=1)
  11. bnet_achs = conf.battle_net_url("https://{0}.api.battle.net/wow/data/guild/achievements?locale=es_ES&apikey={1}")
  12. bnet_guild = conf.battle_net_url("https://{0}.api.battle.net/wow/guild/dun%20modr/mirrors?fields=news,members,achievements&locale=es_ES&apikey={1}")
  13. a = requests.get(url=bnet_achs).json()
  14. g = requests.get(url=bnet_guild).json()
  15. if "members" not in g:
  16. print("'members' not in g")
  17. sys.exit()
  18. members = r.smembers("bot:members") # members database, used by other scripts
  19. chars = set()
  20. # who join the guild, who leaves
  21. for member in g["members"]:
  22. chars.add(member["character"]["name"])
  23. for new in chars.difference(members):
  24. r.sadd("bot:members", new)
  25. members.add(new)
  26. wh.send(":inbox_tray: **[{0}](<https://www.wowprogress.com/character/eu/dun-modr/{0}>)** ha entrado a la guild! 🎉".format(new))
  27. time.sleep(2)
  28. for kick in members.difference(chars):
  29. r.srem("bot:members", kick)
  30. wh.send(":outbox_tray: **[{0}](<https://www.wowprogress.com/character/eu/dun-modr/{0}>)** ha salido a la guild :confused:".format(kick))
  31. time.sleep(2)
  32. if "news" not in g:
  33. print("'news' not in g")
  34. sys.exit()
  35. g["news"].reverse()
  36. r.zremrangebyscore("bot:guild", "-inf", now-(60*60*24*2))
  37. for news in g["news"]:
  38. fid = None
  39. push = None
  40. if news["timestamp"]/1000 < now-(60*60*24): # oooold, check 1 day only
  41. continue
  42. if news["type"] in ["playerAchievement", "guildAchievement"]:
  43. fid = "{} {} {}".format(news["type"], news["character"], news["achievement"]["title"])
  44. push = ":medal: **{0}** gana el logro **{1}**!".format(news["character"], news["achievement"]["title"])
  45. if fid is None or r.zadd("bot:guild", now, fid) == 0 or push is None:
  46. continue
  47. wh.send(push)
  48. time.sleep(2) # prevent rate limit, for example with boss FK
  49. if "achievements" not in g:
  50. print("'achievements' not in g")
  51. sys.exit()
  52. def ach_to_list(data):
  53. ret = []
  54. for d in data:
  55. if "categories" in d:
  56. ret.extend(ach_to_list(d["categories"]))
  57. elif "achievements" in d:
  58. ret.extend(ach_to_list(d["achievements"]))
  59. else:
  60. ret.append(d)
  61. return ret
  62. a = ach_to_list(a["achievements"])
  63. achievements = [int(x) for x in r.smembers("bot:guild-ach")]
  64. for new in set(g["achievements"]["achievementsCompleted"]).difference(achievements):
  65. r.sadd("bot:guild-ach", new)
  66. ach = next((item for item in a if item["id"] == new), None)
  67. if not ach or "title" not in ach:
  68. continue
  69. title = ach["title"]
  70. desc = ach["description"]
  71. url = "http://es.wowhead.com/achievement={0}".format(new)
  72. icon = "https://wow.zamimg.com/images/wow/icons/large/{0}.jpg".format(ach["icon"])
  73. wh.clear_embeds()
  74. wh.add_embed(webhook.embed(title=ach["title"], url="http://es.wowhead.com/achievement={0}".format(new), description=ach["description"], thumbnail=webhook.image(icon)))
  75. wh.send(":clap: La guild ha ganado un logro!")
  76. time.sleep(2)