You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
1.3 KiB

import websockets
import json
import yahoo_pb2
import base64
import asyncio
from utils import *
import requests
async def stream_ticker(ticker):
resp = requests.get("https://query1.finance.yahoo.com/v7/finance/spark?symbols="+ticker+"&range=1d&interval=5m&indicators=close&includeTimestamps=false&includePrePost=false&corsDomain=finance.yahoo.com&.tsrc=finance", headers={
"origin": "https://finance.yahoo.com",
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
})
data = resp.json()["spark"]["result"][0]["response"][0]["meta"]
yield cents(str(data["regularMarketPrice"]))
async with websockets.connect("wss://streamer.finance.yahoo.com/") as ws:
await ws.send(json.dumps({"subscribe": [ticker]}))
async for message in ws:
pricing_data = yahoo_pb2.PricingData()
pricing_data.ParseFromString(base64.b64decode(message))
yield cents(str(pricing_data.price))
if __name__=="__main__":
async def main():
async for p in stream_ticker("ESTH23.CME"):
print(p)
asyncio.run(main())
# asyncio.run(stream_ticker(["ES=F", "YM=F", "NQ=F", "RTY=F", "CL=F", "GC=F", "EST=F", "ESTZ22.CME"]))