-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (52 loc) · 2.08 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from fastapi import FastAPI, HTTPException, Query, Request, status
from fastapi.exceptions import RequestValidationError
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse, PlainTextResponse
import requests, json
from Scrape import Scrape
app = FastAPI()
@app.on_event("startup")
def startup():
structure = '{"elements": [{"from": "[class=\'BNeawe iBp4i AP7Wnd\']","to": "amount"}]}'
global elements_to_scrape
elements_to_scrape = json.loads(structure)
@app.get("/")
def root():
return {"hello":"world"}
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
x = exc.errors()
for i in x:
return JSONResponse(
status_code=status.HTTP_404_NOT_FOUND,
content=jsonable_encoder({"detail": str(i["loc"][1]) +" "+ str(i["msg"])}),
)
@app.get("/v1/{symbol}/")
def get_data(symbol: str):
fetched_data = {}
try:
s = Scrape(1, symbol, elements_to_scrape)
if s.summary():
fetched_data = s.summary()
else:
raise HTTPException(status_code=400, detail=f"{symbol} query is empty")
except requests.TooManyRedirects:
raise HTTPException(status_code=404, detail=f"{symbol} doesn't exist or cannot be found")
except requests.HTTPError:
raise HTTPException(status_code=500, detail="An error has occurred while processing the request.")
return fetched_data
@app.get("/v2/")
def get_data(crypto: str, currency: str):
fetched_data = {}
try:
symbols = [crypto, currency]
s = Scrape(2, symbols, elements_to_scrape)
if s.summary():
fetched_data = s.summary()
else:
raise HTTPException(status_code=400, detail="query is empty")
except requests.TooManyRedirects:
raise HTTPException(status_code=404, detail=f"resource doesn't exist or cannot be found")
except requests.HTTPError:
raise HTTPException(status_code=500, detail="An error has occurred while processing the request.")
return fetched_data