mythics.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #! /usr/bin/python3
  2. import redis
  3. import time
  4. import requests
  5. import traceback
  6. import datetime
  7. from lxml import html
  8. from lxml import etree
  9. import webhook
  10. import conf
  11. now = time.time()
  12. wh = webhook.Webhook(conf.url_discord_webhook_guild)
  13. r = redis.StrictRedis(host='localhost', charset="utf-8", decode_responses=True, db=1)
  14. warcraft_api = "https://worldofwarcraft.com/es-es/game/pve/leaderboards/dun-modr/{}"
  15. members = r.smembers("bot:members")
  16. r.zremrangebyscore("bot:m+", "-inf", now-(60*60*24*7*2)) # 2 semanas
  17. for zone, instance in conf.zones.items():
  18. try:
  19. page = requests.get(warcraft_api.format(zone))
  20. hp = etree.HTMLParser(encoding=page.encoding)
  21. tree = html.fromstring(page.content, parser=hp) # .decode(page.encoding)
  22. top = tree.xpath('//div[@class="Media-text"]/div[@class="font-semp-medium-white"]/text()')
  23. if len(top) < 3:
  24. print("no affix?")
  25. continue
  26. affix = "" if top[0] not in conf.affixes else conf.affixes[top[0]]
  27. affix += "" if top[1] not in conf.affixes else conf.affixes[top[1]]
  28. affix += "" if top[2] not in conf.affixes else conf.affixes[top[2]]
  29. rows = tree.xpath('//div[@class="SortTable-body"]/div[@class="SortTable-row"]')
  30. #print("rows", len(rows))
  31. for row in rows:
  32. #print("tiempo", row.xpath('./div[5]/text()'))
  33. #print("row", html.tostring(row))
  34. pos = row.xpath('./div[1]/text()')[0]
  35. lvl = int(row.xpath('./div[2]/text()')[0])
  36. record = row.xpath('./div[3]/text()')[0]
  37. datee = row.xpath('./div[5]/text()')[0] #row.xpath('./div[5]/@data-value')[0]
  38. team = row.xpath('./div[4]/div/div[@class="List-item gutter-tiny"]')
  39. #print("row", pos, lvl, time, datee, len(team))
  40. k = "{0}.{1}.{2}.{3}".format(zone, lvl, record, datee)
  41. inguild = False
  42. party = []
  43. if lvl < conf.min_mythic or r.zadd("bot:m+", now, k) == 0:
  44. continue
  45. for player in team:
  46. #print("player", html.tostring(player))
  47. name = player.xpath('.//div[@class="Character-name"]/text()')[0]
  48. url = str(player.xpath('./a/@href')[0])
  49. clss = str(player.xpath('./a/@class')[0])
  50. spec = ""
  51. for key in conf.class_icons.keys():
  52. if key in clss:
  53. spec = conf.class_icons[key]
  54. #party.append("{2}{0}{1}".format(name, "" if "dun-modr" in url else " *({})*".format(url.split("/")[6]), roles[len(party)]))
  55. party.append("{1} {0}".format(name if "dun-modr" in url else "*{}*".format(name), spec))
  56. for member in members:
  57. if "/{}".format(member).lower() in url and "dun-modr" in url:
  58. inguild = True
  59. if inguild:
  60. chests = 0
  61. for timer in conf.timers[zone]:
  62. delta = datetime.datetime.strptime(record, '%H:%M:%S') - datetime.datetime.strptime(timer, '%M:%S')
  63. if delta.total_seconds() > 0:
  64. break
  65. chests += 1
  66. if chests == 3:
  67. delta = datetime.datetime.strptime(timer, '%M:%S') - datetime.datetime.strptime(record, '%H:%M:%S')
  68. #tip = "(piedra +{0} por {1})".format(chests, delta)
  69. tip = "(piedra +{0})".format(chests, delta)
  70. msg = ":mega: **[{0}](<{5}>) +{1}** hecha en **{2}** {3}, **rank {4}** de Dun Modr".format(instance, lvl, record, tip, pos, warcraft_api.format(zone))
  71. #print(msg)
  72. wh.clear_embeds()
  73. wh.add_embed(webhook.embed(title=", ".join(party)))
  74. wh.send(msg)
  75. except:
  76. traceback.print_exc()
  77. time.sleep(1)