|
@@ -1,9 +1,10 @@
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
"""
|
|
|
-DuckDuckGo Images
|
|
|
-~~~~~~~~~~~~~~~~~
|
|
|
+DuckDuckGo Extra (images, videos, news)
|
|
|
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
"""
|
|
|
|
|
|
+from datetime import datetime
|
|
|
from typing import TYPE_CHECKING
|
|
|
from urllib.parse import urlencode
|
|
|
|
|
@@ -32,6 +33,9 @@ about = {
|
|
|
|
|
|
# engine dependent config
|
|
|
categories = ['images', 'web']
|
|
|
+ddg_category = 'images'
|
|
|
+"""The category must be any of ``images``, ``videos`` and ``news``
|
|
|
+"""
|
|
|
paging = True
|
|
|
safesearch = True
|
|
|
send_accept_language_header = True
|
|
@@ -39,6 +43,8 @@ send_accept_language_header = True
|
|
|
safesearch_cookies = {0: '-2', 1: None, 2: '1'}
|
|
|
safesearch_args = {0: '1', 1: None, 2: '1'}
|
|
|
|
|
|
+search_path_map = {'images': 'i', 'videos': 'v', 'news': 'news'}
|
|
|
+
|
|
|
|
|
|
def request(query, params):
|
|
|
|
|
@@ -69,28 +75,61 @@ def request(query, params):
|
|
|
args['p'] = safe_search # "-1", "1"
|
|
|
|
|
|
logger.debug("cookies: %s", params['cookies'])
|
|
|
- args = urlencode(args)
|
|
|
- params['url'] = 'https://duckduckgo.com/i.js?{args}'.format(args=args)
|
|
|
+
|
|
|
+ params['url'] = f'https://duckduckgo.com/{search_path_map[ddg_category]}.js?{urlencode(args)}'
|
|
|
|
|
|
return params
|
|
|
|
|
|
|
|
|
+def _image_result(result):
|
|
|
+ return {
|
|
|
+ 'template': 'images.html',
|
|
|
+ 'url': result['url'],
|
|
|
+ 'title': result['title'],
|
|
|
+ 'content': '',
|
|
|
+ 'thumbnail_src': result['thumbnail'],
|
|
|
+ 'img_src': result['image'],
|
|
|
+ 'img_format': '%s x %s' % (result['width'], result['height']),
|
|
|
+ 'source': result['source'],
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+def _video_result(result):
|
|
|
+ return {
|
|
|
+ 'template': 'videos.html',
|
|
|
+ 'url': result['content'],
|
|
|
+ 'title': result['title'],
|
|
|
+ 'content': result['description'],
|
|
|
+ 'thumbnail': result['images'].get('small') or result['images'].get('medium'),
|
|
|
+ 'iframe_src': result['embed_url'],
|
|
|
+ 'source': result['provider'],
|
|
|
+ 'length': result['duration'],
|
|
|
+ 'metadata': result.get('uploader'),
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+def _news_result(result):
|
|
|
+ return {
|
|
|
+ 'url': result['url'],
|
|
|
+ 'title': result['title'],
|
|
|
+ 'content': result['excerpt'],
|
|
|
+ 'source': result['source'],
|
|
|
+ 'publishedDate': datetime.utcfromtimestamp(result['date']),
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
def response(resp):
|
|
|
results = []
|
|
|
res_json = resp.json()
|
|
|
|
|
|
for result in res_json['results']:
|
|
|
- results.append(
|
|
|
- {
|
|
|
- 'template': 'images.html',
|
|
|
- 'title': result['title'],
|
|
|
- 'content': '',
|
|
|
- 'thumbnail_src': result['thumbnail'],
|
|
|
- 'img_src': result['image'],
|
|
|
- 'url': result['url'],
|
|
|
- 'img_format': '%s x %s' % (result['width'], result['height']),
|
|
|
- 'source': result['source'],
|
|
|
- }
|
|
|
- )
|
|
|
+ if ddg_category == 'images':
|
|
|
+ results.append(_image_result(result))
|
|
|
+ elif ddg_category == 'videos':
|
|
|
+ results.append(_video_result(result))
|
|
|
+ elif ddg_category == 'news':
|
|
|
+ results.append(_news_result(result))
|
|
|
+ else:
|
|
|
+ raise ValueError(f"Invalid duckduckgo category: {ddg_category}")
|
|
|
|
|
|
return results
|