mirror of
https://github.com/searxng/searxng.git
synced 2026-09-23 06:36:12 +00:00
[mod] engine: pexels - added type hints and migrate to Image-Results (#6739)
Code style somewhat unified, type annotations added, ``LegacyResult`` replaced by ``Image``, but no functional modifications. Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
@@ -13,18 +13,21 @@ from searx.enginelib import EngineCache
|
|||||||
from searx.exceptions import SearxEngineAPIException, SearxEngineAccessDeniedException
|
from searx.exceptions import SearxEngineAPIException, SearxEngineAccessDeniedException
|
||||||
from searx.network import get
|
from searx.network import get
|
||||||
|
|
||||||
# about
|
if t.TYPE_CHECKING:
|
||||||
|
from extended_types import SXNG_Response
|
||||||
|
from search.processors.online import OnlineParams
|
||||||
|
|
||||||
about = {
|
about = {
|
||||||
"website": 'https://www.pexels.com',
|
"website": "https://www.pexels.com",
|
||||||
"wikidata_id": 'Q101240504',
|
"wikidata_id": "Q101240504",
|
||||||
"official_api_documentation": 'https://www.pexels.com/api/',
|
"official_api_documentation": "https://www.pexels.com/api/",
|
||||||
"use_official_api": False,
|
"use_official_api": False,
|
||||||
"require_api_key": False,
|
"require_api_key": False,
|
||||||
"results": 'JSON',
|
"results": "JSON",
|
||||||
}
|
}
|
||||||
|
|
||||||
base_url = 'https://www.pexels.com'
|
base_url = "https://www.pexels.com"
|
||||||
categories = ['images']
|
categories = ["images"]
|
||||||
|
|
||||||
api_key = "H2jk9uKnhRmL6WPwh89zBezWvr"
|
api_key = "H2jk9uKnhRmL6WPwh89zBezWvr"
|
||||||
"""
|
"""
|
||||||
@@ -34,7 +37,7 @@ results_per_page = 20
|
|||||||
|
|
||||||
paging = True
|
paging = True
|
||||||
time_range_support = True
|
time_range_support = True
|
||||||
time_range_map = {'day': 'last_24_hours', 'week': 'last_week', 'month': 'last_month', 'year': 'last_year'}
|
time_range_map = {"day": "last_24_hours", "week": "last_week", "month": "last_month", "year": "last_year"}
|
||||||
|
|
||||||
SECRET_KEY_RE = re.compile('"secret-key":\b*"(.*?)"')
|
SECRET_KEY_RE = re.compile('"secret-key":\b*"(.*?)"')
|
||||||
SECRET_KEY_DB_KEY = "secret-key"
|
SECRET_KEY_DB_KEY = "secret-key"
|
||||||
@@ -73,14 +76,14 @@ def _get_secret_key():
|
|||||||
raise SearxEngineAPIException("failed to obtain secret key")
|
raise SearxEngineAPIException("failed to obtain secret key")
|
||||||
|
|
||||||
|
|
||||||
def request(query, params):
|
def request(query: str, params: "OnlineParams"):
|
||||||
args = {
|
args = {
|
||||||
'query': query,
|
"query": query,
|
||||||
'page': params['pageno'],
|
"page": params["pageno"],
|
||||||
'per_page': results_per_page,
|
"per_page": results_per_page,
|
||||||
}
|
}
|
||||||
if params['time_range']:
|
if params["time_range"]:
|
||||||
args['date_from'] = time_range_map[params['time_range']]
|
args["date_from"] = time_range_map[params["time_range"]]
|
||||||
|
|
||||||
params["url"] = f"{base_url}/en-us/api/v3/search/photos?{urlencode(args)}"
|
params["url"] = f"{base_url}/en-us/api/v3/search/photos?{urlencode(args)}"
|
||||||
|
|
||||||
@@ -97,24 +100,21 @@ def request(query, params):
|
|||||||
params["headers"]["secret-key"] = secret_key
|
params["headers"]["secret-key"] = secret_key
|
||||||
|
|
||||||
|
|
||||||
def response(resp):
|
def response(resp: "SXNG_Response") -> EngineResults:
|
||||||
res = EngineResults()
|
res = EngineResults()
|
||||||
json_data = resp.json()
|
json_data = resp.json()
|
||||||
|
|
||||||
for result in json_data.get('data', []):
|
for result in json_data.get("data", []):
|
||||||
attrs = result["attributes"]
|
attrs = result["attributes"]
|
||||||
res.add(
|
res.add(
|
||||||
res.types.LegacyResult(
|
res.types.Image(
|
||||||
{
|
url=f"{base_url}/photo/{attrs['slug']}-{attrs['id']}/",
|
||||||
'template': 'images.html',
|
title=attrs["title"],
|
||||||
'url': f"{base_url}/photo/{attrs['slug']}-{attrs['id']}/",
|
content=attrs["description"],
|
||||||
'title': attrs["title"],
|
thumbnail_src=attrs["image"]["small"],
|
||||||
'content': attrs["description"],
|
img_src=attrs["image"]["download_link"],
|
||||||
'thumbnail_src': attrs["image"]["small"],
|
resolution=f"{attrs['width']}x{attrs['height']}",
|
||||||
'img_src': attrs["image"]["download_link"],
|
author=f"{attrs['user']['username']}",
|
||||||
'resolution': f"{attrs['width']}x{attrs['height']}",
|
|
||||||
'author': f"{attrs['user']['username']}",
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user