-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracking.py
47 lines (39 loc) · 1.74 KB
/
tracking.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import requests
from db import INSERT, GET
from util import progressbar
from datetime import datetime
SYMBOLS = GET.tickers()
LAST_TRACKING_DATE = str(GET.last_tracking_date()[0])[:10]
def get_data(url: str) -> list:
res = requests.get(url)
return res.json()
def check_timing() -> bool:
now = str(datetime.now())[:10]
return LAST_TRACKING_DATE == now
def main(url: str, exchange: str) -> None:
if check_timing(): return
json = get_data(url)
progressbar(0, len(json), f"Inserting trackings from {exchange}: ")
for i, object in enumerate(json):
if object["symbol"] not in SYMBOLS:
INSERT.stock(
object["symbol"],
object["name"],
exchange
)
INSERT.tracking(
symbol=object["symbol"],
last_price=float(object["lastsale"][1:]) if len(object["lastsale"][1:]) else None,
volume=int(object["volume"]) if len(object["volume"]) else None,
marketcap=int(float(object["marketCap"])) if len(object["marketCap"]) else None,
price_change_pct=float(object["pctchange"][:-1]) if len(object["pctchange"][:-1]) else None
)
progressbar(i + 1, len(json), None)
if __name__ == "__main__":
stock_data = [
{ "url": "https://raw.githubusercontent.com/rreichel3/US-Stock-Symbols/main/amex/amex_full_tickers.json", "exchange": "amex" },
{ "url": "https://raw.githubusercontent.com/rreichel3/US-Stock-Symbols/main/nasdaq/nasdaq_full_tickers.json", "exchange": "nasdaq" },
{ "url": "https://raw.githubusercontent.com/rreichel3/US-Stock-Symbols/main/nyse/nyse_full_tickers.json", "exchange": "nyse"}
]
for data in stock_data:
main(data["url"], data["exchange"])