Skip to content

Commit f519f61

Browse files
Merge pull request #6 from kushagra21-afk/master
fixing rate limiting concerns with the api calls
2 parents 31aa8d8 + f646d4b commit f519f61

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

app.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
raise ValueError("Please provide a GROQ API key")
1919

2020
app = FastAPI()
21-
# Web searching agent
2221
app.add_middleware(
2322
CORSMiddleware,
2423
allow_origins=["*"],
@@ -29,10 +28,14 @@
2928

3029
@app.get("/top-stocks")
3130
async def read_top_stocks():
32-
top_stocks = ['AAPL', 'GOOGL', 'MSFT', 'AMZN', 'TSLA']
33-
stock_info = get_top_stocks(top_stocks)
31+
top_stocks = ['AAPL', 'MSFT', 'AMZN', 'GOOGL']
32+
stock = " ".join(top_stocks)
33+
stock_info = get_top_stocks(stock)
3434
return stock_info
3535

36+
@app.get("/")
37+
def read_root():
38+
return {"message": "Welcome to Investo!"}
3639

3740
@app.get("/chat")
3841
def chat(query: str):

topStocks.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
import yfinance as yf
2+
import requests
3+
import time
4+
session = requests.Session()
5+
session.headers.update({
6+
"User-Agent": "Chrome/122.0.0.0"
7+
})
28

39
def get_top_stocks(symbols):
410
stock_data = []
5-
for symbol in symbols:
6-
try:
7-
ticker = yf.Ticker(symbol)
8-
info = ticker.info
11+
try:
12+
top_stocks = symbols.split()
13+
tickers = yf.Tickers(symbols)
14+
while top_stocks:
15+
stock = top_stocks.pop()
16+
info = tickers.tickers[stock].info
917
stock_info = {
10-
'symbol': symbol,
11-
'name': info.get('shortName', 'N/A'),
12-
'currentPrice': info.get('currentPrice', 'N/A'),
13-
'previousClose': info.get('previousClose', 'N/A'),
14-
'sector': info.get('sector', 'N/A')
15-
}
16-
stock_data.append(stock_info)
17-
except Exception as e:
18-
print(f"Error fetching {symbol}: {e}")
18+
'symbol': stock,
19+
'name': info.get('shortName', 'N/A'),
20+
'currentPrice': info.get('currentPrice', 'N/A'),
21+
'previousClose': info.get('previousClose', 'N/A'),
22+
'sector': info.get('sector', 'N/A')
23+
}
24+
stock_data.push(stock_info)
25+
except Exception as e:
26+
print(f"Error fetching {symbols}: {e}")
27+
time.sleep(5)
1928
return stock_data

0 commit comments

Comments
 (0)