Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,9 +1,12 @@
|
|
1 |
-
from fastapi import FastAPI, HTTPException, Query
|
2 |
from pydantic import BaseModel
|
3 |
import hrequests
|
4 |
import trafilatura
|
5 |
from fastapi.middleware.cors import CORSMiddleware
|
6 |
from typing import Optional
|
|
|
|
|
|
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
@@ -68,6 +71,20 @@ app.add_middleware(
|
|
68 |
allow_headers=["*"],
|
69 |
)
|
70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
@app.get("/", tags=["Home"])
|
72 |
def api_home():
|
73 |
return {'detail': 'Welcome to Web-Scraping API! Visit https://pvanand-web-scraping.hf.space/docs to test'}
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException, Query, Depends
|
2 |
from pydantic import BaseModel
|
3 |
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 |
allow_headers=["*"],
|
72 |
)
|
73 |
|
74 |
+
# Function to fetch real-time trending searches and cache results for 1 hour
|
75 |
+
@lru_cache(maxsize=None)
|
76 |
+
def fetch_trending_searches(country_code: str):
|
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 |
+
# Endpoint to get trending searches for a specific country
|
83 |
+
@app.get("/trending-searches/{country_code}")
|
84 |
+
async def get_trending_searches(country_code: str):
|
85 |
+
return fetch_trending_searches(country_code.upper())
|
86 |
+
|
87 |
+
|
88 |
@app.get("/", tags=["Home"])
|
89 |
def api_home():
|
90 |
return {'detail': 'Welcome to Web-Scraping API! Visit https://pvanand-web-scraping.hf.space/docs to test'}
|