-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
39 lines (31 loc) · 1.05 KB
/
main.py
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
from scrape import Scrape
from requests import HTTPError, TooManyRedirects
from fastapi import FastAPI, Query, HTTPException
from typing import List, Optional
import json
app = FastAPI()
elements_to_scrape = {}
@app.on_event("startup")
def startup():
f = open("scrape.json")
data = f.read()
f.close()
global elements_to_scrape
elements_to_scrape = json.loads(data)
@app.get("/root")
def root():
return "Hello World!"
@app.get("/v1/{symbol}/summary/")
def summary(symbol, q: Optional[List[str]] = Query(None)):
summary_data = {}
try:
s = Scrape(symbol, elements_to_scrape)
summary_data = s.summary()
if(q != None):
if all (k in summary_data for k in q):
summary_data = {key: summary_data[key] for key in q}
except TooManyRedirects:
raise HTTPException(status_code=404, detail="{symbol} doesn't exist or cannot be found.")
except HTTPError:
raise HTTPException(status_code=500, detail="An error has occurred while processing the request.")
return summary_data