Satyam0077's picture
Update search.py
6497e33 verified
import requests
import pandas as pd
import time
import os
# βœ… Retrieve the API key from Hugging Face secrets
API_KEY = os.getenv("SERPAPI_API_KEY", "") # This will get the API key from Hugging Face secrets
# βœ… Check if the API_KEY is set
if not API_KEY:
raise ValueError("API key is missing. Please set the SERPAPI_API_KEY environment variable.")
def get_collaboration_data(competitors, target="Virgin Media"):
results = []
# Iterate over each competitor to search for collaborations with Virgin Media
for comp in competitors:
query = f"{comp} {target} partnership OR collaboration"
params = {
"q": query,
"api_key": API_KEY,
"engine": "google", # You can switch to other engines if needed
"num": 5 # Number of results to return per query
}
print(f"Searching: {query}") # Log the query for tracking
try:
# Make a request to the SERP API
response = requests.get("https://serpapi.com/search", params=params)
response.raise_for_status() # Check for request errors (e.g., 4xx, 5xx)
# Parse the JSON response
data = response.json()
# Extract the organic results
for item in data.get("organic_results", []):
results.append({
"Competitor": comp,
"Title": item.get("title", "No title available"),
"Link": item.get("link", "No link available"),
"Snippet": item.get("snippet", "No snippet available")
})
except requests.exceptions.RequestException as e:
print(f"Error fetching data for {comp}: {e}")
except ValueError as e:
print(f"Error parsing JSON response for {comp}: {e}")
time.sleep(1) # βœ… Respect API rate limits
# Return results as a DataFrame
return pd.DataFrame(results)