data.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import database
  2. import psycopg2.extras
  3. import stocks
  4. from dotenv import load_dotenv
  5. import os
  6. import sys
  7. from pprint import pprint
  8. from datetime import datetime
  9. from urllib.parse import urlencode
  10. # from urllib.request import Request, urlopen
  11. import requests
  12. def broadcast(name, timestamp, price, quantity, this_drop):
  13. # development only
  14. load_dotenv()
  15. url = os.environ["BROADCAST_URL"]
  16. # Create the string
  17. time = datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
  18. print(time)
  19. this_drop_decimal = float(this_drop / 1e9)
  20. string = "{}: {} dropped {} shares at ${:.2f} for a grand total of ${:.1f}B".format(
  21. time, name, quantity, price, this_drop_decimal)
  22. json = {'content': string}
  23. print(urlencode(json).encode())
  24. response = requests.post(url, json=json)
  25. if response.status_code in [200, 204]:
  26. print("Webhook executed")
  27. else:
  28. print("status code {}: {}".format(response.status_code, response.content.decode("utf-8")))
  29. # Retrieve last and second to last timestamp from the serverS
  30. db = database.db
  31. cursor = db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
  32. query = """SELECT DISTINCT timestamp FROM stocks ORDER BY stocks.timestamp DESC LIMIT 1 OFFSET 1;"""
  33. cursor.execute(query)
  34. res = cursor.fetchone()
  35. timestamp_previous = res.timestamp
  36. query = """SELECT DISTINCT timestamp FROM stocks ORDER BY stocks.timestamp DESC LIMIT 1;"""
  37. cursor.execute(query)
  38. res = cursor.fetchone()
  39. timestamp_latest = res.timestamp
  40. with open('timestamp.txt', 'w+') as f:
  41. timestamp_stored = f.read() or 0
  42. if timestamp_latest != timestamp_stored:
  43. f.write("{}".format(timestamp_latest))
  44. else:
  45. # Exit when there is nothing to do
  46. sys.exit()
  47. print(timestamp_previous)
  48. print(timestamp_latest)
  49. for stock in stocks.stocks:
  50. query = """SELECT current_price, available_shares FROM stocks WHERE stock_id = %s AND timestamp = %s"""
  51. cursor.execute(query, (stock[1], timestamp_latest))
  52. data_latest = cursor.fetchone()
  53. cursor.execute(query, (stock[1], timestamp_previous))
  54. data_previous = cursor.fetchone()
  55. pprint(data_latest)
  56. pprint(data_previous)
  57. # Calculations
  58. available_latest = int(data_latest.available_shares * data_latest.current_price)
  59. available_previous = int(data_previous.available_shares * data_previous.current_price)
  60. print(available_latest)
  61. print(available_previous)
  62. drop = available_latest - available_previous
  63. # broadcast(stock[0], timestamp_latest, data_latest.current_price, data_latest.available_shares, drop)
  64. if drop > stock[3] * 1e9:
  65. broadcast(stock[0], timestamp_latest, data_latest.current_price, data_latest.available_shares, drop)
  66. cursor.close()
  67. db.close()