2023-08-05 17:46:04 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
"""
|
|
|
|
Brave (General, news, videos, images)
|
|
|
|
"""
|
|
|
|
|
|
|
|
from urllib.parse import urlencode
|
2023-08-05 18:25:10 +00:00
|
|
|
import chompjs
|
2023-08-05 17:46:04 +00:00
|
|
|
|
|
|
|
about = {
|
|
|
|
"website": 'https://search.brave.com/',
|
|
|
|
"wikidata_id": 'Q22906900',
|
|
|
|
"official_api_documentation": None,
|
|
|
|
"use_official_api": False,
|
|
|
|
"require_api_key": False,
|
|
|
|
"results": 'HTML',
|
|
|
|
}
|
|
|
|
base_url = "https://search.brave.com/"
|
|
|
|
paging = False
|
2023-08-05 18:25:10 +00:00
|
|
|
categories = ['images', 'videos', 'news'] # images, videos, news
|
|
|
|
|
2023-08-05 17:46:04 +00:00
|
|
|
|
|
|
|
def request(query, params):
|
|
|
|
args = {
|
|
|
|
'q': query,
|
|
|
|
'spellcheck': 1,
|
|
|
|
}
|
|
|
|
params["url"] = f"{base_url}{categories[0]}?{urlencode(args)}"
|
|
|
|
|
|
|
|
|
2023-08-05 18:25:10 +00:00
|
|
|
def get_video_results(json_data):
|
|
|
|
results = []
|
2023-08-05 17:46:04 +00:00
|
|
|
|
2023-08-05 18:25:10 +00:00
|
|
|
for result in json_data:
|
2023-08-05 17:46:04 +00:00
|
|
|
results.append(
|
|
|
|
{
|
2023-08-05 18:25:10 +00:00
|
|
|
'template': 'videos.html',
|
2023-08-05 17:46:04 +00:00
|
|
|
'url': result['url'],
|
|
|
|
'thumbnail_src': result['thumbnail']['src'],
|
|
|
|
'img_src': result['properties']['url'],
|
|
|
|
'content': result['description'],
|
|
|
|
'title': result['title'],
|
|
|
|
'source': result['source'],
|
2023-08-05 18:25:10 +00:00
|
|
|
'duration': result['video']['duration'],
|
2023-08-05 17:46:04 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return results
|
|
|
|
|
2023-08-05 18:25:10 +00:00
|
|
|
|
2023-08-05 17:46:04 +00:00
|
|
|
def response(resp):
|
2023-08-05 18:25:10 +00:00
|
|
|
results = []
|
2023-08-05 17:46:04 +00:00
|
|
|
|
2023-08-05 18:25:10 +00:00
|
|
|
datastr = ""
|
|
|
|
for line in resp.text.split("\n"):
|
|
|
|
if "const data = " in line:
|
|
|
|
datastr = line.replace("const data = ", "").strip()[:-1]
|
|
|
|
break
|
|
|
|
|
|
|
|
json_data = chompjs.parse_js_object(datastr)
|
|
|
|
|
2023-08-05 18:35:04 +00:00
|
|
|
json_resp = json_data[1]['data']['body']['response']
|
|
|
|
if categories[0] == 'news':
|
|
|
|
json_resp = json_resp['news']
|
2023-08-05 18:25:10 +00:00
|
|
|
|
2023-08-05 18:35:04 +00:00
|
|
|
for result in json_resp["results"]:
|
2023-08-05 18:25:10 +00:00
|
|
|
item = {
|
|
|
|
'url': result['url'],
|
|
|
|
'title': result['title'],
|
|
|
|
'content': result['description'],
|
|
|
|
}
|
|
|
|
if result['thumbnail'] != "null":
|
|
|
|
item['thumbnail'] = result['thumbnail']['src']
|
|
|
|
|
2023-08-05 18:35:04 +00:00
|
|
|
if categories[0] == 'images':
|
|
|
|
item['template'] = 'images.html'
|
|
|
|
item['img_format'] = result['properties']['format']
|
|
|
|
item['source'] = result['source']
|
|
|
|
item['img_src'] = result['properties']['url']
|
|
|
|
elif categories[0] == 'videos':
|
|
|
|
item['template'] = 'videos.html'
|
|
|
|
item['length'] = result['video']['duration']
|
|
|
|
|
2023-08-05 18:25:10 +00:00
|
|
|
results.append(item)
|
|
|
|
|
|
|
|
return results
|