2017-07-10 08:58:51 +00:00
|
|
|
#! /usr/bin/python3
|
|
|
|
|
|
|
|
import time
|
|
|
|
import sys
|
2017-12-13 11:37:31 +00:00
|
|
|
import redis
|
|
|
|
import requests
|
2017-07-10 08:58:51 +00:00
|
|
|
import webhook
|
|
|
|
import conf
|
|
|
|
|
|
|
|
now = time.time()
|
|
|
|
|
2017-08-31 15:21:23 +00:00
|
|
|
wh = webhook.Webhook(conf.url_discord_webhook_guild)
|
2017-07-10 08:58:51 +00:00
|
|
|
|
2017-08-31 15:21:23 +00:00
|
|
|
r = redis.StrictRedis(host='localhost', charset="utf-8", decode_responses=True, db=1)
|
2017-07-10 08:58:51 +00:00
|
|
|
|
2017-07-20 10:58:54 +00:00
|
|
|
bnet_achs = conf.battle_net_url("https://{0}.api.battle.net/wow/data/guild/achievements?locale=es_ES&apikey={1}")
|
|
|
|
|
2017-12-12 12:54:43 +00:00
|
|
|
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}")
|
2017-07-10 08:58:51 +00:00
|
|
|
|
2017-07-20 10:58:54 +00:00
|
|
|
a = requests.get(url=bnet_achs).json()
|
2017-07-10 08:58:51 +00:00
|
|
|
g = requests.get(url=bnet_guild).json()
|
|
|
|
|
|
|
|
|
2017-07-20 10:58:54 +00:00
|
|
|
|
2017-07-10 08:58:51 +00:00
|
|
|
if "members" not in g:
|
2017-12-13 11:37:31 +00:00
|
|
|
print("'members' not in g")
|
|
|
|
sys.exit()
|
2017-07-10 08:58:51 +00:00
|
|
|
|
|
|
|
members = r.smembers("bot:members") # members database, used by other scripts
|
|
|
|
chars = set()
|
|
|
|
|
2017-07-20 10:58:54 +00:00
|
|
|
# who join the guild, who leaves
|
2017-07-10 08:58:51 +00:00
|
|
|
|
|
|
|
for member in g["members"]:
|
2017-12-13 11:37:31 +00:00
|
|
|
chars.add(member["character"]["name"])
|
2017-07-10 08:58:51 +00:00
|
|
|
|
|
|
|
for new in chars.difference(members):
|
2017-12-13 11:37:31 +00:00
|
|
|
r.sadd("bot:members", new)
|
|
|
|
members.add(new)
|
|
|
|
wh.send(":inbox_tray: **[{0}](<https://www.wowprogress.com/character/eu/dun-modr/{0}>)** ha entrado a la guild! 🎉".format(new))
|
|
|
|
time.sleep(2)
|
2017-07-10 08:58:51 +00:00
|
|
|
|
|
|
|
for kick in members.difference(chars):
|
2017-12-13 11:37:31 +00:00
|
|
|
r.srem("bot:members", kick)
|
|
|
|
wh.send(":outbox_tray: **[{0}](<https://www.wowprogress.com/character/eu/dun-modr/{0}>)** ha salido a la guild :confused:".format(kick))
|
|
|
|
time.sleep(2)
|
2017-07-10 08:58:51 +00:00
|
|
|
|
|
|
|
|
2017-07-20 10:58:54 +00:00
|
|
|
|
2017-07-10 08:58:51 +00:00
|
|
|
if "news" not in g:
|
2017-12-13 11:37:31 +00:00
|
|
|
print("'news' not in g")
|
|
|
|
sys.exit()
|
2017-07-10 08:58:51 +00:00
|
|
|
|
|
|
|
g["news"].reverse()
|
|
|
|
r.zremrangebyscore("bot:guild", "-inf", now-(60*60*24*2))
|
|
|
|
|
|
|
|
for news in g["news"]:
|
2017-12-13 11:37:31 +00:00
|
|
|
fid = None
|
|
|
|
push = None
|
2017-07-10 08:58:51 +00:00
|
|
|
|
2017-12-13 11:37:31 +00:00
|
|
|
if news["timestamp"]/1000 < now-(60*60*24): # oooold, check 1 day only
|
|
|
|
continue
|
2017-07-10 08:58:51 +00:00
|
|
|
|
2017-12-13 11:37:31 +00:00
|
|
|
if news["type"] in ["playerAchievement", "guildAchievement"]:
|
|
|
|
fid = "{} {} {}".format(news["type"], news["character"], news["achievement"]["title"])
|
|
|
|
push = ":medal: **{0}** gana el logro **{1}**!".format(news["character"], news["achievement"]["title"])
|
2017-07-10 08:58:51 +00:00
|
|
|
|
2017-12-13 11:37:31 +00:00
|
|
|
if fid is None or r.zadd("bot:guild", now, fid) == 0 or push is None:
|
|
|
|
continue
|
2017-07-10 08:58:51 +00:00
|
|
|
|
2017-12-13 11:37:31 +00:00
|
|
|
wh.send(push)
|
|
|
|
time.sleep(2) # prevent rate limit, for example with boss FK
|
2017-07-20 10:58:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if "achievements" not in g:
|
2017-12-13 11:37:31 +00:00
|
|
|
print("'achievements' not in g")
|
|
|
|
sys.exit()
|
2017-07-20 10:58:54 +00:00
|
|
|
|
|
|
|
def ach_to_list(data):
|
2017-12-13 11:37:31 +00:00
|
|
|
ret = []
|
2017-07-20 10:58:54 +00:00
|
|
|
|
2017-12-13 11:37:31 +00:00
|
|
|
for d in data:
|
|
|
|
if "categories" in d:
|
|
|
|
ret.extend(ach_to_list(d["categories"]))
|
2017-07-20 10:58:54 +00:00
|
|
|
|
2017-12-13 11:37:31 +00:00
|
|
|
elif "achievements" in d:
|
|
|
|
ret.extend(ach_to_list(d["achievements"]))
|
2017-07-20 10:58:54 +00:00
|
|
|
|
2017-12-13 11:37:31 +00:00
|
|
|
else:
|
|
|
|
ret.append(d)
|
2017-07-20 10:58:54 +00:00
|
|
|
|
2017-12-13 11:37:31 +00:00
|
|
|
return ret
|
2017-07-20 10:58:54 +00:00
|
|
|
|
|
|
|
a = ach_to_list(a["achievements"])
|
|
|
|
|
|
|
|
achievements = [int(x) for x in r.smembers("bot:guild-ach")]
|
|
|
|
|
|
|
|
for new in set(g["achievements"]["achievementsCompleted"]).difference(achievements):
|
2017-12-13 11:37:31 +00:00
|
|
|
r.sadd("bot:guild-ach", new)
|
2017-07-20 10:58:54 +00:00
|
|
|
|
2017-12-13 11:37:31 +00:00
|
|
|
ach = next((item for item in a if item["id"] == new), None)
|
|
|
|
if not ach or "title" not in ach:
|
|
|
|
continue
|
2017-07-20 10:58:54 +00:00
|
|
|
|
2017-12-13 11:37:31 +00:00
|
|
|
title = ach["title"]
|
|
|
|
desc = ach["description"]
|
|
|
|
url = "http://es.wowhead.com/achievement={0}".format(new)
|
|
|
|
icon = "https://wow.zamimg.com/images/wow/icons/large/{0}.jpg".format(ach["icon"])
|
2017-07-20 10:58:54 +00:00
|
|
|
|
2017-12-13 11:37:31 +00:00
|
|
|
wh.clear_embeds()
|
|
|
|
wh.add_embed(webhook.embed(title=ach["title"], url="http://es.wowhead.com/achievement={0}".format(new), description=ach["description"], thumbnail=webhook.image(icon)))
|
|
|
|
wh.send(":clap: La guild ha ganado un logro!")
|
2017-07-20 10:58:54 +00:00
|
|
|
|
2017-12-13 11:37:31 +00:00
|
|
|
time.sleep(2)
|