Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -4,9 +4,11 @@ import hrequests
|
|
| 4 |
import trafilatura
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from typing import Optional
|
| 7 |
-
from functools import lru_cache
|
| 8 |
from pytrends.request import TrendReq
|
| 9 |
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
|
@@ -71,18 +73,25 @@ app.add_middleware(
|
|
| 71 |
allow_headers=["*"],
|
| 72 |
)
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
pytrends = TrendReq(hl='en-US', tz=360)
|
| 78 |
-
pytrends.build_payload(kw_list=[], timeframe='now 1-H')
|
| 79 |
-
trending_searches_df = pytrends.realtime_trending_searches(pn=country_code)
|
| 80 |
-
return trending_searches_df
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
@app.get("/", tags=["Home"])
|
| 88 |
def api_home():
|
|
|
|
| 4 |
import trafilatura
|
| 5 |
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 caches, close_caches
|
| 10 |
+
from fastapi_cache.backends.inmemory import InMemoryCacheBackend
|
| 11 |
+
from fastapi_cache.decorator import cache
|
| 12 |
|
| 13 |
app = FastAPI()
|
| 14 |
|
|
|
|
| 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 on_startup():
|
| 84 |
+
pass
|
| 85 |
+
|
| 86 |
+
@app.on_event("shutdown")
|
| 87 |
+
async def on_shutdown():
|
| 88 |
+
await close_caches()
|
| 89 |
+
|
| 90 |
+
@app.get("/trending_searches")
|
| 91 |
+
@cache(expire=3600) # Cache for 1 hour
|
| 92 |
+
async def get_trending_searches(country: str = Query("US", description="Country code (e.g., 'US' for United States)")):
|
| 93 |
+
trending_searches = pytrends.realtime_trending_searches(pn=country)
|
| 94 |
+
return trending_searches.to_dict(orient="records")
|
| 95 |
|
| 96 |
@app.get("/", tags=["Home"])
|
| 97 |
def api_home():
|