view.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. load_dotenv()
  11. webhooks_json = os.environ["DISCORD_TOKENS"]
  12. webhooks = json.loads(webhooks_json)
  13. name = status['name']
  14. its_id = status['id']
  15. status_text = f"[{name} [{its_id}]](https://www.torn.com/profiles.php?XID={its_id}) → " + \
  16. "Loot level changed to "+roman.toRoman(status['integer'])
  17. status_text += "\n" + status['time_string']
  18. this_json = {'content': status_text}
  19. pprint(this_json)
  20. _send(webhooks, this_json)
  21. def send_alert(this_alert):
  22. load_dotenv()
  23. webhooks_json = os.environ["DISCORD_TOKENS"]
  24. webhooks = json.loads(webhooks_json)
  25. this_json = {'content': this_alert}
  26. _send(webhooks, this_json)
  27. def _send(send_webhooks, send_json):
  28. for webhook in send_webhooks:
  29. url = base_url+webhook
  30. response = requests.post(url, json=send_json)
  31. if response.status_code in [200, 204]:
  32. print("Webhook executed")
  33. else:
  34. print("status code {}: {}".format(response.status_code, response.content.decode("utf-8")))
  35. if response.status_code == int(429):
  36. sys.exit(1)