-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCdmApi.py
68 lines (52 loc) · 1.71 KB
/
CdmApi.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import math
import requests
from lxml import objectify
import pprint
BASEURL = 'https://server21033.contentdm.oclc.org' # CONTENTdm backend URL
APIURL = BASEURL + '/dmwebservices/index.php'
def getMetadata(nick, ptr):
url = (APIURL) + '?q=dmGetItemInfo/%s/%s/json' % (nick, ptr)
print(url)
response = requests.get(url)
return response.json()
def getCpdPages(nick, ptr):
url = (APIURL) + '?q=dmGetCompoundObjectInfo/%s/%s/json' % (nick, ptr)
print(url)
response = requests.get(url)
return response.json()
def getTotalRec(nick):
url = (APIURL) + '?q=dmQueryTotalRecs/%s|0/xml' % (nick)
response = requests.get(url)
obj = objectify.fromstring(response.content)
return obj.totalrecs.total
def query(nick, searchstrings, field, sortby, maxrecs, resume):
url = (APIURL) + '?q=dmQuery/%s/%s/%s/%s/%s/%s/json' % (nick, searchstrings, field, sortby, maxrecs, resume)
print(url)
response = requests.get(url)
return response.json()
def getAllPtr(nick, maxitems=0):
if maxitems > 0:
cnt = maxitems
else:
cnt = getTotalRec(nick)
print('found %s records in collection %s...' % (cnt, nick))
get = 100
ptrs = []
iters = math.ceil(cnt / get)
print(iters)
for c in range(0, iters+1):
resume = (c * get) + 1
print('get %s results starting at %s...' % (get, resume))
r = query(nick, '*', 'title', 'title', get, resume)
for record in r['records']:
ptr = record['pointer']
ptrs.append(ptr)
return ptrs
def isCpd(nick, ptr):
record = getMetadata(nick, ptr)
file = record['find']
a = file.split('.')
if a[1] == 'cpd':
return True
else:
return False