view.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import os
  2. import sys
  3. from pprint import pprint
  4. import json
  5. from dotenv import load_dotenv
  6. import roman
  7. import requests
  8. base_url = 'https://discordapp.com/api/webhooks/'
  9. def send_message(status):
  10. name = status['name']
  11. its_id = status['id']
  12. status_text = f"[{name} [{its_id}]](https://www.torn.com/profiles.php?XID={its_id}) → " + \
  13. "Loot level changed to "+roman.toRoman(status['integer'])
  14. status_text += "\n" + status['time_string']
  15. this_json = {"content": status_text}
  16. pprint(this_json)
  17. _send(this_json)
  18. def send_alert(this_alert):
  19. this_json = {"content": this_alert}
  20. _send(this_json)
  21. def _send(send_json):
  22. load_dotenv()
  23. webhooks_json = os.environ["DISCORD_TOKENS"]
  24. webhooks_json = webhooks_json.replace("'", '"')
  25. print(webhooks_json)
  26. webhooks = json.loads(webhooks_json)
  27. for webhook_key in webhooks:
  28. webhook = webhooks[webhook_key]
  29. if 'Alert: 10 mins' in send_json['content'] and webhook_key == 'Unrelenting':
  30. send_json["content"] = "@here " + send_json['content']
  31. pprint(send_json)
  32. url = base_url+webhook
  33. response = requests.post(url, json=send_json)
  34. if response.status_code in [200, 204]:
  35. print("Webhook executed")
  36. else:
  37. print("status code {}: {}".format(response.status_code, response.content.decode("utf-8")))
  38. if response.status_code == int(429):
  39. sys.exit(1)