view.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import os
  2. from datetime import datetime
  3. import requests
  4. import json
  5. base_url = 'https://discordapp.com/api/webhooks/'
  6. def create_message(name, timestamp, price, quantity, current_quantity=0, forecast_previous="",
  7. forecast_latest="", npc_drop=False):
  8. """
  9. Creates the string to be broadcast
  10. :param name:
  11. :param timestamp:
  12. :param price:
  13. :param quantity:
  14. :param current_quantity:
  15. :param forecast_previous:
  16. :param forecast_latest:
  17. :param npc_drop:
  18. :return:
  19. """
  20. this_time = datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M')
  21. this_drop_decimal = quantity * price / 1e9
  22. npc = ''
  23. if npc_drop:
  24. npc = '*NPC* '
  25. this_message = "{}: {}{} dropped {:,} shares at ${:,.2f} for a grand total of ${:,.1f}B [{}]".format(
  26. this_time, npc, name, quantity, price, this_drop_decimal, current_quantity)
  27. if forecast_previous and forecast_latest:
  28. this_message = "{}: {} changed from {} to {} with {:,} shares available at ${:,.2f}".format(
  29. this_time, name, forecast_previous, forecast_latest, current_quantity, price)
  30. return this_message
  31. def broadcast(this_message):
  32. """
  33. :param this_message:
  34. :return:
  35. """
  36. # development only
  37. # load_dotenv()
  38. # url = os.environ["BROADCAST_URL"]
  39. webhooks_json = os.environ["DISCORD_TOKENS"]
  40. webhooks = json.loads(webhooks_json)
  41. content = {'content': this_message}
  42. for webhook_key in webhooks:
  43. webhook = webhooks[webhook_key]
  44. url = base_url + webhook
  45. response = requests.post(url, json=content)
  46. if response.status_code in [200, 204]:
  47. print("Webhook executed")
  48. else:
  49. print("status code {}: {}".format(response.status_code, response.content.decode("utf-8")))