File size: 1,461 Bytes
0c591a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
"""
Stock search route handlers.
Provides stock ticker autocomplete functionality.
"""

from fastapi import APIRouter, HTTPException, Query

from src.stock_listings import get_us_stock_listings, search_stocks

router = APIRouter()

# Stock listings cache (loaded once at startup)
STOCK_LISTINGS: list = []


async def load_stock_listings():
    """Load stock listings on startup."""
    global STOCK_LISTINGS
    try:
        STOCK_LISTINGS = get_us_stock_listings()
        print(f"Loaded {len(STOCK_LISTINGS)} US stock listings")
    except Exception as e:
        print(f"Warning: Could not load stock listings: {e}")


@router.get("/api/stocks/search")
async def search_stocks_endpoint(q: str = Query(..., min_length=1, max_length=50)):
    """Search US stock listings by symbol or company name."""
    global STOCK_LISTINGS

    if not STOCK_LISTINGS:
        # Fallback: try loading if not already loaded
        try:
            STOCK_LISTINGS = get_us_stock_listings()
        except Exception:
            raise HTTPException(status_code=503, detail="Stock listings not available")

    results = search_stocks(q, STOCK_LISTINGS, max_results=10)

    return {
        "query": q,
        "results": [
            {
                "symbol": r["symbol"],
                "name": r["name"],
                "exchange": r["exchange"],
                "match_type": r.get("match_type", "unknown")
            }
            for r in results
        ]
    }