Spaces:
Running
Running
File size: 3,255 Bytes
da8f14f 750068f da8f14f 750068f da8f14f c055908 750068f c055908 |
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
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)
|