add bulk download endpoint
This commit is contained in:
parent
ca406f837f
commit
04f6805353
49
app/main.py
49
app/main.py
@ -2,6 +2,7 @@
|
|||||||
import random
|
import random
|
||||||
import io
|
import io
|
||||||
import gzip
|
import gzip
|
||||||
|
import csv
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
import secrets
|
import secrets
|
||||||
@ -9,6 +10,7 @@ from benzinga import financial_data
|
|||||||
from typing import List, Dict, Set
|
from typing import List, Dict, Set
|
||||||
# Third-party library imports
|
# Third-party library imports
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
import zipfile
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import orjson
|
import orjson
|
||||||
import aiohttp
|
import aiohttp
|
||||||
@ -298,6 +300,9 @@ class HistoricalPrice(BaseModel):
|
|||||||
ticker: str
|
ticker: str
|
||||||
timePeriod: str
|
timePeriod: str
|
||||||
|
|
||||||
|
class BulkDownload(BaseModel):
|
||||||
|
tickers: list
|
||||||
|
|
||||||
class AnalystId(BaseModel):
|
class AnalystId(BaseModel):
|
||||||
analystId: str
|
analystId: str
|
||||||
|
|
||||||
@ -4307,6 +4312,50 @@ async def get_stock_data(data: BulkList, api_key: str = Security(get_api_key)):
|
|||||||
return combined_data
|
return combined_data
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/bulk-download")
|
||||||
|
async def get_stock(data: BulkDownload, api_key: str = Security(get_api_key)):
|
||||||
|
# Ensure tickers is a list
|
||||||
|
tickers = [ticker.upper() for ticker in data.tickers]
|
||||||
|
|
||||||
|
# Create an in-memory binary stream for the zip archive
|
||||||
|
memory_file = io.BytesIO()
|
||||||
|
|
||||||
|
# Open a zipfile for writing into that memory stream
|
||||||
|
with zipfile.ZipFile(memory_file, mode="w", compression=zipfile.ZIP_DEFLATED) as zf:
|
||||||
|
for ticker in tickers:
|
||||||
|
try:
|
||||||
|
with open(f"json/historical-price/max/{ticker}.json", 'rb') as file:
|
||||||
|
# Load the JSON data for the ticker
|
||||||
|
data_list = orjson.loads(file.read())
|
||||||
|
except Exception:
|
||||||
|
data_list = []
|
||||||
|
|
||||||
|
# Convert the JSON data to CSV format. Adjust the keys as needed.
|
||||||
|
csv_buffer = io.StringIO()
|
||||||
|
csv_writer = csv.writer(csv_buffer)
|
||||||
|
|
||||||
|
if data_list:
|
||||||
|
# Write headers using keys from the first record
|
||||||
|
headers = list(data_list[0].keys())
|
||||||
|
csv_writer.writerow(headers)
|
||||||
|
# Write each row
|
||||||
|
for row in data_list:
|
||||||
|
csv_writer.writerow([row.get(key, "") for key in headers])
|
||||||
|
else:
|
||||||
|
csv_writer.writerow(["No data available"])
|
||||||
|
|
||||||
|
zf.writestr(f"{ticker}.csv", csv_buffer.getvalue())
|
||||||
|
|
||||||
|
# Seek back to the start of the BytesIO stream before returning it.
|
||||||
|
memory_file.seek(0)
|
||||||
|
|
||||||
|
# Return the zip file as a StreamingResponse.
|
||||||
|
return StreamingResponse(
|
||||||
|
memory_file,
|
||||||
|
media_type="application/zip",
|
||||||
|
headers={"Content-Disposition": "attachment; filename=bulk_data.zip"}
|
||||||
|
)
|
||||||
|
|
||||||
@app.get("/newsletter")
|
@app.get("/newsletter")
|
||||||
async def get_newsletter():
|
async def get_newsletter():
|
||||||
try:
|
try:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user