-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
49 lines (40 loc) · 1.4 KB
/
Copy pathapp.py
File metadata and controls
49 lines (40 loc) · 1.4 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import asyncio
import aiohttp
# Top 10 US-listed companies by market capitalization.
SYMBOLS = [
"AAPL", # Apple
"MSFT", # Microsoft
"NVDA", # NVIDIA
"GOOGL", # Alphabet
"AMZN", # Amazon
"META", # Meta Platforms
"BRK-B", # Berkshire Hathaway
"TSLA", # Tesla
"AVGO", # Broadcom
"LLY", # Eli Lilly
]
CHART_URL = "https://query1.finance.yahoo.com/v8/finance/chart/{symbol}"
HEADERS = {"User-Agent": "Mozilla/5.0"}
async def fetch_price(session: aiohttp.ClientSession, symbol: str) -> dict:
"""Fetch the latest price for a single symbol."""
async with session.get(CHART_URL.format(symbol=symbol)) as response:
response.raise_for_status()
data = await response.json()
meta = data["chart"]["result"][0]["meta"]
return {
"symbol": symbol,
"price": meta["regularMarketPrice"],
"currency": meta["currency"],
}
async def main() -> None:
async with aiohttp.ClientSession(headers=HEADERS) as session:
# Fetch all symbols concurrently.
quotes = await asyncio.gather(
*(fetch_price(session, symbol) for symbol in SYMBOLS)
)
print(f"{'Symbol':<8}{'Price':>12} Currency")
print("-" * 30)
for quote in quotes:
print(f"{quote['symbol']:<8}{quote['price']:>12,.2f} {quote['currency']}")
if __name__ == "__main__":
asyncio.run(main())