-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_data.py
More file actions
35 lines (30 loc) · 1.3 KB
/
Copy pathfetch_data.py
File metadata and controls
35 lines (30 loc) · 1.3 KB
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
#!/usr/bin/env python3
"""Builds data.json — a snapshot of quote/summary/news for the default tickers.
Run by the GitHub Action so the Pages demo has real data without a live server.
"""
import json, time, re, datetime
import yahoo
# keep in sync with DEFAULT in index.html
TICKERS = "GOOG,AAPL,MSFT,NVDA,AMZN,TSLA,UBER,SPCX,PLTR,NOW,TSM,QQQ,SPY,VRT,CRWV,LLY,CEG,VST,META,RKLB,QCOM,MU,INTC,NOK,FIG,GEV,GD,SPGI,V".split(",")
def jget(url):
try:
return json.loads(yahoo.get(url))
except Exception:
return {}
def main():
out = {"updated": datetime.datetime.now(datetime.timezone.utc).isoformat(timespec="minutes"), "stocks": {}}
for sym in TICKERS:
out["stocks"][sym] = {
"quote": jget(yahoo.chart_url(sym)),
"intraday": jget(yahoo.intraday_url(sym)),
"summary": json.loads(yahoo.summary_bytes(sym) or b"{}"),
"news": jget(yahoo.news_url(sym)),
}
time.sleep(0.3) # be polite to Yahoo
with open("data.json", "w", encoding="utf-8") as f:
json.dump(out, f, separators=(",", ":"))
ok = sum(1 for s in out["stocks"].values()
if s["quote"].get("chart", {}).get("result"))
print(f"wrote data.json — {ok}/{len(TICKERS)} quotes at {out['updated']}")
if __name__ == "__main__":
main()