[mod] Open Library engine: revision of the engine (Paper result)

Revision of the engine / use of the result type Paper as well as other
typifications.

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
Markus Heiser 2025-09-10 16:42:39 +02:00 committed by Markus Heiser
parent 0691e50e13
commit 96e63df8ca
3 changed files with 90 additions and 44 deletions

View File

@ -0,0 +1,8 @@
.. _openlibrary engine:
============
Open Library
============
.. automodule:: searx.engines.openlibrary
:members:

View File

@ -1,71 +1,109 @@
# SPDX-License-Identifier: AGPL-3.0-or-later # SPDX-License-Identifier: AGPL-3.0-or-later
"""Open library (books) """`Open Library`_ is an open, editable library catalog, building towards a web
""" page for every book ever published.
from urllib.parse import urlencode
import re
.. _Open Library: https://openlibrary.org
Configuration
=============
The service sometimes takes a very long time to respond, the ``timeout`` may
need to be adjusted.
.. code:: yaml
- name: openlibrary
engine: openlibrary
shortcut: ol
timeout: 10
Implementations
===============
"""
from datetime import datetime
import typing as t
from urllib.parse import urlencode
from dateutil import parser from dateutil import parser
from searx.result_types import EngineResults
if t.TYPE_CHECKING:
from searx.extended_types import SXNG_Response
from searx.search.processors import OnlineParams
about = { about = {
'website': 'https://openlibrary.org', "website": "https://openlibrary.org",
'wikidata_id': 'Q1201876', "wikidata_id": "Q1201876",
'require_api_key': False, "require_api_key": False,
'use_official_api': False, "use_official_api": False,
'official_api_documentation': 'https://openlibrary.org/developers/api', "official_api_documentation": "https://openlibrary.org/developers/api",
} }
paging = True paging = True
categories = [] categories = ["general", "books"]
base_url = "https://openlibrary.org" base_url = "https://openlibrary.org"
search_api = "https://openlibrary.org/search.json"
"""The engine uses the API at the endpoint search.json_.
.. _search.json: https://openlibrary.org/dev/docs/api/search
"""
results_per_page = 10 results_per_page = 10
def request(query, params): def request(query: str, params: "OnlineParams") -> None:
args = { args = {
'q': query, "q": query,
'page': params['pageno'], "page": params["pageno"],
'limit': results_per_page, "limit": results_per_page,
"fields": "*",
} }
params['url'] = f"{base_url}/search.json?{urlencode(args)}" params["url"] = f"{search_api}?{urlencode(args)}"
return params logger.debug("REST API: %s", params["url"])
def _parse_date(date): def response(resp: "SXNG_Response") -> EngineResults:
try: res = EngineResults()
return parser.parse(date) json_data = resp.json()
except parser.ParserError:
return None
for item in json_data.get("docs", []):
def response(resp): cover = ""
results = [] if "lending_identifier_s" in item:
for item in resp.json().get("docs", []):
cover = None
if 'lending_identifier_s' in item:
cover = f"https://archive.org/services/img/{item['lending_identifier_s']}" cover = f"https://archive.org/services/img/{item['lending_identifier_s']}"
published = item.get('publish_date') published = item.get("publish_date")
if published: if published:
published_dates = [date for date in map(_parse_date, published) if date] published_dates = [date for date in map(_parse_date, published) if date]
if published_dates: if published_dates:
published = min(published_dates) published = min(published_dates)
if not published: if not published:
published = parser.parse(str(item.get('first_published_year'))) published = _parse_date(str(item.get("first_publish_year")))
result = { content = " / ".join(item.get("first_sentence", []))
'template': 'paper.html', res.add(
'url': f"{base_url}{item['key']}", res.types.Paper(
'title': item['title'], url=f"{base_url}/{item['key']}",
'content': re.sub(r"\{|\}", "", item['first_sentence'][0]) if item.get('first_sentence') else '', title=item["title"],
'isbn': item.get('isbn', [])[:5], content=content,
'authors': item.get('author_name', []), isbn=item.get("isbn", [])[:5],
'thumbnail': cover, authors=item.get("author_name", []),
'publishedDate': published, thumbnail=cover,
'tags': item.get('subject', [])[:10] + item.get('place', [])[:10], publishedDate=published,
} tags=item.get("subject", [])[:10] + item.get("place", [])[:10],
results.append(result) )
)
return res
return results
def _parse_date(date: str) -> datetime | None:
if not date:
return None
try:
return parser.parse(date)
except parser.ParserError:
return None

View File

@ -1517,7 +1517,7 @@ engines:
- name: openlibrary - name: openlibrary
engine: openlibrary engine: openlibrary
shortcut: ol shortcut: ol
timeout: 5 timeout: 10
disabled: true disabled: true
- name: openmeteo - name: openmeteo