view.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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, stock_id=""):
  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. :param stock_id:
  19. :return:
  20. """
  21. this_time = datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M')
  22. this_drop_decimal = quantity * price / 1e9
  23. npc = ''
  24. if npc_drop:
  25. npc = '*NPC* '
  26. name = ('<@!'+stock_id+'>' if stock_id else name)
  27. this_message = "{}: {}{} dropped {:,} shares at ${:,.2f} for a grand total of ${:,.1f}B [{}]".format(
  28. this_time, npc, name, quantity, price, this_drop_decimal, current_quantity)
  29. if forecast_previous and forecast_latest:
  30. this_message = "{}: {} changed from {} to {} with {:,} shares available at ${:,.2f}".format(
  31. this_time, name, forecast_previous, forecast_latest, current_quantity, price)
  32. return this_message
  33. def broadcast(this_message):
  34. """
  35. :param this_message:
  36. :return:
  37. """
  38. # development only
  39. # load_dotenv()
  40. # url = os.environ["BROADCAST_URL"]
  41. webhooks_json = os.environ["DISCORD_TOKENS"]
  42. webhooks = json.loads(webhooks_json)
  43. content = {'content': this_message}
  44. for webhook_key in webhooks:
  45. webhook = webhooks[webhook_key]
  46. url = base_url + webhook
  47. response = requests.post(url, json=content)
  48. if response.status_code in [200, 204]:
  49. print("Webhook executed")
  50. else:
  51. print("status code {}: {}".format(response.status_code, response.content.decode("utf-8")))