-
Notifications
You must be signed in to change notification settings - Fork 3
/
theta.py
551 lines (471 loc) · 19 KB
/
theta.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
from nose.tools import set_trace
from collections import defaultdict
import datetime
import base64
import os
import json
import logging
import re
from config import (
Configuration,
temp_config,
)
from util import LanguageCodes
from util.xmlparser import XMLParser
from util.http import (
HTTP,
RemoteIntegrationException,
)
from coverage import CoverageFailure
from model import (
Contributor,
DataSource,
DeliveryMechanism,
LicensePool,
Edition,
Identifier,
Representation,
Subject,
)
from metadata_layer import (
SubjectData,
ContributorData,
FormatData,
IdentifierData,
CirculationData,
Metadata,
)
from config import Configuration
from coverage import BibliographicCoverageProvider
class ThetaAPI(object):
PRODUCTION_BASE_URL = "http://acstheta.dlogics.com/bookstore/api"
QA_BASE_URL = "http://thetaapiqa.baker-taylor.com/Services/VendorAPI/"
DATE_FORMAT = "%m-%d-%Y %H:%M:%S"
access_token_endpoint = 'accesstoken'
availability_endpoint = 'availability/v2'
log = logging.getLogger("Theta API")
def __init__(self, _db, username=None, library_id=None, password=None,
base_url=None):
self._db = _db
(env_library_id, env_username,
env_password, env_base_url) = self.environment_values()
self.library_id = library_id or env_library_id
self.username = username or env_username
self.password = password or env_password
self.base_url = base_url or env_base_url
if self.base_url == 'qa':
self.base_url = self.QA_BASE_URL
elif self.base_url == 'production':
self.base_url = self.PRODUCTION_BASE_URL
self.token = "mock_token"
@classmethod
def environment_values(cls):
config = Configuration.integration('Theta')
values = []
for name in [
'library_id',
'username',
'password',
'server',
]:
value = config.get(name)
if value:
value = value.encode("utf8")
values.append(value)
return values
@classmethod
def from_environment(cls, _db):
# Make sure all environment values are present. If any are missing,
# return None
values = cls.environment_values()
if len([x for x in values if not x]):
cls.log.info(
"No Theta client configured."
)
return None
return cls(_db)
@property
def source(self):
return DataSource.lookup(self._db, DataSource.THETA)
@property
def authorization_headers(self):
authorization = u":".join([self.username, self.password, self.library_id])
authorization = authorization.encode("utf_16_le")
authorization = base64.b64encode(authorization)
return dict(Authorization="Basic " + authorization)
def refresh_bearer_token(self):
url = self.base_url + self.access_token_endpoint
headers = self.authorization_headers
response = self._make_request(
url, 'post', headers, allowed_response_codes=[200]
)
return self.parse_token(response.content)
def request(self, url, method='get', extra_headers={}, data=None,
params=None, exception_on_401=False):
"""Make an HTTP request, acquiring/refreshing a bearer token
if necessary.
"""
if not self.token:
self.token = self.refresh_bearer_token()
headers = dict(extra_headers)
headers['Authorization'] = "Bearer " + self.token
headers['Library'] = self.library_id
if exception_on_401:
disallowed_response_codes = ["401"]
else:
disallowed_response_codes = None
response = self._make_request(
url=url, method=method, headers=headers,
data=data, params=params,
disallowed_response_codes=disallowed_response_codes
)
if response.status_code == 401:
# This must be our first 401, since our second 401 will
# make _make_request raise a RemoteIntegrationException.
#
# The token has expired. Get a new token and try again.
self.token = None
return self.request(
url=url, method=method, extra_headers=extra_headers,
data=data, params=params, exception_on_401=True
)
else:
return response
def availability(self, patron_id=None, since=None, title_ids=[]):
url = self.base_url + self.availability_endpoint
args = dict()
if since:
since = since.strftime(self.DATE_FORMAT)
args['updatedDate'] = since
if patron_id:
args['patronId'] = patron_id
if title_ids:
args['titleIds'] = ','.join(title_ids)
response = self.request(url, params=args)
return response
@classmethod
def create_identifier_strings(cls, identifiers):
identifier_strings = []
for i in identifiers:
if isinstance(i, Identifier):
value = i.identifier
else:
value = i
identifier_strings.append(value)
return identifier_strings
@classmethod
def parse_token(cls, token):
data = json.loads(token)
return data['access_token']
def _make_request(self, url, method, headers, data=None, params=None,
**kwargs):
"""Actually make an HTTP request."""
return HTTP.request_with_timeout(
method, url, headers=headers, data=data,
params=params, **kwargs
)
class MockThetaAPI(ThetaAPI):
def __init__(self, _db, with_token=True, *args, **kwargs):
with temp_config() as config:
config[Configuration.INTEGRATIONS]['Theta'] = {
'library_id' : 'a',
'username' : 'b',
'password' : 'c',
'server' : 'http://theta.test/',
}
super(MockThetaAPI, self).__init__(_db, *args, **kwargs)
if with_token:
self.token = "mock token"
self.responses = []
self.requests = []
def queue_response(self, status_code, headers={}, content=None):
from testing import MockRequestsResponse
self.responses.insert(
0, MockRequestsResponse(status_code, headers, content)
)
def _make_request(self, url, *args, **kwargs):
self.requests.append([url, args, kwargs])
response = self.responses.pop()
return HTTP._process_response(
url, response, kwargs.get('allowed_response_codes'),
kwargs.get('disallowed_response_codes')
)
class ThetaBibliographicCoverageProvider(BibliographicCoverageProvider):
"""Fill in bibliographic metadata for Theta records.
Currently this is only used by BibliographicRefreshScript. It's
not normally necessary because the Theta API combines
bibliographic and availability data.
"""
def __init__(self, _db, input_identifier_types=None,
metadata_replacement_policy=None, theta_api=None,
**kwargs):
# We ignore the value of input_identifier_types, but it's
# passed in by RunCoverageProviderScript, so we accept it as
# part of the signature.
self.parser = BibliographicParser()
theta_api = theta_api or ThetaAPI(_db)
super(ThetaBibliographicCoverageProvider, self).__init__(
_db, theta_api, DataSource.THETA,
batch_size=25,
metadata_replacement_policy=metadata_replacement_policy,
**kwargs
)
def process_batch(self, identifiers):
identifier_strings = self.api.create_identifier_strings(identifiers)
response = self.api.availability(title_ids=identifier_strings)
seen_identifiers = set()
batch_results = []
for metadata, availability in self.parser.process_all(response.content):
identifier, is_new = metadata.primary_identifier.load(self._db)
if not identifier in identifiers:
# Theta told us about a book we didn't ask
# for. This shouldn't happen, but if it does we should
# do nothing further.
continue
seen_identifiers.add(identifier.identifier)
result = self.set_metadata(identifier, metadata)
if not isinstance(result, CoverageFailure):
result = self.handle_success(identifier)
batch_results.append(result)
# Create a CoverageFailure object for each original identifier
# not mentioned in the results.
for identifier_string in identifier_strings:
if identifier_string not in seen_identifiers:
identifier, ignore = Identifier.for_foreign_id(
self._db, Identifier.THETA_ID, identifier_string
)
result = CoverageFailure(
identifier, "Book not in collection", data_source=self.output_source, transient=False
)
batch_results.append(result)
return batch_results
def handle_success(self, identifier):
return self.set_presentation_ready(identifier)
def process_item(self, identifier):
results = self.process_batch([identifier])
return results[0]
class ThetaParser(XMLParser):
NS = {"axis": "http://axis360api.baker-taylor.com/vendorAPI"}
SHORT_DATE_FORMAT = "%m/%d/%Y"
FULL_DATE_FORMAT_IMPLICIT_UTC = "%m/%d/%Y %I:%M:%S %p"
FULL_DATE_FORMAT = "%m/%d/%Y %I:%M:%S %p +00:00"
def _xpath1_boolean(self, e, target, ns, default=False):
text = self.text_of_optional_subtag(e, target, ns)
if text is None:
return default
if text == 'true':
return True
else:
return False
def _xpath1_date(self, e, target, ns):
value = self.text_of_optional_subtag(e, target, ns)
if value is None:
return value
try:
attempt = datetime.datetime.strptime(
value, self.FULL_DATE_FORMAT_IMPLICIT_UTC)
value += ' +00:00'
except ValueError:
pass
return datetime.datetime.strptime(value, self.FULL_DATE_FORMAT)
class BibliographicParser(ThetaParser):
DELIVERY_DATA_FOR_THETA_FORMAT = {
"Blio" : None,
"Acoustik" : None,
"ePub" : (Representation.EPUB_MEDIA_TYPE, DeliveryMechanism.ADOBE_DRM),
"PDF" : (Representation.PDF_MEDIA_TYPE, DeliveryMechanism.ADOBE_DRM),
}
log = logging.getLogger("Theta Bibliographic Parser")
@classmethod
def parse_list(self, l):
"""Turn strings like this into lists:
FICTION / Thrillers; FICTION / Suspense; FICTION / General
Ursu, Anne ; Fortune, Eric (ILT)
"""
return [x.strip() for x in l.split(";")]
def __init__(self, include_availability=True, include_bibliographic=True):
self.include_availability = include_availability
self.include_bibliographic = include_bibliographic
def process_all(self, string):
for i in super(BibliographicParser, self).process_all(
string, "//axis:title", self.NS):
yield i
def extract_availability(self, circulation_data, element, ns):
identifier = self.text_of_subtag(element, 'axis:titleId', ns)
primary_identifier = IdentifierData(Identifier.THETA_ID, identifier)
if not circulation_data:
circulation_data = CirculationData(
data_source=DataSource.THETA,
primary_identifier=primary_identifier,
)
availability = self._xpath1(element, 'axis:availability', ns)
total_copies = self.int_of_subtag(availability, 'axis:totalCopies', ns)
available_copies = self.int_of_subtag(
availability, 'axis:availableCopies', ns)
size_of_hold_queue = self.int_of_subtag(
availability, 'axis:holdsQueueSize', ns)
availability_updated = self.text_of_optional_subtag(
availability, 'axis:updateDate', ns)
if availability_updated:
try:
attempt = datetime.datetime.strptime(
availability_updated, self.FULL_DATE_FORMAT_IMPLICIT_UTC)
availability_updated += ' +00:00'
except ValueError:
pass
availability_updated = datetime.datetime.strptime(
availability_updated, self.FULL_DATE_FORMAT)
circulation_data.licenses_owned=total_copies
circulation_data.licenses_available=available_copies
circulation_data.licenses_reserved=0
circulation_data.patrons_in_hold_queue=size_of_hold_queue
return circulation_data
# Theta authors with a special role have an abbreviation after their names,
# e.g. "San Ruby (FRW)"
role_abbreviation = re.compile("\(([A-Z][A-Z][A-Z])\)$")
generic_author = object()
role_abbreviation_to_role = dict(
INT=Contributor.INTRODUCTION_ROLE,
EDT=Contributor.EDITOR_ROLE,
PHT=Contributor.PHOTOGRAPHER_ROLE,
ILT=Contributor.ILLUSTRATOR_ROLE,
TRN=Contributor.TRANSLATOR_ROLE,
FRW=Contributor.FOREWORD_ROLE,
ADP=generic_author, # Author of adaptation
COR=generic_author, # Corporate author
)
@classmethod
def parse_contributor(cls, author, primary_author_found=False):
if primary_author_found:
default_author_role = Contributor.AUTHOR_ROLE
else:
default_author_role = Contributor.PRIMARY_AUTHOR_ROLE
role = default_author_role
match = cls.role_abbreviation.search(author)
if match:
role_type = match.groups()[0]
role = cls.role_abbreviation_to_role.get(
role_type, Contributor.UNKNOWN_ROLE)
if role is cls.generic_author:
role = default_author_role
author = author[:-5].strip()
return ContributorData(
sort_name=author, roles=role)
def extract_bibliographic(self, element, ns):
"""Turn bibliographic metadata into a Metadata and a CirculationData objects,
and return them as a tuple."""
# TODO: These are consistently empty (some are clearly for
# audiobooks) so I don't know what they do and/or what format
# they're in.
#
# annotation
# edition
# narrator
# runtime
identifier = self.text_of_subtag(element, 'axis:titleId', ns)
isbn = self.text_of_optional_subtag(element, 'axis:isbn', ns)
title = self.text_of_subtag(element, 'axis:productTitle', ns)
contributor = self.text_of_optional_subtag(
element, 'axis:contributor', ns)
contributors = []
found_primary_author = False
if contributor:
for c in self.parse_list(contributor):
contributor = self.parse_contributor(
c, found_primary_author)
if Contributor.PRIMARY_AUTHOR_ROLE in contributor.roles:
found_primary_author = True
contributors.append(contributor)
subject = self.text_of_optional_subtag(element, 'axis:subject', ns)
subjects = []
if subject:
for subject_identifier in self.parse_list(subject):
subjects.append(
SubjectData(
type=Subject.BISAC, identifier=subject_identifier,
weight=1
)
)
publication_date = self.text_of_optional_subtag(
element, 'axis:publicationDate', ns)
if publication_date:
publication_date = datetime.datetime.strptime(
publication_date, self.SHORT_DATE_FORMAT)
series = self.text_of_optional_subtag(element, 'axis:series', ns)
publisher = self.text_of_optional_subtag(element, 'axis:publisher', ns)
imprint = self.text_of_optional_subtag(element, 'axis:imprint', ns)
audience = self.text_of_optional_subtag(element, 'axis:audience', ns)
if audience:
subjects.append(
SubjectData(
type=Subject.THETA_AUDIENCE,
identifier=audience,
weight=1,
)
)
language = self.text_of_subtag(element, 'axis:language', ns)
# We don't use this for anything.
# file_size = self.int_of_optional_subtag(element, 'theta:fileSize', ns)
primary_identifier = IdentifierData(Identifier.THETA_ID, identifier)
identifiers = []
if isbn:
identifiers.append(IdentifierData(Identifier.ISBN, isbn))
formats = []
acceptable = False
seen_formats = []
for format_tag in self._xpath(
element, 'axis:availability/axis:availableFormats/axis:formatName',
ns
):
informal_name = format_tag.text
seen_formats.append(informal_name)
if informal_name not in self.DELIVERY_DATA_FOR_THETA_FORMAT:
self.log("Unrecognized Theta format name for %s: %s" % (
identifier, informal_name
))
elif self.DELIVERY_DATA_FOR_THETA_FORMAT.get(informal_name):
content_type, drm_scheme = self.DELIVERY_DATA_FOR_THETA_FORMAT[
informal_name
]
formats.append(
FormatData(content_type=content_type, drm_scheme=drm_scheme)
)
if not formats:
self.log.error(
"No supported format for %s (%s)! Saw: %s", identifier,
title, ", ".join(seen_formats)
)
metadata = Metadata(
data_source=DataSource.THETA,
title=title,
language=language,
medium=Edition.BOOK_MEDIUM,
series=series,
publisher=publisher,
imprint=imprint,
published=publication_date,
primary_identifier=primary_identifier,
identifiers=identifiers,
subjects=subjects,
contributors=contributors,
)
circulationdata = CirculationData(
data_source=DataSource.THETA,
primary_identifier=primary_identifier,
formats=formats,
)
metadata.circulation = circulationdata
return metadata
def process_one(self, element, ns):
if self.include_bibliographic:
bibliographic = self.extract_bibliographic(element, ns)
else:
bibliographic = None
passed_availability = None
if bibliographic and bibliographic.circulation:
passed_availability = bibliographic.circulation
if self.include_availability:
availability = self.extract_availability(circulation_data=passed_availability, element=element, ns=ns)
else:
availability = None
return bibliographic, availability