clean endpoints
This commit is contained in:
parent
c36ab25583
commit
fdde91673f
145
app/main.py
145
app/main.py
@ -399,6 +399,10 @@ class InfoText(BaseModel):
|
|||||||
class HistoricalDate(BaseModel):
|
class HistoricalDate(BaseModel):
|
||||||
date: str
|
date: str
|
||||||
|
|
||||||
|
class FinancialStatement(BaseModel):
|
||||||
|
ticker: str
|
||||||
|
statement: str
|
||||||
|
|
||||||
class OptionsWatchList(BaseModel):
|
class OptionsWatchList(BaseModel):
|
||||||
optionsIdList: list
|
optionsIdList: list
|
||||||
|
|
||||||
@ -914,54 +918,13 @@ async def economic_calendar(data:TickerData, api_key: str = Security(get_api_key
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.post("/stock-income")
|
@app.post("/financial-statement")
|
||||||
async def stock_income(data: TickerData, api_key: str = Security(get_api_key)):
|
async def get_data(data: FinancialStatement, api_key: str = Security(get_api_key)):
|
||||||
data = data.dict()
|
ticker = data.ticker.upper()
|
||||||
ticker = data['ticker'].upper()
|
statement = data.statement.lower()
|
||||||
|
cache_key = f"financial-statement-{ticker}"
|
||||||
|
|
||||||
cache_key = f"stock-income-{ticker}"
|
|
||||||
cached_result = redis_client.get(cache_key)
|
cached_result = redis_client.get(cache_key)
|
||||||
|
|
||||||
if cached_result:
|
|
||||||
return StreamingResponse(
|
|
||||||
io.BytesIO(cached_result),
|
|
||||||
media_type="application/json",
|
|
||||||
headers={"Content-Encoding": "gzip"}
|
|
||||||
)
|
|
||||||
try:
|
|
||||||
with open(f"json/financial-statements/income-statement/quarter/{ticker}.json", 'rb') as file:
|
|
||||||
quarter_res = orjson.loads(file.read())
|
|
||||||
except:
|
|
||||||
quarter_res = []
|
|
||||||
|
|
||||||
try:
|
|
||||||
with open(f"json/financial-statements/income-statement/annual/{ticker}.json", 'rb') as file:
|
|
||||||
annual_res = orjson.loads(file.read())
|
|
||||||
except:
|
|
||||||
annual_res = []
|
|
||||||
|
|
||||||
res = {'quarter': quarter_res, 'annual': annual_res}
|
|
||||||
|
|
||||||
res = orjson.dumps(res)
|
|
||||||
compressed_data = gzip.compress(res)
|
|
||||||
|
|
||||||
redis_client.set(cache_key, compressed_data)
|
|
||||||
redis_client.expire(cache_key, 3600 * 24) # Set cache expiration time to 1 day
|
|
||||||
|
|
||||||
return StreamingResponse(
|
|
||||||
io.BytesIO(compressed_data),
|
|
||||||
media_type="application/json",
|
|
||||||
headers={"Content-Encoding": "gzip"}
|
|
||||||
)
|
|
||||||
|
|
||||||
@app.post("/stock-balance-sheet")
|
|
||||||
async def stock_balance_sheet(data: TickerData, api_key: str = Security(get_api_key)):
|
|
||||||
data = data.dict()
|
|
||||||
ticker = data['ticker'].upper()
|
|
||||||
|
|
||||||
cache_key = f"stock-balance-sheet-{ticker}"
|
|
||||||
cached_result = redis_client.get(cache_key)
|
|
||||||
|
|
||||||
if cached_result:
|
if cached_result:
|
||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
io.BytesIO(cached_result),
|
io.BytesIO(cached_result),
|
||||||
@ -969,25 +932,43 @@ async def stock_balance_sheet(data: TickerData, api_key: str = Security(get_api_
|
|||||||
headers={"Content-Encoding": "gzip"}
|
headers={"Content-Encoding": "gzip"}
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
keys_to_remove = {"symbol","reportedCurrency", "cik", "filingDate", "acceptedDate"}
|
||||||
with open(f"json/financial-statements/balance-sheet-statement/quarter/{ticker}.json", 'rb') as file:
|
base_path = f"json/financial-statements/{statement}"
|
||||||
quarter_res = orjson.loads(file.read())
|
|
||||||
except:
|
|
||||||
quarter_res = []
|
|
||||||
|
|
||||||
try:
|
def load_and_clean(path: str):
|
||||||
with open(f"json/financial-statements/balance-sheet-statement/annual/{ticker}.json", 'rb') as file:
|
if not os.path.exists(path):
|
||||||
annual_res = orjson.loads(file.read())
|
return []
|
||||||
except:
|
with open(path, "rb") as f:
|
||||||
annual_res = []
|
raw_data = orjson.loads(f.read())
|
||||||
|
cleaned = []
|
||||||
|
for sublist in raw_data:
|
||||||
|
# Ensure sublist is a list; if not, skip or wrap in a list as appropriate.
|
||||||
|
if isinstance(sublist, list):
|
||||||
|
cleaned_sublist = [
|
||||||
|
{k: v for k, v in item.items() if k not in keys_to_remove}
|
||||||
|
if isinstance(item, dict) else item
|
||||||
|
for item in sublist
|
||||||
|
]
|
||||||
|
cleaned.append(cleaned_sublist)
|
||||||
|
else:
|
||||||
|
# If sublist isn't a list, we can choose to ignore it or process it as a single dict.
|
||||||
|
if isinstance(sublist, dict):
|
||||||
|
cleaned.append({k: v for k, v in sublist.items() if k not in keys_to_remove})
|
||||||
|
else:
|
||||||
|
# Append as is if it's neither a list nor a dict.
|
||||||
|
cleaned.append(sublist)
|
||||||
|
return cleaned
|
||||||
|
|
||||||
res = {'quarter': quarter_res, 'annual': annual_res}
|
|
||||||
|
|
||||||
res = orjson.dumps(res)
|
quarter_res = load_and_clean(f"{base_path}/quarter/{ticker}.json")
|
||||||
compressed_data = gzip.compress(res)
|
annual_res = load_and_clean(f"{base_path}/annual/{ticker}.json")
|
||||||
|
ttm_res = load_and_clean(f"{base_path}/ttm/{ticker}.json")
|
||||||
|
|
||||||
redis_client.set(cache_key, compressed_data)
|
compressed_data = gzip.compress(
|
||||||
redis_client.expire(cache_key, 3600 * 24) # Set cache expiration time to 1 day
|
orjson.dumps({'quarter': quarter_res, 'annual': annual_res, 'ttm': ttm_res})
|
||||||
|
)
|
||||||
|
|
||||||
|
redis_client.setex(cache_key, 86400, compressed_data) # 1 day expiry
|
||||||
|
|
||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
io.BytesIO(compressed_data),
|
io.BytesIO(compressed_data),
|
||||||
@ -995,6 +976,7 @@ async def stock_balance_sheet(data: TickerData, api_key: str = Security(get_api_
|
|||||||
headers={"Content-Encoding": "gzip"}
|
headers={"Content-Encoding": "gzip"}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.post("/stock-ratios")
|
@app.post("/stock-ratios")
|
||||||
async def stock_ratios(data: TickerData, api_key: str = Security(get_api_key)):
|
async def stock_ratios(data: TickerData, api_key: str = Security(get_api_key)):
|
||||||
data = data.dict()
|
data = data.dict()
|
||||||
@ -1037,47 +1019,6 @@ async def stock_ratios(data: TickerData, api_key: str = Security(get_api_key)):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.post("/stock-cash-flow")
|
|
||||||
async def stock_cash_flow(data: TickerData, api_key: str = Security(get_api_key)):
|
|
||||||
data = data.dict()
|
|
||||||
ticker = data['ticker'].upper()
|
|
||||||
|
|
||||||
cache_key = f"stock-cash-flow-{ticker}"
|
|
||||||
cached_result = redis_client.get(cache_key)
|
|
||||||
|
|
||||||
if cached_result:
|
|
||||||
return StreamingResponse(
|
|
||||||
io.BytesIO(cached_result),
|
|
||||||
media_type="application/json",
|
|
||||||
headers={"Content-Encoding": "gzip"}
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
|
||||||
with open(f"json/financial-statements/cash-flow-statement/quarter/{ticker}.json", 'rb') as file:
|
|
||||||
quarter_res = orjson.loads(file.read())
|
|
||||||
except:
|
|
||||||
quarter_res = []
|
|
||||||
|
|
||||||
try:
|
|
||||||
with open(f"json/financial-statements/cash-flow-statement/annual/{ticker}.json", 'rb') as file:
|
|
||||||
annual_res = orjson.loads(file.read())
|
|
||||||
except:
|
|
||||||
annual_res = []
|
|
||||||
|
|
||||||
res = {'quarter': quarter_res, 'annual': annual_res}
|
|
||||||
|
|
||||||
res = orjson.dumps(res)
|
|
||||||
compressed_data = gzip.compress(res)
|
|
||||||
|
|
||||||
redis_client.set(cache_key, compressed_data)
|
|
||||||
redis_client.expire(cache_key, 3600 * 24) # Set cache expiration time to 1 day
|
|
||||||
|
|
||||||
return StreamingResponse(
|
|
||||||
io.BytesIO(compressed_data),
|
|
||||||
media_type="application/json",
|
|
||||||
headers={"Content-Encoding": "gzip"}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/economic-calendar")
|
@app.get("/economic-calendar")
|
||||||
async def economic_calendar(api_key: str = Security(get_api_key)):
|
async def economic_calendar(api_key: str = Security(get_api_key)):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user