60 lines
2.1 KiB
Python
60 lines
2.1 KiB
Python
from fastapi import APIRouter, Depends
|
|
from app.api.auth import get_current_user
|
|
from app.services.stock_engine import (
|
|
get_scan, get_ribbon, get_smartmoney, get_trend, get_marketintel
|
|
)
|
|
|
|
import requests
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/scan")
|
|
def scan(current_user = Depends(get_current_user)):
|
|
return get_scan(current_user.sector_map or {})
|
|
|
|
@router.get("/ribbon")
|
|
def ribbon(current_user = Depends(get_current_user)):
|
|
return get_ribbon(current_user.sector_map or {})
|
|
|
|
@router.get("/smartmoney")
|
|
def smartmoney(current_user = Depends(get_current_user)):
|
|
return get_smartmoney(current_user.sector_map or {})
|
|
|
|
@router.get("/trend")
|
|
def trend(current_user = Depends(get_current_user)):
|
|
return get_trend(current_user.sector_map or {})
|
|
|
|
@router.get("/marketintel")
|
|
def marketintel(current_user = Depends(get_current_user)):
|
|
return get_marketintel()
|
|
|
|
@router.get("/search")
|
|
def search(q: str, current_user = Depends(get_current_user)):
|
|
headers = {'User-Agent': 'Mozilla/5.0'}
|
|
url = f"https://query2.finance.yahoo.com/v1/finance/search?q={q}"esCount=10&newsCount=0"
|
|
res = requests.get(url, headers=headers)
|
|
if res.status_code == 200:
|
|
data = res.json()
|
|
quotes = data.get("quotes", [])
|
|
# Filter for Indian stocks (.NS or .BO)
|
|
indian_stocks = [q for q in quotes if q.get("symbol", "").endswith((".NS", ".BO"))][:10]
|
|
|
|
if indian_stocks:
|
|
import yfinance as yf
|
|
|
|
symbols = [q["symbol"] for q in indian_stocks]
|
|
tickers = yf.Tickers(" ".join(symbols))
|
|
|
|
for stock in indian_stocks:
|
|
try:
|
|
info = tickers.tickers[stock["symbol"]].info
|
|
stock["currentPrice"] = info.get("currentPrice") or info.get("regularMarketPrice")
|
|
stock["marketCap"] = info.get("marketCap")
|
|
except Exception as e:
|
|
print(f"Error fetching {stock['symbol']}: {e}")
|
|
stock["currentPrice"] = None
|
|
stock["marketCap"] = None
|
|
|
|
return indian_stocks
|
|
return []
|