-
Notifications
You must be signed in to change notification settings - Fork 78
/
sources_searcher.py
66 lines (51 loc) · 2.03 KB
/
sources_searcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import requests
from typing import Dict, Any, Optional, List
# use ENV variables
# Constants
API_URL = "https://google.serper.dev/search"
API_KEY = os.getenv("SERPER_API_KEY")
DEFAULT_LOCATION = 'us'
HEADERS = {
'X-API-KEY': API_KEY,
'Content-Type': 'application/json'
}
def get_sources(query: str, pro_mode: bool = False, stored_location: Optional[str] = None) -> Dict[str, Any]:
"""
Fetch search results from Serper API.
:param query: Search query string
:param pro_mode: Boolean to determine the number of results
:param stored_location: Optional location string
:return: Dictionary containing search results
"""
try:
search_location = (stored_location or DEFAULT_LOCATION).lower()
num_results = 10 if pro_mode else 20
payload = {
"q": query,
"num": num_results,
"gl": search_location
}
response = requests.post(API_URL, headers=HEADERS, json=payload, timeout=10)
response.raise_for_status()
data = response.json()
return {
'organic': extract_fields(data.get('organic', []), ['title', 'link', 'snippet', 'date']),
'topStories': extract_fields(data.get('topStories', []), ['title', 'imageUrl']),
'images': extract_fields(data.get('images', [])[:6], ['title', 'imageUrl']),
'graph': data.get('knowledgeGraph'),
'answerBox': data.get('answerBox')
}
except requests.RequestException as e:
print(f"HTTP error while getting sources: {e}")
except Exception as e:
print(f"Unexpected error while getting sources: {e}")
return {}
def extract_fields(items: List[Dict[str, Any]], fields: List[str]) -> List[Dict[str, Any]]:
"""
Extract specified fields from a list of dictionaries.
:param items: List of dictionaries
:param fields: List of fields to extract
:return: List of dictionaries with only the specified fields
"""
return [{key: item[key] for key in fields if key in item} for item in items]