Skip to content

Commit

Permalink
experimental test to speed up backend
Browse files Browse the repository at this point in the history
  • Loading branch information
MuslemRahimi committed Feb 22, 2025
1 parent 50597ff commit a065888
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/cron_dividends.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ async def run():

for ticker in tqdm(total_symbols):
res = await get_data(ticker, con, etf_con, stock_symbols, etf_symbols)
print(res)

try:
if len(res.get('history', [])) > 0:
await save_as_json(ticker, res, 'json/dividends/companies')
Expand Down
38 changes: 37 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import requests
from pathlib import Path
import asyncio
import httpx

# Database related imports
import sqlite3
Expand All @@ -42,6 +43,7 @@
from utils.helper import load_latest_json

# DB constants & context manager
API_URL = "http://localhost:8000"

STOCK_DB = 'stocks'
ETF_DB = 'etf'
Expand Down Expand Up @@ -4256,11 +4258,45 @@ async def get_data(data:TickerData, api_key: str = Security(get_api_key)):
)


async def fetch_data(client, endpoint, ticker):
url = f"{API_URL}{endpoint}"
try:
response = await client.post(url, json={"ticker": ticker}, headers={"X-API-KEY": STOCKNEAR_API_KEY})
response.raise_for_status()
return {endpoint: response.json()}
except Exception as e:
return {endpoint: {"error": str(e)}}

@app.post("/stock-data")
async def get_stock_data(data:TickerData, api_key: str = Security(get_api_key)):
ticker = data.ticker.upper()
endpoints = [
"/stockdeck",
"/analyst-summary-rating",
"/stock-quote",
"/pre-post-quote",
"/wiim",
"/one-day-price",
"/next-earnings",
"/earnings-surprise",
"/stock-news",
]

async with httpx.AsyncClient() as client:
tasks = [fetch_data(client, endpoint, ticker) for endpoint in endpoints]
results = await asyncio.gather(*tasks)

# Combine results
data = {k: v for result in results for k, v in result.items()}
return data


@app.get("/newsletter")
async def get_newsletter():
try:
with open(f"json/newsletter/data.json", 'rb') as file:
res = orjson.loads(file.read())
except:
res = []
return res
return res

0 comments on commit a065888

Please sign in to comment.