trymonolith commited on
Commit
7dc8f17
·
verified ·
1 Parent(s): 10b82a7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from fastapi.responses import JSONResponse
3
+ from pytrends.request import TrendReq
4
+ import asyncio
5
+ import time
6
+
7
+ app = FastAPI(title="PyTrends API")
8
+
9
+ @app.get("/health")
10
+ async def health_check():
11
+ return {"status": "Health check endpoint", "message": "Get interest over time for keywords", "interest_by_region": "Get interest by region for keywords", "trending_searches": "Get trending queries for a keywords", "related_queries": "Get related queries for keywords", "note": "If you get a 429 error, Google Trends is rate limiting. Wait a few minutes and try again."}
12
+
13
+ @app.get("/interest_over_time")
14
+ async def interest_over_time(keyword: str):
15
+ try:
16
+ pytrends = TrendReq(hl='en-US', tz=360)
17
+ time.sleep(0.5)
18
+ pytrends.build_payload([keyword], timeframe='today 12-m')
19
+ df = pytrends.interest_over_time()
20
+ return {"data": df.to_dict()}
21
+ except Exception as e:
22
+ return JSONResponse(status_code=429, content={"error": str(e), "note": "If you get a 429 error, Google Trends is rate limiting. Wait a few minutes and try again."})
23
+
24
+ @app.get("/interest_by_region")
25
+ async def interest_by_region(keyword: str):
26
+ try:
27
+ pytrends = TrendReq(hl='en-US', tz=360)
28
+ time.sleep(0.5)
29
+ pytrends.build_payload([keyword])
30
+ df = pytrends.interest_by_region()
31
+ return {"data": df.to_dict()}
32
+ except Exception as e:
33
+ return JSONResponse(status_code=429, content={"error": str(e), "note": "If you get a 429 error, Google Trends is rate limiting. Wait a few minutes and try again."})
34
+
35
+ @app.get("/trending_searches")
36
+ async def trending_searches():
37
+ try:
38
+ pytrends = TrendReq(hl='en-US', tz=360)
39
+ time.sleep(0.5)
40
+ df = pytrends.trending_searches(pn='united_states')
41
+ return {"data": df.to_dict()}
42
+ except Exception as e:
43
+ return JSONResponse(status_code=429, content={"error": str(e), "note": "If you get a 429 error, Google Trends is rate limiting. Wait a few minutes and try again."})
44
+
45
+ @app.get("/related_queries")
46
+ async def related_queries(keyword: str):
47
+ try:
48
+ pytrends = TrendReq(hl='en-US', tz=360)
49
+ time.sleep(0.5)
50
+ pytrends.build_payload([keyword])
51
+ df = pytrends.related_queries()
52
+ return {"data": str(df)}
53
+ except Exception as e:
54
+ return JSONResponse(status_code=429, content={"error": str(e), "note": "If you get a 429 error, Google Trends is rate limiting. Wait a few minutes and try again."})