forked from axsddlr/rpilocator_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
86 lines (66 loc) · 2.45 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.errors import RateLimitExceeded
from slowapi.util import get_remote_address
from starlette.requests import Request
from ast import literal_eval
from api.scrape import rpiLoc
limiter = Limiter(key_func=get_remote_address)
app = FastAPI(
title="Unofficial rpilocator API for Raspberry Pi 4 Model B only",
description="An Unofficial REST API for [RpiLocator](https://rpilocator.com/), Made by [Andre "
"Saddler]( "
"https://github.com/axsddlr)",
version="1.0.3",
docs_url="/",
redoc_url=None,
)
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# init limiter
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
# init classes
rpiloc = rpiLoc()
@app.get("/pi4-stockcheck/{gbopts}", tags=["pi4"])
def pi4_8gb_stockcheck(request: Request, gbopts):
"""get all pi4-8gbs in stock"""
#print(type(gbopts))
gblist = literal_eval(gbopts)
#print(gblist, gbopts)
return rpiloc.pi4_8gig_stockcheck(gblist)
@app.get("/pi4/{country}", tags=["pi4"])
@limiter.limit("1/minute")
def all_pi4_in_a_country(request: Request, country):
"""get all pi4 in a country"""
return rpiloc.rpi_all(country)
@app.get("/v2/pi4/{region}", tags=["rss"])
@limiter.limit("1/minute")
def all_pi4_in_a_region(request: Request, region):
"""get all pi4 in a country"""
return rpiloc.get_rss_entires(region)
@app.get("/v3/pi4/{region}", tags=["twitter"])
@limiter.limit("55/minute")
def all_pi4_in_a_region_tweets(request: Request, region):
"""get all pi4 in a country via twitter alerts"""
return rpiloc.get_rpil_tweets(region)
@app.get("/pi4/{country}/{gbs}", tags=["pi4"])
@limiter.limit("1/minute")
def all_pi4_in_country_with_model_via_GiB(request: Request, country, gbs):
"""get all pi4 in a country with a plus model"""
return rpiloc.rpi_model(country, gbs)
@app.get("/v2/pi4/{region}/{gbs}", tags=["rss"])
@limiter.limit("1/minute")
def all_pi4_in_region_with_model_via_GiB(request: Request, region, gbs):
"""get all pi4 in a region with a plus model"""
return rpiloc.get_rss_model_entires(region, gbs)
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=3000)