David Li commited on
Commit
6a06a5e
·
1 Parent(s): 2b6d150

fix: update requirements.txt

Browse files
Files changed (2) hide show
  1. main.py +42 -20
  2. requirements.txt +2 -1
main.py CHANGED
@@ -1,38 +1,60 @@
1
  from fastapi import FastAPI
2
  import pandas as pd
 
 
 
 
3
 
4
  from fastapi.middleware.cors import CORSMiddleware
5
 
6
- origins = [
7
- "https://stonk-list-api.p.rapidapi.com",
8
- "http://localhost:3000"
9
- ]
10
-
11
  app = FastAPI()
12
 
 
 
 
 
13
 
14
  app.add_middleware(
15
  CORSMiddleware,
16
- allow_origins=origins,
17
  allow_credentials=True,
18
  allow_methods=["*"],
19
  allow_headers=["*"],
20
  )
21
 
22
- us_tickers = pd.read_csv(
23
- "https://raw.githubusercontent.com/dli-invest/eod_tickers/main/data/us_stock_data.csv", index_col=False
24
- )
25
-
26
- cad_tickers = pd.read_csv(
27
- "https://raw.githubusercontent.com/FriendlyUser/cad_tickers_list/main/static/latest/stocks.csv", index_col=False
28
- )
29
- @app.get("/tickers")
30
- async def get_spreadsheet(exchange: str = "US"):
31
  """
32
  """
33
- if exchange == "US":
34
- # return spreadsheets from github here
35
- return us_tickers.to_csv()
36
- else:
37
- return cad_tickers.to_csv()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
 
1
  from fastapi import FastAPI
2
  import pandas as pd
3
+ import os
4
+ from deta import Deta
5
+ from fastapi.responses import StreamingResponse, FileResponse
6
+ from io import BytesIO
7
 
8
  from fastapi.middleware.cors import CORSMiddleware
9
 
 
 
 
 
 
10
  app = FastAPI()
11
 
12
+ # load deta using
13
+ project_key = os.getenv("DETA_DRIVE_KEY")
14
+ deta = Deta(project_key)
15
+ drive = deta.Drive("stonk_events")
16
 
17
  app.add_middleware(
18
  CORSMiddleware,
19
+ # allow_origins=origins,
20
  allow_credentials=True,
21
  allow_methods=["*"],
22
  allow_headers=["*"],
23
  )
24
 
25
+ @app.get("/files")
26
+ async def get_files(exchange: str = "US"):
 
 
 
 
 
 
 
27
  """
28
  """
29
+ # get files in deta
30
+ result = drive.list()
31
+ all_files = result.get("names")
32
+ paging = result.get("paging")
33
+ last = paging.get("last") if paging else None
34
+
35
+ while (last):
36
+ # provide last from previous call
37
+ result = drive.list(last=last)
38
+
39
+ all_files += result.get("names")
40
+ # update last
41
+ paging = result.get("paging")
42
+ last = paging.get("last") if paging else None
43
+ return all_files
44
+
45
+
46
+ # get file by name from deta /file/{id}
47
+ @app.get("/file/{id}")
48
+ async def get_file(id: str):
49
+ output_file = drive.get(id)
50
+ # return file
51
+ # content = output_file.read()
52
+
53
+ content = output_file.read()
54
+ output_file.close()
55
+ headers = {
56
+ 'Content-Disposition': f'inline; filename="{id}"'
57
+ }
58
+ bytesio_object = BytesIO(content)
59
+ return StreamingResponse(bytesio_object, headers=headers)
60
 
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  fastapi
2
- pandas
 
 
1
  fastapi
2
+ pandas
3
+ deta