56 lines
1.9 KiB
Python
56 lines
1.9 KiB
Python
import json
|
|
|
|
import serpapi
|
|
|
|
from config import SERPAPI_API_KEY
|
|
from lib.jsonify import jsonify_anything
|
|
|
|
client = serpapi.Client(api_key=SERPAPI_API_KEY)
|
|
|
|
|
|
def search_google(query: str):
|
|
if not SERPAPI_API_KEY:
|
|
return {'error': True, 'message': 'The SerpAPI key has not been provided, so this function is disabled.'}
|
|
results = client.search(q=query, engine="google", hl="en", gl="us")
|
|
del results['serpapi_pagination']
|
|
del results['search_metadata']
|
|
del results['pagination']
|
|
del results['search_parameters']
|
|
del results['search_information']
|
|
if results.get('inline_videos'):
|
|
del results['inline_videos']
|
|
|
|
# Need to dump and reparse the JSON so that it is actually formatted correctly.
|
|
return json.loads(jsonify_anything(results))
|
|
|
|
|
|
def search_google_maps(query: str, latitude: float, longitude: float, zoom: float = None):
|
|
"""
|
|
https://serpapi.com/google-maps-api#api-parameters-geographic-location-ll
|
|
"""
|
|
if not SERPAPI_API_KEY:
|
|
return {'error': True, 'message': 'The SerpAPI key has not been provided, so this function is disabled.'}
|
|
if zoom:
|
|
z_str = f',{zoom}z'
|
|
else:
|
|
z_str = ''
|
|
results = client.search(q=query,
|
|
engine="google_maps",
|
|
ll=f'@{latitude},{longitude}{z_str}',
|
|
type='search',
|
|
)
|
|
del results['search_parameters']
|
|
del results['search_metadata']
|
|
del results['serpapi_pagination']
|
|
return json.loads(jsonify_anything(results))
|
|
|
|
|
|
def search_google_news(query: str):
|
|
if not SERPAPI_API_KEY:
|
|
return {'error': True, 'message': 'The SerpAPI key has not been provided, so this function is disabled.'}
|
|
results = client.search(q=query, engine="google_news", hl="en", gl="us")
|
|
del results['menu_links']
|
|
del results['search_metadata']
|
|
del results['search_parameters']
|
|
return json.loads(jsonify_anything(results))
|