Skip to content

Commit 468d93b

Browse files
committed
Fixing query, updating doc
1 parent e91e200 commit 468d93b

3 files changed

Lines changed: 45 additions & 37 deletions

File tree

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# EDAN python
44

5-
Basic python3 module to query the Smithsonian's Enterprise Digital Asset Network (EDAN).
5+
Basic python3 module to query the Smithsonian's Enterprise Digital Asset Network (EDAN). Requires Python 3.11 or higher.
66

77
## Installation
88

@@ -12,25 +12,23 @@ Install using pip:
1212
pip install edan
1313
```
1414

15-
## Load the module
16-
17-
Save the file `edan.py` and load it as a module:
15+
Then, load the module:
1816

1917
```python
2018
import edan
2119
```
2220

2321
## Search EDAN
2422

25-
Set your credentials and use `edan.searchEDAN()`:
23+
Set your credentials and use `edan.edan_metadata_search()`:
2624

2725
```python
2826
#EDAN creds
2927
AppID = "APP_ID"
3028
AppKey = "verylong_key"
3129

3230
#Search for images of orchids from Smithsonian Gardens
33-
results = edan.edan_metadata_search("orchids smithsonian gardens images", AppID, AppKey)
31+
results = edan.edan_metadata_search("orchids smithsonian gardens images", AppID=AppID, AppKey=AppKey)
3432

3533
#Number of results available for this search
3634
results['rowCount']
@@ -42,6 +40,7 @@ results_rows = results['rows']
4240
The function `edan_metadata_search()` takes these arguments:
4341

4442
* edan_query = Search items
43+
* fqs = JSON array of filter query parameters
4544
* AppID = Your AppID
4645
* AppKey = Your AppKey
4746
* rows = How many rows to return, max is 100, default is 10
@@ -52,7 +51,7 @@ The function `edan_metadata_search()` takes these arguments:
5251
```python
5352
import json
5453

55-
item = edan.edan_content_getcontent(results['rows'][0]['url'], AppID, AppKey)
54+
item = edan.edan_content_getcontent(results['rows'][0]['url'], AppID=AppID, AppKey=AppKey)
5655
```
5756

5857
The function `edan_content_getcontent()` takes these arguments (must provide `id` or `url`):

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "edan"
7-
version = "0.3"
7+
version = "0.4"
88
authors = [
99
{ name="Luis J. Villanueva", email="villanueval@si.edu" },
1010
]

src/edan/edan.py

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#!/usr/bin/env python3
22
#
33
# Search metadata in the EDAN API
4-
# v 0.3
4+
# v 0.4
55
#
66

77
import urllib.parse
88
import urllib.request
9-
import datetime
10-
import email.utils
9+
import time
1110
import uuid
1211
import hashlib
1312
import json
@@ -24,13 +23,13 @@ def query_edan(edan_q, url, AppID=None, AppKey=None):
2423
Execute the query
2524
"""
2625
# Date of request
27-
dt = datetime.datetime.now()
28-
RequestDate = email.utils.format_datetime(dt)
29-
# Generated uniquely for this request
30-
Nonce = str(uuid.uuid4()).replace('-', '')
26+
RequestDate = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
27+
# Nonce generated uniquely for this request
28+
# using UUIDv4
29+
Nonce = uuid.uuid4().hex
3130
# This will be the value of X-AuthContent, each element is joined by a single newline
3231
StringToSign = "{}\n{}\n{}\n{}".format(Nonce, edan_q, RequestDate, AppKey)
33-
# First hash using SHA1
32+
# Hash using SHA1
3433
HashedString = hashlib.sha1(StringToSign.encode('utf-8')).hexdigest()
3534
# Base64 encode
3635
EncodedString = b64encode(HashedString.encode('utf-8')).decode('utf-8')
@@ -53,75 +52,85 @@ def query_edan(edan_q, url, AppID=None, AppKey=None):
5352
return json.loads(data)
5453

5554

56-
def edan_metadata_search(edan_query, AppID=None, AppKey=None, rows=10, start=0):
55+
def edan_content_getcontent(id=None, url=None, AppID=None, AppKey=None):
5756
"""
58-
Returns structured data rows about the piece of content addressed.
57+
Get details from an item using an EDAN ID
5958
"""
6059
if AppID is None:
6160
raise ValueError("Missing AppID")
6261
if AppKey is None:
6362
raise ValueError("Missing AppKey")
6463
# Request
65-
edan_query = urllib.parse.quote_plus(edan_query)
66-
edan_q = "q={}&rows={}&start={}&facet=true".format(edan_query, rows, start)
64+
if id != None:
65+
edan_q = "id={}".format(id)
66+
elif url != None:
67+
edan_q = "url={}".format(url)
6768
# Put whole thing together
68-
url = 'https://edan.si.edu/metadata/v2.0/metadata/search.htm?{}'.format(edan_q)
69+
url = 'https://edan.si.edu/content/v2.0/content/getContent.htm?{}'.format(edan_q)
6970
# Execute query
7071
result = query_edan(edan_q, url, AppID, AppKey)
7172
return result
7273

7374

74-
def edan_metadata_suggestions(edan_query, AppID=None, AppKey=None, rows=10, start=0):
75+
def edan_metadata_search(edan_query=None, fqs=None, AppID=None, AppKey=None, rows=10, start=0):
7576
"""
76-
Returns suggested content for a given search term.
77+
Returns structured data rows about the piece of content addressed.
7778
"""
7879
if AppID is None:
7980
raise ValueError("Missing AppID")
8081
if AppKey is None:
8182
raise ValueError("Missing AppKey")
8283
# Request
83-
edan_query = urllib.parse.quote_plus(edan_query)
84-
edan_q = "suggest={}".format(edan_query)
84+
if edan_query is None and fqs is None:
85+
raise ValueError("Missing edan_query or fqs")
86+
if edan_query is not None:
87+
edan_query = urllib.parse.quote_plus(edan_query)
88+
else:
89+
edan_query = ""
90+
if fqs is not None:
91+
fqs_query = urllib.parse.quote_plus(fqs)
92+
else:
93+
fqs_query = ""
94+
edan_q = "q={}&fqs={}rows={}&start={}&facet=true".format(edan_query, fqs_query, rows, start)
8595
# Put whole thing together
86-
url = 'https://edan.si.edu/metadata/v2.0/metadata/getSuggestions.htm?{}'.format(edan_q)
96+
url = 'https://edan.si.edu/metadata/v2.0/metadata/search.htm?{}'.format(edan_q)
8797
# Execute query
8898
result = query_edan(edan_q, url, AppID, AppKey)
8999
return result
90100

91101

92-
def edan_metadata_getFacets(edan_query, AppID=None, AppKey=None, limit=100, mincount=1, offset=0, sort="index"):
102+
def edan_metadata_suggestions(edan_query, AppID=None, AppKey=None):
93103
"""
94-
Returns facets found for a given search term. All requests will timeout after 30 seconds and may return incomplete results; measuring performance is required before implementing in a production environment.
104+
Returns suggested content for a given search term.
95105
"""
96106
if AppID is None:
97107
raise ValueError("Missing AppID")
98108
if AppKey is None:
99109
raise ValueError("Missing AppKey")
100110
# Request
101111
edan_query = urllib.parse.quote_plus(edan_query)
102-
edan_q = "q={}&limit={}&mincount={}&offset={}&sort={}".format(edan_query, limit, mincount, offset, sort)
112+
edan_q = "suggest={}".format(edan_query)
103113
# Put whole thing together
104-
url = 'https://edan.si.edu/metadata/v2.0/metadata/getFacets.htm?{}'.format(edan_q)
114+
url = 'https://edan.si.edu/metadata/v2.0/metadata/getSuggestions.htm?{}'.format(edan_q)
105115
# Execute query
106116
result = query_edan(edan_q, url, AppID, AppKey)
107117
return result
108118

109119

110-
def edan_content_getcontent(id=None, url=None, AppID=None, AppKey=None):
120+
def edan_metadata_getFacets(edan_query, AppID=None, AppKey=None, limit=100, mincount=1, offset=0, sort="index"):
111121
"""
112-
Get details from an item using an EDAN ID
122+
Returns facets found for a given search term. All requests will timeout after 30 seconds and may return incomplete results; measuring performance is required before implementing in a production environment.
113123
"""
114124
if AppID is None:
115125
raise ValueError("Missing AppID")
116126
if AppKey is None:
117127
raise ValueError("Missing AppKey")
118128
# Request
119-
if id != None:
120-
edan_q = "id={}".format(id)
121-
elif url != None:
122-
edan_q = "url={}".format(url)
129+
edan_query = urllib.parse.quote_plus(edan_query)
130+
edan_q = "q={}&limit={}&mincount={}&offset={}&sort={}".format(edan_query, limit, mincount, offset, sort)
123131
# Put whole thing together
124-
url = 'https://edan.si.edu/content/v2.0/content/getContent.htm?{}'.format(edan_q)
132+
url = 'https://edan.si.edu/metadata/v2.0/metadata/getFacets.htm?{}'.format(edan_q)
125133
# Execute query
126134
result = query_edan(edan_q, url, AppID, AppKey)
127135
return result
136+

0 commit comments

Comments
 (0)