Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI | |
| from fastapi.responses import JSONResponse | |
| from pytrends.request import TrendReq | |
| import time | |
| app = FastAPI(title="PyTrends API") | |
| async def health_check(): | |
| return {"status": "Health check", "message": "PyTrends API"} | |
| async def interest_over_time(keyword: str): | |
| try: | |
| pytrends = TrendReq(hl='en-US', tz=360) | |
| time.sleep(0.5) | |
| pytrends.build_payload([keyword], timeframe='today 12-m') | |
| df = pytrends.interest_over_time() | |
| return {"data": df.to_dict()} | |
| except: | |
| return JSONResponse(status_code=429, content={"error": "Rate limited"}) | |
| async def interest_by_region(keyword: str): | |
| try: | |
| pytrends = TrendReq(hl='en-US', tz=360) | |
| time.sleep(0.5) | |
| pytrends.build_payload([keyword]) | |
| df = pytrends.interest_by_region() | |
| return {"data": df.to_dict()} | |
| except: | |
| return JSONResponse(status_code=429, content={"error": "Rate limited"}) | |
| async def trending_searches(): | |
| try: | |
| pytrends = TrendReq(hl='en-US', tz=360) | |
| time.sleep(0.5) | |
| df = pytrends.trending_searches(pn='united_states') | |
| return {"data": df.to_dict()} | |
| except: | |
| return JSONResponse(status_code=429, content={"error": "Rate limited"}) | |
| async def related_queries(keyword: str): | |
| try: | |
| pytrends = TrendReq(hl='en-US', tz=360) | |
| time.sleep(0.5) | |
| pytrends.build_payload([keyword]) | |
| df = pytrends.related_queries() | |
| return {"data": str(df)} | |
| except: | |
| return JSONResponse(status_code=429, content={"error": "Rate limited"}) |