Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -3,10 +3,15 @@ from fastapi.templating import Jinja2Templates
|
|
| 3 |
from fastapi import FastAPI, Request, HTTPException
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
from utils import StockDataFetcher
|
|
|
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
app = FastAPI(swagger_ui_parameters={"syntaxHighlight.theme": "obsidian"})
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
fetcher = StockDataFetcher()
|
| 11 |
|
| 12 |
origins = ["*"]
|
|
@@ -26,7 +31,10 @@ async def read_root(request: Request):
|
|
| 26 |
return templates.TemplateResponse("hello.html", {"request": request})
|
| 27 |
|
| 28 |
|
| 29 |
-
@app.get('/ltp'
|
|
|
|
|
|
|
|
|
|
| 30 |
async def get_data(ticker: str):
|
| 31 |
try:
|
| 32 |
response = fetcher.fetch_latest_price(ticker)
|
|
@@ -39,5 +47,45 @@ async def get_stocks_data(ticker: str, intervals: int, days: int):
|
|
| 39 |
try:
|
| 40 |
response = fetcher.fetch_stock_data(ticker, 15, 10).to_dict(orient="records")
|
| 41 |
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
except:
|
| 43 |
return {"data" : response}
|
|
|
|
| 3 |
from fastapi import FastAPI, Request, HTTPException
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
from utils import StockDataFetcher
|
| 6 |
+
from pydantic import BaseModel
|
| 7 |
+
from typing import List, Dict
|
| 8 |
|
| 9 |
|
| 10 |
app = FastAPI(swagger_ui_parameters={"syntaxHighlight.theme": "obsidian"})
|
| 11 |
|
| 12 |
+
class StockPriceResponse(BaseModel):
|
| 13 |
+
ltp: float
|
| 14 |
+
|
| 15 |
fetcher = StockDataFetcher()
|
| 16 |
|
| 17 |
origins = ["*"]
|
|
|
|
| 31 |
return templates.TemplateResponse("hello.html", {"request": request})
|
| 32 |
|
| 33 |
|
| 34 |
+
@app.get('/ltp', response_model=StockPriceResponse, response_model_exclude_unset=True,
|
| 35 |
+
summary="Get the latest price of a stock",
|
| 36 |
+
response_description="Latest stock price",
|
| 37 |
+
responses={200: {"content": {"application/json": {"example": {"ltp": 123.45}}}}}))
|
| 38 |
async def get_data(ticker: str):
|
| 39 |
try:
|
| 40 |
response = fetcher.fetch_latest_price(ticker)
|
|
|
|
| 47 |
try:
|
| 48 |
response = fetcher.fetch_stock_data(ticker, 15, 10).to_dict(orient="records")
|
| 49 |
return response
|
| 50 |
+
except:
|
| 51 |
+
return {"data" : response}
|
| 52 |
+
|
| 53 |
+
@app.get('/chain')
|
| 54 |
+
async def get_stocks_data(ticker: str):
|
| 55 |
+
try:
|
| 56 |
+
response = fetcher.fetch_option_chain(ticker).to_dict(orient="records")
|
| 57 |
+
return response
|
| 58 |
+
except:
|
| 59 |
+
return {"data" : response}
|
| 60 |
+
|
| 61 |
+
@app.get('/search')
|
| 62 |
+
async def get_stocks_data(ticker: str):
|
| 63 |
+
try:
|
| 64 |
+
response = fetcher.search_entity(ticker)
|
| 65 |
+
return response
|
| 66 |
+
except:
|
| 67 |
+
return {"data" : response}
|
| 68 |
+
|
| 69 |
+
@app.get('/news')
|
| 70 |
+
async def get_stocks_data(ticker: str, page: int, size: int):
|
| 71 |
+
try:
|
| 72 |
+
response = fetcher.fetch_stock_news(ticker, page=page, size=size).to_dict(orient="records")
|
| 73 |
+
return response
|
| 74 |
+
except:
|
| 75 |
+
return {"data" : response}
|
| 76 |
+
|
| 77 |
+
@app.get('/all')
|
| 78 |
+
async def get_stocks_data():
|
| 79 |
+
try:
|
| 80 |
+
response = fetcher.fetch_all_stock().to_dict(orient="records")
|
| 81 |
+
return response
|
| 82 |
+
except:
|
| 83 |
+
return {"data" : response}
|
| 84 |
+
|
| 85 |
+
@app.get('/signal')
|
| 86 |
+
async def get_stocks_data(index: str):
|
| 87 |
+
try:
|
| 88 |
+
response = fetcher.realtime_signal(index).tail(1).to_dict(orient="records")[0]
|
| 89 |
+
return response
|
| 90 |
except:
|
| 91 |
return {"data" : response}
|