.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
loc.py
Go to the documentation of this file.
1
# SPDX-License-Identifier: AGPL-3.0-or-later
2
"""Library of Congress: query Photo, Print and Drawing from API endpoint_
3
``photos``.
4
5
.. _endpoint: https://www.loc.gov/apis/json-and-yaml/requests/endpoints/
6
7
.. note::
8
9
Beside the ``photos`` endpoint_ there are more endpoints available / we are
10
looking forward for contributions implementing more endpoints.
11
12
"""
13
14
from
urllib.parse
import
urlencode
15
from
searx.network
import
raise_for_httperror
16
17
about = {
18
"website"
:
'https://www.loc.gov/pictures/'
,
19
"wikidata_id"
:
'Q131454'
,
20
"official_api_documentation"
:
'https://www.loc.gov/api'
,
21
"use_official_api"
:
True
,
22
"require_api_key"
:
False
,
23
"results"
:
'JSON'
,
24
}
25
26
categories = [
'images'
]
27
paging =
True
28
29
endpoint =
'photos'
30
base_url =
'https://loc.gov'
31
search_string =
"/{endpoint}/?sp={page}&{query}&fo=json"
32
33
34
def
request
(query, params):
35
36
search_path = search_string.format(
37
endpoint=endpoint,
38
query=urlencode({
'q'
: query}),
39
page=params[
'pageno'
],
40
)
41
params[
'url'
] = base_url + search_path
42
params[
'raise_for_httperror'
] =
False
43
return
params
44
45
46
def
response
(resp):
47
48
results = []
49
json_data = resp.json()
50
51
json_results = json_data.get(
'results'
)
52
if
not
json_results:
53
# when a search term has none results, loc sends a JSON in a HTTP 404
54
# response and the HTTP status code is set in the 'status' element.
55
if
json_data.get(
'status'
) == 404:
56
return
results
57
58
raise_for_httperror(resp)
59
60
for
result
in
json_results:
61
62
url = result[
"item"
].get(
"link"
)
63
if
not
url:
64
continue
65
66
img_src = result[
'item'
].get(
'service_medium'
)
67
if
not
img_src
or
img_src ==
'https://memory.loc.gov/pp/grp.gif'
:
68
continue
69
70
title = result[
'title'
]
71
if
title.startswith(
'['
):
72
title = title.strip(
'[]'
)
73
74
content_items = [
75
result[
'item'
].get(
'created_published_date'
),
76
result[
'item'
].get(
'summary'
, [
None
])[0],
77
result[
'item'
].get(
'notes'
, [
None
])[0],
78
result[
'item'
].get(
'part_of'
, [
None
])[0],
79
]
80
81
author =
None
82
if
result[
'item'
].get(
'creators'
):
83
author = result[
'item'
][
'creators'
][0][
'title'
]
84
85
results.append(
86
{
87
'template'
:
'images.html'
,
88
'url'
: url,
89
'title'
: title,
90
'content'
:
' / '
.join([i
for
i
in
content_items
if
i]),
91
'img_src'
: img_src,
92
'thumbnail_src'
: result[
'item'
].get(
'thumb_gallery'
),
93
'author'
: author,
94
}
95
)
96
97
return
results
searx.engines.loc.request
request(query, params)
Definition
loc.py:34
searx.engines.loc.response
response(resp)
Definition
loc.py:46
searx.network
Definition
__init__.py:1
searxng
searx
engines
loc.py
Generated on Sat Nov 16 2024 00:10:57 for .oO SearXNG Developer Documentation Oo. by
1.12.0