.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
openlibrary.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Open library (books)
3"""
4from urllib.parse import urlencode
5import re
6
7from dateutil import parser
8
9about = {
10 'website': 'https://openlibrary.org',
11 'wikidata_id': 'Q1201876',
12 'require_api_key': False,
13 'use_official_api': False,
14 'official_api_documentation': 'https://openlibrary.org/developers/api',
15}
16
17paging = True
18categories = []
19
20base_url = "https://openlibrary.org"
21results_per_page = 10
22
23
24def request(query, params):
25 args = {
26 'q': query,
27 'page': params['pageno'],
28 'limit': results_per_page,
29 }
30 params['url'] = f"{base_url}/search.json?{urlencode(args)}"
31 return params
32
33
34def _parse_date(date):
35 try:
36 return parser.parse(date)
37 except parser.ParserError:
38 return None
39
40
41def response(resp):
42 results = []
43
44 for item in resp.json().get("docs", []):
45 cover = None
46 if 'lending_identifier_s' in item:
47 cover = f"https://archive.org/services/img/{item['lending_identifier_s']}"
48
49 published = item.get('publish_date')
50 if published:
51 published_dates = [date for date in map(_parse_date, published) if date]
52 if published_dates:
53 published = min(published_dates)
54
55 if not published:
56 published = parser.parse(str(item.get('first_published_year')))
57
58 result = {
59 'template': 'paper.html',
60 'url': f"{base_url}{item['key']}",
61 'title': item['title'],
62 'content': re.sub(r"\{|\}", "", item['first_sentence'][0]) if item.get('first_sentence') else '',
63 'isbn': item.get('isbn', [])[:5],
64 'authors': item.get('author_name', []),
65 'thumbnail': cover,
66 'publishedDate': published,
67 'tags': item.get('subject', [])[:10] + item.get('place', [])[:10],
68 }
69 results.append(result)
70
71 return results