trymonolith commited on
Commit
84ae442
·
verified ·
1 Parent(s): 2eacfb3

Create app.py

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