From a065888cb68659e436060cdb29766da309b529f1 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Sat, 22 Feb 2025 21:13:49 +0100 Subject: [PATCH] experimental test to speed up backend --- app/cron_dividends.py | 2 +- app/main.py | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/app/cron_dividends.py b/app/cron_dividends.py index b567e74..8a3a763 100644 --- a/app/cron_dividends.py +++ b/app/cron_dividends.py @@ -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') diff --git a/app/main.py b/app/main.py index a5cdcd9..921fc0a 100755 --- a/app/main.py +++ b/app/main.py @@ -19,6 +19,7 @@ import requests from pathlib import Path import asyncio +import httpx # Database related imports import sqlite3 @@ -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' @@ -4256,6 +4258,39 @@ 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: @@ -4263,4 +4298,5 @@ async def get_newsletter(): res = orjson.loads(file.read()) except: res = [] - return res \ No newline at end of file + return res +