paulhemb's picture
Update serp_client.py
e141537 verified
Raw History Blame Contribute Delete
1.96 kB
import serpapi
import logging
from typing import List, Dict
from config import SERPAPI_KEY
logger = logging.getLogger(__name__)
def get_search_volume(query: str) -> int:
"""Fetch Google Trends interest over time (max value 0-100)."""
if not SERPAPI_KEY:
return 0
try:
client = serpapi.Client(api_key=SERPAPI_KEY)
params = {
"engine": "google_trends",
"q": query,
"date": "today 12-m",
"tz": "420",
"data_type": "TIMESERIES"
}
results = client.search(params)
timeline = results.get("interest_over_time", {}).get("timeline_data", [])
if timeline:
values = [point["values"][0]["extracted_value"] for point in timeline if point.get("values")]
return max(values) if values else 0
return 0
except Exception as e:
logger.error(f"SerpAPI error: {e}")
return 0
def search_competitors(idea: str) -> List[str]:
if not SERPAPI_KEY:
return []
try:
client = serpapi.Client(api_key=SERPAPI_KEY)
params = {
"engine": "google",
"q": f"{idea} startup",
"num": 10
}
results = client.search(params)
organic = results.get("organic_results", [])
return [r["title"] for r in organic][:5]
except Exception as e:
logger.error(f"SerpAPI error: {e}")
return []
def search_funding_news(idea: str) -> List[Dict]:
if not SERPAPI_KEY:
return []
try:
client = serpapi.Client(api_key=SERPAPI_KEY)
params = {
"engine": "google_news",
"q": f"{idea} funding",
"num": 5
}
results = client.search(params)
news = results.get("news_results", [])
return [{"title": n["title"], "link": n["link"]} for n in news]
except Exception as e:
logger.error(f"SerpAPI error: {e}")
return []