GrowwFI / main.py
jarvisx17's picture
Update main.py
af55bf7 verified
raw
history blame
5.59 kB
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi import FastAPI, Request, HTTPException, Query, Body
from fastapi.middleware.cors import CORSMiddleware
from utils import StockDataFetcher
from Model import *
import json
fetcher = StockDataFetcher()
app = FastAPI(swagger_ui_parameters={"syntaxHighlight.theme": "obsidian"})
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
templates = Jinja2Templates(directory="templates")
# HTML Page
@app.get("/", response_class=HTMLResponse)
async def read_root(request: Request):
return templates.TemplateResponse("hello.html", {"request": request})
# Latest Price Fetch
@app.get('/ltp', response_model=LTP, responses={200: {"description": "Successful response", "content": {"application/json": {"example": ltp_response_example}}}})
async def get_data(ticker: str = Query(..., description="Enter your stock name.", example='zomato')):
try:
response = fetcher.fetch_latest_price(ticker)
if response:
return {'ltp' :response}
else:
raise HTTPException(status_code=404, detail="ltp price not found")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Search Stock Ticker
@app.get('/search', response_model=Stock, responses={200: {"description": "Successful response", "content": {"application/json": {"example": search_response_example}}}})
async def stock_search(ticker: str = Query(..., description="Enter your stock name.", example='zomato')):
try:
fetcher = StockDataFetcher()
response = fetcher.search_entity(ticker)
if response:
return response
else:
raise HTTPException(status_code=404, detail="Stock not found")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Fetch Historical Data
@app.get('/historical', response_model=HISTORICAL, responses={200: {"description": "Successful response", "content": {"application/json": {"example": historical_response_example}}}})
async def get_stocks_data(ticker: str = Query(..., description="Enter your stock name.", example='zomato'), intervals: int = Query(..., description="Select your interval 1m, 5m, 15m", example=5), days: int = Query(..., description="Enter you numbers of day", example=10)):
try:
response = fetcher.fetch_stock_data(ticker, intervals, days).to_dict(orient="records")
if response:
return {"data" : response}
else:
raise HTTPException(status_code=404, detail="historical data not found.")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Fetch Stock Chain
@app.get('/chain', response_model=CHAIN, responses={200: {"description": "Successful response", "content": {"application/json": {"example": chain_response_example}}}})
async def get_chain(ticker: str = Query(..., description="Enter your index name from [nifty, nifty-bank]", example='nifty-bank')):
try:
response, exp, index_ltp = fetcher.fetch_option_chain(ticker)
response = response.to_dict(orient="records")
if response:
return {"ltp": index_ltp, "expiry": exp, "data" : response}
else:
raise HTTPException(status_code=404, detail="Unable to fetch chain.")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Stocks News
@app.get('/news', response_model=NEWS, responses={200: {"description": "Successful response", "content": {"application/json": {"example": news_response_example}}}})
async def get_stocks_data(ticker: str = Query(..., description="Enter your stock name.", example='zomato'), page: int = Query(..., description="Enter number of pages.", example=3), size: int = Query(..., description="Enter number of news you want to extract", example=4)):
try:
response = fetcher.fetch_stock_news(ticker, page=page, size=size).to_dict(orient="records")
if response:
return {"data" : response}
else:
raise HTTPException(status_code=404, detail="Unable to fetch news data.")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Get signal data
@app.get('/signal', response_model=SIGNAL, responses={200: {"description": "Successful response", "content": {"application/json": {"example": signal_response_example}}}})
async def get_stock_signal(index: str = Query(..., description="Enter your index name from [nifty, nifty-bank]", example='nifty')):
try:
response = fetcher.realtime_signal(index).tail(1).to_dict(orient="records")[0]
if response:
return {"data" : response}
else:
raise HTTPException(status_code=404, detail="Unable to fetch signals")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Fetch all stocks info
@app.get('/all', response_model=ALL, responses={200: {"description": "Successful response", "content": {"application/json": {"example": all_response_example}}}})
async def get_stocks_data():
try:
response = fetcher.fetch_all_stock().to_json(orient="records")
if response:
return {"data" : response}
else:
raise HTTPException(status_code=404, detail="Unable to fetch all stocks")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))