Spaces:
Sleeping
Sleeping
Create brave.py
Browse files
brave.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
from urllib.parse import quote
|
| 4 |
+
from time import sleep
|
| 5 |
+
|
| 6 |
+
braveNewsEndpoint = "https://api.search.brave.com/res/v1/news/search"
|
| 7 |
+
braveSearchEndpoint = "https://api.search.brave.com/res/v1/web/search"
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def get_brave_search_results(query: str, brave_key: str):
|
| 11 |
+
rv = ''
|
| 12 |
+
url = f'{braveSearchEndpoint}?q={quote(query)}&count=20'
|
| 13 |
+
sleep_time = 1
|
| 14 |
+
while sleep_time < 10:
|
| 15 |
+
response = requests.get(
|
| 16 |
+
url,
|
| 17 |
+
headers= {"Accept": "application/json",
|
| 18 |
+
"X-Subscription-Token": brave_key
|
| 19 |
+
},
|
| 20 |
+
)
|
| 21 |
+
if response.status_code == 429:
|
| 22 |
+
sleep(sleep_time)
|
| 23 |
+
sleep_time *= 3
|
| 24 |
+
else:
|
| 25 |
+
break
|
| 26 |
+
if sleep_time > 9:
|
| 27 |
+
return 'Sorry no items could be found.'
|
| 28 |
+
rv ='''Following are list items delineated by *item separator*
|
| 29 |
+
At the end of each item is (item source, item age)
|
| 30 |
+
*item separator* '''
|
| 31 |
+
jdata = response.json()
|
| 32 |
+
web_results = jdata['web']['results']
|
| 33 |
+
for item in web_results:
|
| 34 |
+
title = item['title']
|
| 35 |
+
description = item['description']
|
| 36 |
+
rv += f'{title}: {description} --'
|
| 37 |
+
try: # extra_snippets can be missing
|
| 38 |
+
for snip in item['extra_snippets']:
|
| 39 |
+
rv += (snip + ' ')
|
| 40 |
+
except:
|
| 41 |
+
pass
|
| 42 |
+
try:
|
| 43 |
+
host = item['meta_url']['hostname']
|
| 44 |
+
except:
|
| 45 |
+
host = 'unknown'
|
| 46 |
+
try:
|
| 47 |
+
age = item['age']
|
| 48 |
+
except:
|
| 49 |
+
age = 'unknown'
|
| 50 |
+
rv += f' (Item source: {host}, Item age: {age})'
|
| 51 |
+
rv += ' *item separator* '
|
| 52 |
+
return rv
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def get_brave_news(query: str, brave_key: str, interval: str = 'pd'):
|
| 56 |
+
url = f'{braveNewsEndpoint}?q={quote(query)}&count=20&extra_snippets=true&freshness={interval}'
|
| 57 |
+
sleep_time = 1
|
| 58 |
+
while sleep_time < 10:
|
| 59 |
+
response = requests.get(
|
| 60 |
+
url,
|
| 61 |
+
headers= {"Accept": "application/json",
|
| 62 |
+
"X-Subscription-Token": brave_key
|
| 63 |
+
},
|
| 64 |
+
)
|
| 65 |
+
if response.status_code == 429:
|
| 66 |
+
sleep(sleep_time)
|
| 67 |
+
sleep_time *= 3
|
| 68 |
+
else:
|
| 69 |
+
break;
|
| 70 |
+
if sleep_time > 9:
|
| 71 |
+
return ('Sorry no news items were found.')
|
| 72 |
+
|
| 73 |
+
rv ='''Following are list items delineated by *item separator*
|
| 74 |
+
At the end of each item is (item source, item age)
|
| 75 |
+
*item separator* '''
|
| 76 |
+
jdata = response.json()
|
| 77 |
+
for item in jdata['results']:
|
| 78 |
+
title = item['title']
|
| 79 |
+
description = item['description']
|
| 80 |
+
rv += f'{title}: {description} --'
|
| 81 |
+
try: # extra_snippets can be missing
|
| 82 |
+
for snip in item['extra_snippets']:
|
| 83 |
+
rv += (snip + ' ')
|
| 84 |
+
except:
|
| 85 |
+
pass
|
| 86 |
+
try:
|
| 87 |
+
host = item['meta_url']['hostname']
|
| 88 |
+
except:
|
| 89 |
+
host = 'unknown'
|
| 90 |
+
try:
|
| 91 |
+
age = item['age']
|
| 92 |
+
except:
|
| 93 |
+
age = 'unknown'
|
| 94 |
+
rv += f' (Item source: {host}, Item age: {age})'
|
| 95 |
+
rv += ' *item separator* '
|
| 96 |
+
return rv
|
| 97 |
+
|