on master help sign in
Merge pull request #2544 from mrwormo/congresslibrary

[Engine] Add Library of Congress engine

commit436d366448131088a9cddcedc9b789a44b38ee97 authorAlexandre Flament <alex@al-f.net> dateWed 10 Feb 2021, 10:13 (6 years ago) parenteafd27f051da88
2 files changed+73
searx/engines/loc.py+68 searx/settings.yml+5
searx/engines/loc.pynew file+68
@@ -0,0 +1,68 @@
1+# SPDX-License-Identifier: AGPL-3.0-or-later
2+"""
3+
4+ Library of Congress : images from Prints and Photographs Online Catalog
5+
6+"""
7+
8+from json import loads
9+from urllib.parse import urlencode
10+
11+
12+about = {
13+ "website": 'https://www.loc.gov/pictures/',
14+ "wikidata_id": 'Q131454',
15+ "official_api_documentation": 'https://www.loc.gov/pictures/api',
16+ "use_official_api": True,
17+ "require_api_key": False,
18+ "results": 'JSON',
19+}
20+
21+categories = ['images']
22+
23+paging = True
24+
25+base_url = 'https://loc.gov/pictures/search/?'
26+search_string = "&sp={page}&{query}&fo=json"
27+
28+IMG_SRC_FIXES = {
29+ 'https://tile.loc.gov/storage-services/': 'https://tile.loc.gov/storage-services/',
30+ 'https://loc.gov/pictures/static/images/': 'https://tile.loc.gov/storage-services/',
31+ 'https://www.loc.gov/pictures/cdn/': 'https://tile.loc.gov/storage-services/',
32+}
33+
34+
35+def request(query, params):
36+
37+ search_path = search_string.format(
38+ query=urlencode({'q': query}),
39+ page=params['pageno'])
40+
41+ params['url'] = base_url + search_path
42+
43+ return params
44+
45+
46+def response(resp):
47+ results = []
48+
49+ json_data = loads(resp.text)
50+
51+ for result in json_data['results']:
52+ img_src = result['image']['full']
53+ for url_prefix, url_replace in IMG_SRC_FIXES.items():
54+ if img_src.startswith(url_prefix):
55+ img_src = img_src.replace(url_prefix, url_replace)
56+ break
57+ else:
58+ img_src = result['image']['thumb']
59+ results.append({
60+ 'url': result['links']['item'],
61+ 'title': result['title'],
62+ 'img_src': img_src,
63+ 'thumbnail_src': result['image']['thumb'],
64+ 'author': result['creator'],
65+ 'template': 'images.html'
66+ })
67+
68+ return results
searx/settings.yml+5
@@ -679,6 +679,11 @@ engines:
679679 require_api_key: false
680680 results: HTML
681681
682+ - name : library of congress
683+ engine : loc
684+ shortcut : loc
685+ categories : images
686+
682687 - name : lobste.rs
683688 engine : xpath
684689 search_url : https://lobste.rs/search?utf8=%E2%9C%93&q={query}&what=stories&order=relevance