Skip to content

Instantly share code, notes, and snippets.

@ColeMurray
Created November 12, 2023 00:29
Show Gist options
  • Save ColeMurray/6f615e105aac4d93650c7089b16fc279 to your computer and use it in GitHub Desktop.
Save ColeMurray/6f615e105aac4d93650c7089b16fc279 to your computer and use it in GitHub Desktop.
basic google search custom engine
import json
import sys
from article_agent.config import my_api_key, my_cse_id
from googleapiclient.discovery import build
def get_search_results(search_term: str, num_search_results: int = 10) -> json:
"""Perform a Google search using Custom Search API"""
# Build request
service = build("customsearch", "v1", developerKey=my_api_key)
# Execute request
query_result = service.cse().list(q=search_term, cx=my_cse_id, num=num_search_results).execute()
print(query_result)
#with open(f'{"-".join(search_term.split("-"))}.json', 'w') as output_file:
with open(f'openai-alpha.json', 'w') as output_file:
json.dump(query_result, output_file)
return query_result
if __name__ == '__main__':
# take first argument from cli
search_term = sys.argv[1]
num_search_results = 10
get_search_results(search_term, num_search_results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment