data.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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)
  41. if timestamp_latest == int(timestamp_stored):
  42. # print(timestamp_latest)
  43. # Exit when there is nothing to do
  44. sys.exit()
  45. with open('timestamp.txt', 'w') as f:
  46. f.write("{}".format(timestamp_latest))
  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()