view.py 1.5 KB

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