@@ -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