view.py 1.9 KB

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