From c999cfb422605e3812e119a7470c07d79896202d Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 17 Sep 2023 20:14:05 +0200 Subject: [PATCH] [feat] engine: implementation of wallhaven --- docs/dev/engines/online/wallhaven.rst | 13 +++++ searx/engines/wallhaven.py | 84 +++++++++++++++++++++++++++ searx/settings.yml | 5 ++ 3 files changed, 102 insertions(+) create mode 100644 docs/dev/engines/online/wallhaven.rst create mode 100644 searx/engines/wallhaven.py diff --git a/docs/dev/engines/online/wallhaven.rst b/docs/dev/engines/online/wallhaven.rst new file mode 100644 index 000000000..b473293ca --- /dev/null +++ b/docs/dev/engines/online/wallhaven.rst @@ -0,0 +1,13 @@ +.. _wallhaven engine: + +========= +Wallhaven +========= + +.. contents:: Contents + :depth: 2 + :local: + :backlinks: entry + +.. automodule:: searx.engines.wallhaven + :members: diff --git a/searx/engines/wallhaven.py b/searx/engines/wallhaven.py new file mode 100644 index 000000000..f80b5d86d --- /dev/null +++ b/searx/engines/wallhaven.py @@ -0,0 +1,84 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# lint: pylint +"""Wallhaven_ is a site created by and for people who like wallpapers. + +.. _Wallhaven: https://wallhaven.cc/about#Copyright +""" + +from datetime import datetime +from urllib.parse import urlencode + +about = { + 'website': 'https://wallhaven.cc/', + 'official_api_documentation': 'https://wallhaven.cc/help/api', + 'use_official_api': True, + 'require_api_key': False, + 'results': 'JSON', +} +categories = ['images'] +paging = True + +base_url = "https://wallhaven.cc" + +api_key = '' +"""If you own an API key you can add it here, further read `Rate Limiting and +Errors`_. + +.. _Rate Limiting and Errors: https://wallhaven.cc/help/api#limits + +""" + +# Possible categories: sfw, sketchy, nsfw +safesearch_map = {0: '111', 1: '110', 2: '100'} +"""Turn purities on(1) or off(0) NSFW requires a valid API key. + +.. code:: text + + 100/110/111 <-- Bits stands for: SFW, Sketchy and NSFW + +`What are SFW, Sketchy and NSFW all about?`_: + +- SFW = "Safe for work" wallpapers. *Grandma approves.* +- Sketchy = Not quite SFW not quite NSFW. *Grandma might be uncomfortable.* +- NSFW = "Not safe for work". *Grandma isn't sure who you are anymore.* + +.. _What are SFW, Sketchy and NSFW all about?: + https://wallhaven.cc/faq#What-are-SFW-Sketchy-and-NSFW-all-about + +""" + + +def request(query, params): + args = { + 'q': query, + 'page': params['pageno'], + 'purity': safesearch_map[params['safesearch']], + } + + if api_key: + params['api_key'] = api_key + + params['url'] = f"{base_url}/api/v1/search?{urlencode(args)}" + return params + + +def response(resp): + results = [] + + json = resp.json() + + for result in json['data']: + results.append( + { + 'template': 'images.html', + 'title': '', + 'content': f"{result['category']} / {result['purity']}", + 'url': result['url'], + 'img_src': result['path'], + 'thumbnail_src': result['thumbs']['small'], + 'img_format': result['resolution'], + 'publishedDate': datetime.strptime(result['created_at'], '%Y-%m-%d %H:%M:%S'), + } + ) + + return results diff --git a/searx/settings.yml b/searx/settings.yml index 0eb410a13..ff30f509a 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -1908,6 +1908,11 @@ engines: timeout: 10.0 disabled: true + - name: wallhaven + engine: wallhaven + # api_key: abcdefghijklmnopqrstuvwxyz + shortcut: wh + # wikimini: online encyclopedia for children # The fulltext and title parameter is necessary for Wikimini because # sometimes it will not show the results and redirect instead