view.py 1.4 KB

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