Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -6,8 +6,8 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
6 |
from typing import Optional
|
7 |
from pytrends.request import TrendReq
|
8 |
from datetime import datetime, timedelta
|
9 |
-
from fastapi_cache import
|
10 |
-
from fastapi_cache.backends.inmemory import
|
11 |
from fastapi_cache.decorator import cache
|
12 |
|
13 |
app = FastAPI()
|
@@ -73,25 +73,17 @@ app.add_middleware(
|
|
73 |
allow_headers=["*"],
|
74 |
)
|
75 |
|
76 |
-
# Set up in-memory cache
|
77 |
-
cache_backend = InMemoryCacheBackend()
|
78 |
-
caches.set("default", cache_backend)
|
79 |
-
|
80 |
pytrends = TrendReq()
|
81 |
|
82 |
@app.on_event("startup")
|
83 |
-
async def
|
84 |
-
|
85 |
-
|
86 |
-
@app.on_event("shutdown")
|
87 |
-
async def on_shutdown():
|
88 |
-
await close_caches()
|
89 |
|
90 |
-
@app.get("/
|
91 |
-
@cache(expire=3600)
|
92 |
-
async def
|
93 |
-
trending_searches = pytrends.realtime_trending_searches(pn=
|
94 |
-
return trending_searches.to_dict(orient=
|
95 |
|
96 |
@app.get("/", tags=["Home"])
|
97 |
def api_home():
|
|
|
6 |
from typing import Optional
|
7 |
from pytrends.request import TrendReq
|
8 |
from datetime import datetime, timedelta
|
9 |
+
from fastapi_cache import FastAPICache
|
10 |
+
from fastapi_cache.backends.inmemory import InMemoryBackend
|
11 |
from fastapi_cache.decorator import cache
|
12 |
|
13 |
app = FastAPI()
|
|
|
73 |
allow_headers=["*"],
|
74 |
)
|
75 |
|
|
|
|
|
|
|
|
|
76 |
pytrends = TrendReq()
|
77 |
|
78 |
@app.on_event("startup")
|
79 |
+
async def startup():
|
80 |
+
FastAPICache.init(InMemoryBackend(), prefix="fastapi-cache")
|
|
|
|
|
|
|
|
|
81 |
|
82 |
+
@app.get("/realtime_trending_searches")
|
83 |
+
@cache(expire=3600)
|
84 |
+
async def get_realtime_trending_searches(pn: str = Query('US', description="Country code for trending searches")):
|
85 |
+
trending_searches = pytrends.realtime_trending_searches(pn=pn)
|
86 |
+
return trending_searches.to_dict(orient='records')
|
87 |
|
88 |
@app.get("/", tags=["Home"])
|
89 |
def api_home():
|