discord-guild-notifications/loot.py

63 lines
1.7 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
"""
Loot notifications
"""
2017-07-10 08:58:51 +00:00
import time
import json
import traceback
import os
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()
wh = webhook.Webhook(conf.url_discord_webhook_guild)
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
bnet_member = "https://{1}.api.battle.net/wow/character/dun%20modr/{0}?fields=feed,items&locale=es_ES&apikey={2}"
# legends database
items = {}
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "items_legend.json"), 'r') as f:
2017-12-13 11:37:31 +00:00
js = json.load(f)
2017-07-10 08:58:51 +00:00
2017-12-13 11:37:31 +00:00
for item in js:
items[item["item_id"]] = item
2017-07-10 08:58:51 +00:00
members = r.smembers("bot:members") # i need all guild members
request = requests.Session()
for m in members:
2017-12-13 11:37:31 +00:00
try:
member = request.get(url=bnet_member.format(m, conf.battle_net_region, conf.battle_net_apikey)).json()
2017-07-10 08:58:51 +00:00
2017-12-13 11:37:31 +00:00
if "feed" not in member:
continue
2017-12-13 11:37:31 +00:00
for feed in member["feed"]:
if feed["type"] != "LOOT":
continue
2017-12-13 11:37:31 +00:00
fid = "{}-{}".format(m, feed["itemId"])
2017-07-10 08:58:51 +00:00
2017-12-13 11:37:31 +00:00
if feed["itemId"] in items and r.sadd("bot:legends", fid):
wh.send(":tangerine: **{0}** pilla legendario! **[{1}](<http://es.wowhead.com/item={2}>)**".format(member["name"], items[feed["itemId"]]["name_eses"], feed["itemId"]))
time.sleep(2)
2017-07-10 08:58:51 +00:00
2017-12-13 11:37:31 +00:00
except:
traceback.print_exc()
2017-07-10 08:58:51 +00:00
"""
Vamos a generar el archivo items_legend.json
Sacar la lista de legends de aquí: view-source:http://es.wowhead.com/items/quality:5/slot:16:5:8:11:10:1:7:2:3:12:6:9?filter=166;7;0
Buscar: 'var _ = {};' y copiar toda la línea siguiente
Reemplazar: '_\[(\d+)\]=\{' por '\n{"item_id": \1, '
Reemplazar: ';$' por ','
Añadir los [ ... ]
"""