AgentX / brave.py
dlflannery's picture
Update brave.py
750068f verified
import requests
import json
from urllib.parse import quote
from time import sleep
braveNewsEndpoint = "https://api.search.brave.com/res/v1/news/search"
braveSearchEndpoint = "https://api.search.brave.com/res/v1/web/search"
def get_brave_search_results(query: str, brave_key: str):
rv = ''
url = f'{braveSearchEndpoint}?q={quote(query)}&count=20'
sleep_time = 1
while sleep_time < 10:
response = requests.get(
url,
headers= {"Accept": "application/json",
"X-Subscription-Token": brave_key
},
)
if response.status_code != 200: # == 429:
sleep(sleep_time)
sleep_time *= 3
else:
break
if sleep_time > 9:
return 'Sorry no items could be found.'
rv ='''Following are list items delineated by *item separator*
At the end of each item is (item source, item age)
*item separator* '''
jdata = response.json()
web_results = jdata['web']['results']
for item in web_results:
title = item['title']
description = item['description']
rv += f'{title}: {description} --'
try: # extra_snippets can be missing
for snip in item['extra_snippets']:
rv += (snip + ' ')
except:
pass
try:
host = item['meta_url']['hostname']
except:
host = 'unknown'
try:
age = item['age']
except:
age = 'unknown'
rv += f' (Item source: {host}, Item age: {age})'
rv += ' *item separator* '
return rv
def get_brave_news(query: str, brave_key: str, interval: str = 'pd'):
url = f'{braveNewsEndpoint}?q={quote(query)}&count=20&extra_snippets=true&freshness={interval}'
sleep_time = 1
while sleep_time < 10:
response = requests.get(
url,
headers= {"Accept": "application/json",
"X-Subscription-Token": brave_key
},
)
if response.status_code != 200: # == 429:
sleep(sleep_time)
sleep_time *= 3
else:
break;
if sleep_time > 9:
return ('Sorry no news items were found.')
rv ='''Following are list items delineated by *item separator*
At the end of each item is (item source, item age)
*item separator* '''
jdata = response.json()
for item in jdata['results']:
title = item['title']
description = item['description']
rv += f'{title}: {description} --'
try: # extra_snippets can be missing
for snip in item['extra_snippets']:
rv += (snip + ' ')
except:
pass
try:
host = item['meta_url']['hostname']
except:
host = 'unknown'
try:
age = item['age']
except:
age = 'unknown'
rv += f' (Item source: {host}, Item age: {age})'
rv += ' *item separator* '
return rv
# if __name__ == '__main__':
# response = get_brave_news('latest news about charlie kirk shooting',
# 'BSAI-GRRwoCR1j',
# 'pd')
# print(response)