-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdpla.py
More file actions
40 lines (31 loc) · 1.4 KB
/
dpla.py
File metadata and controls
40 lines (31 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import requests, html_wrapper
def query(query: str):
# // Put your API key here
api_key: str = "0f50a5575c58e285fc8646f1cf1aeecd"
# // Make a request to the DPLA API
r = requests.get(f"https://api.dp.la/v2/items?q={query}&api_key={api_key}", headers={
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
}, timeout=5)
# // Declare an empty response string
response_str: str = ""
# // Loop through the api results
for doc in r.json()["docs"]:
data_provider_name: str = doc["dataProvider"]["name"]
resource_url: str = doc["isShownAt"]
# // Get the title from the api response
resource_title: str = doc["sourceResource"]["title"][0]
# // Get the description from the api response
if "description" not in doc["sourceResource"]:
continue
# // Join the description list into a string
resource_description: str = ("".join(
f"{desc}<br>" for desc in doc["sourceResource"]["description"]
))[:300] + "...<br>"
# // Add data to html response string
response_str += html_wrapper.search_result(
f"{data_provider_name}: {resource_title}",
resource_description,
resource_url,
)
# // Return the response string
return response_str