Spaces:
Sleeping
Sleeping
File size: 5,585 Bytes
a8cf3d1 443a71d a8cf3d1 4799cc5 443a71d a8cf3d1 c0c8bed 443a71d a8cf3d1 443a71d a8cf3d1 443a71d a8cf3d1 c0c8bed 443a71d a8cf3d1 5bb19c3 443a71d 38fff96 5bb19c3 af55bf7 a47194c 443a71d af55bf7 443a71d 5bb19c3 443a71d 5bb19c3 443a71d |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
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, banknifty]", example='banknifty')):
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)) |