data.py 2.8 KB

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