This repository has been archived by the owner on Apr 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathoverdrive.py
1363 lines (1196 loc) · 51.4 KB
/
overdrive.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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import datetime
import isbnlib
import os
import json
import logging
from urllib.parse import urlsplit, quote, urlunsplit
import sys
from sqlalchemy.orm.exc import (
NoResultFound,
)
from sqlalchemy.orm.session import Session
from .classifier import Classifier
from .config import (
temp_config,
CannotLoadConfiguration,
Configuration,
)
from .model import (
get_one,
get_one_or_create,
Classification,
Collection,
ConfigurationSetting,
Contributor,
Credential,
DataSource,
DeliveryMechanism,
Edition,
ExternalIntegration,
Hyperlink,
Identifier,
Library,
Measurement,
MediaTypes,
Representation,
Subject,
)
from .metadata_layer import (
CirculationData,
ContributorData,
FormatData,
IdentifierData,
Metadata,
MeasurementData,
LinkData,
SubjectData,
)
from .coverage import (
BibliographicCoverageProvider,
)
from .testing import DatabaseTest
from .util.http import (
HTTP,
BadResponseException,
)
from .util.string_helpers import base64
from .util.worker_pools import RLock
from .util.datetime_helpers import strptime_utc, to_utc, utc_now
from .testing import MockRequestsResponse
class OverdriveAPI(object):
log = logging.getLogger("Overdrive API")
# A lock for threaded usage.
lock = RLock()
# Production and testing have different host names for some of the
# API endpoints. This is configurable on the collection level.
SERVER_NICKNAME = "server_nickname"
PRODUCTION_SERVERS = "production"
TESTING_SERVERS = "testing"
HOSTS = {
PRODUCTION_SERVERS : dict(
host="https://api.overdrive.com",
patron_host="https://patron.api.overdrive.com",
),
TESTING_SERVERS : dict(
host="https://integration.api.overdrive.com",
patron_host="https://integration-patron.api.overdrive.com",
)
}
# Production and testing setups use the same URLs for Client
# Authentication and Patron Authentication, but we use the same
# system as for other hostnames to give a consistent look to the
# templates.
for host in list(HOSTS.values()):
host['oauth_patron_host'] = "https://oauth-patron.overdrive.com"
host['oauth_host'] = "https://oauth.overdrive.com"
# Each of these endpoint URLs has a slot to plug in one of the
# appropriate servers. This will be filled in either by a call to
# the endpoint() method (if there are other variables in the
# template), or by the _do_get or _do_post methods (if there are
# no other variables).
TOKEN_ENDPOINT = "%(oauth_host)s/token"
PATRON_TOKEN_ENDPOINT = "%(oauth_patron_host)s/patrontoken"
LIBRARY_ENDPOINT = "%(host)s/v1/libraries/%(library_id)s"
ADVANTAGE_LIBRARY_ENDPOINT = "%(host)s/v1/libraries/%(parent_library_id)s/advantageAccounts/%(library_id)s"
ALL_PRODUCTS_ENDPOINT = "%(host)s/v1/collections/%(collection_token)s/products?sort=%(sort)s"
METADATA_ENDPOINT = "%(host)s/v1/collections/%(collection_token)s/products/%(item_id)s/metadata"
EVENTS_ENDPOINT = "%(host)s/v1/collections/%(collection_token)s/products?lastUpdateTime=%(lastupdatetime)s&sort=%(sort)s&limit=%(limit)s"
AVAILABILITY_ENDPOINT = "%(host)s/v2/collections/%(collection_token)s/products/%(product_id)s/availability"
PATRON_INFORMATION_ENDPOINT = "%(patron_host)s/v1/patrons/me"
CHECKOUTS_ENDPOINT = "%(patron_host)s/v1/patrons/me/checkouts"
CHECKOUT_ENDPOINT = "%(patron_host)s/v1/patrons/me/checkouts/%(overdrive_id)s"
FORMATS_ENDPOINT = "%(patron_host)s/v1/patrons/me/checkouts/%(overdrive_id)s/formats"
HOLDS_ENDPOINT = "%(patron_host)s/v1/patrons/me/holds"
HOLD_ENDPOINT = "%(patron_host)s/v1/patrons/me/holds/%(product_id)s"
ME_ENDPOINT = "%(patron_host)s/v1/patrons/me"
MAX_CREDENTIAL_AGE = 50 * 60
PAGE_SIZE_LIMIT = 300
EVENT_SOURCE = "Overdrive"
EVENT_DELAY = datetime.timedelta(minutes=120)
# The formats we care about.
FORMATS = "ebook-epub-open,ebook-epub-adobe,ebook-pdf-adobe,ebook-pdf-open,audiobook-overdrive".split(",")
# The formats that can be read by the default Library Simplified reader.
DEFAULT_READABLE_FORMATS = set(
["ebook-epub-open", "ebook-epub-adobe", "ebook-pdf-open",
"audiobook-overdrive"]
)
# The formats that indicate the book has been fulfilled on an
# incompatible platform and just can't be fulfilled on Simplified
# in any format.
INCOMPATIBLE_PLATFORM_FORMATS = set(["ebook-kindle"])
OVERDRIVE_READ_FORMAT = "ebook-overdrive"
TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
WEBSITE_ID = "website_id"
# When associating an Overdrive account with a library, it's
# necessary to also specify an "ILS name" obtained from
# Overdrive. Components that don't authenticate patrons (such as
# the metadata wrangler) don't need to set this value.
ILS_NAME_KEY = "ils_name"
ILS_NAME_DEFAULT = "default"
def __init__(self, _db, collection):
if collection.protocol != ExternalIntegration.OVERDRIVE:
raise ValueError(
"Collection protocol is %s, but passed into OverdriveAPI!" %
collection.protocol
)
self._db = _db
self.library_id = collection.external_account_id
self.collection_id = collection.id
if collection.parent:
# This is an Overdrive Advantage account.
self.parent_library_id = collection.parent.external_account_id
# We're going to inherit all of the Overdrive credentials
# from the parent (the main Overdrive account), except for the
# library ID, which we already set.
collection = collection.parent
else:
self.parent_library_id = None
integration = collection.external_integration
self.client_key = integration.username
self.client_secret = integration.password
self.website_id = integration.setting(self.WEBSITE_ID).value
if (not self.client_key or not self.client_secret or not self.website_id
or not self.library_id):
raise CannotLoadConfiguration(
"Overdrive configuration is incomplete."
)
# Figure out which hostnames we'll be using when constructing
# endpoint URLs.
server_nickname = (
integration.setting(self.SERVER_NICKNAME).value
or self.PRODUCTION_SERVERS
)
if server_nickname not in self.HOSTS:
server_nickname = self.PRODUCTION_SERVERS
# Set the hostnames we'll be using. Make a new dictionary just
# to be safe.
self.hosts = dict(self.HOSTS[server_nickname])
# Use utf8 instead of unicode encoding
settings = [self.client_key, self.client_secret, self.website_id]
self.client_key, self.client_secret, self.website_id = (
setting.encode('utf8') for setting in settings
)
# This is set by an access to .token, or by a call to
# check_creds() or refresh_creds().
self._token = None
# This is set by an access to .collection_token
self._collection_token = None
def endpoint(self, url, **kwargs):
"""Create the URL to an Overdrive API endpoint.
:param url: A template for the URL.
:param kwargs: Arguments to be interpolated into the template.
The server hostname will be interpolated automatically; you
don't have to pass it in.
"""
if not '%(' in url:
# Nothing to interpolate.
return url
kwargs.update(self.hosts)
return url % kwargs
@property
def token(self):
if not self._token:
self.check_creds()
return self._token
@property
def collection_token(self):
"""Get the token representing this particular Overdrive collection.
As a side effect, this will verify that the Overdrive
credentials are working.
"""
if not self._collection_token:
self.check_creds()
library = self.get_library()
error = library.get('errorCode')
if error:
message = library.get('message')
raise CannotLoadConfiguration(
"Overdrive credentials are valid but could not fetch library: %s"
% message
)
self._collection_token = library['collectionToken']
return self._collection_token
@property
def collection(self):
return Collection.by_id(self._db, id=self.collection_id)
@property
def source(self):
return DataSource.lookup(self._db, DataSource.OVERDRIVE)
def ils_name(self, library):
"""Determine the ILS name to use for the given Library.
"""
return self.ils_name_setting(
self._db, self.collection, library
).value_or_default(self.ILS_NAME_DEFAULT)
@classmethod
def ils_name_setting(cls, _db, collection, library):
"""Find the ConfigurationSetting controlling the ILS name
for the given collection and library.
"""
return ConfigurationSetting.for_library_and_externalintegration(
_db, cls.ILS_NAME_KEY, library, collection.external_integration
)
@property
def advantage_library_id(self):
"""The library ID for this library, as we should look for it in
certain API documents served by Overdrive.
For ordinary collections, and for consortial collections
shared among libraries, this will be -1.
For Overdrive Advantage accounts, this will be the numeric
value of the Overdrive library ID.
"""
if self.parent_library_id is None:
# This is not an Overdrive Advantage collection.
#
# Instead of looking for the library ID itself in these
# documents, we should look for the constant -1.
return -1
return int(self.library_id)
def check_creds(self, force_refresh=False):
"""If the Bearer Token has expired, update it."""
with self.lock:
refresh_on_lookup = self.refresh_creds
if force_refresh:
refresh_on_lookup = lambda x: x
credential = self.credential_object(refresh_on_lookup)
if force_refresh:
self.refresh_creds(credential)
self._token = credential.credential
def credential_object(self, refresh):
"""Look up the Credential object that allows us to use
the Overdrive API.
"""
return Credential.lookup(
self._db, DataSource.OVERDRIVE, None, None, refresh,
collection=self.collection
)
def refresh_creds(self, credential):
"""Fetch a new Bearer Token and update the given Credential object."""
response = self.token_post(
self.TOKEN_ENDPOINT,
dict(grant_type="client_credentials"),
allowed_response_codes=[200]
)
data = response.json()
self._update_credential(credential, data)
self._token = credential.credential
def get(self, url, extra_headers, exception_on_401=False):
"""Make an HTTP GET request using the active Bearer Token."""
headers = dict(Authorization="Bearer %s" % self.token)
headers.update(extra_headers)
status_code, headers, content = self._do_get(url, headers)
if status_code == 401:
if exception_on_401:
# This is our second try. Give up.
raise BadResponseException.from_response(
url,
"Something's wrong with the Overdrive OAuth Bearer Token!",
(status_code, headers, content)
)
else:
# Refresh the token and try again.
self.check_creds(True)
return self.get(url, extra_headers, True)
else:
return status_code, headers, content
@property
def token_authorization_header(self):
s = b"%s:%s" % (self.client_key, self.client_secret)
return "Basic " + base64.standard_b64encode(s).strip()
def token_post(self, url, payload, headers={}, **kwargs):
"""Make an HTTP POST request for purposes of getting an OAuth token."""
headers = dict(headers)
headers['Authorization'] = self.token_authorization_header
return self._do_post(url, payload, headers, **kwargs)
def _update_credential(self, credential, overdrive_data):
"""Copy Overdrive OAuth data into a Credential object."""
credential.credential = overdrive_data['access_token']
expires_in = (overdrive_data['expires_in'] * 0.9)
credential.expires = utc_now() + datetime.timedelta(
seconds=expires_in)
@property
def _library_endpoint(self):
"""Which URL should we go to to get information about this collection?
If this is an ordinary Overdrive account, we get information
from LIBRARY_ENDPOINT.
If this is an Overdrive Advantage account, we get information
from LIBRARY_ADVANTAGE_ENDPOINT.
"""
args = dict(library_id=self.library_id)
if self.parent_library_id:
# This is an Overdrive advantage account.
args['parent_library_id'] = self.parent_library_id
endpoint = self.ADVANTAGE_LIBRARY_ENDPOINT
else:
endpoint = self.LIBRARY_ENDPOINT
return self.endpoint(endpoint, **args)
def get_library(self):
"""Get basic information about the collection, including
a link to the titles in the collection.
"""
url = self._library_endpoint
with self.lock:
representation, cached = Representation.get(
self._db, url, self.get,
exception_handler=Representation.reraise_exception,
)
return json.loads(representation.content)
def get_advantage_accounts(self):
"""Find all the Overdrive Advantage accounts managed by this library.
:yield: A sequence of OverdriveAdvantageAccount objects.
"""
library = self.get_library()
links = library.get('links', {})
advantage = links.get('advantageAccounts')
if not advantage:
return []
if advantage:
# This library has Overdrive Advantage accounts, or at
# least a link where some may be found.
advantage_url = advantage.get('href')
if not advantage_url:
return
representation, cached = Representation.get(
self._db, advantage_url, self.get,
exception_handler=Representation.reraise_exception,
)
return OverdriveAdvantageAccount.from_representation(
representation.content
)
def all_ids(self):
"""Get IDs for every book in the system, with the most recently added
ones at the front.
"""
next_link = self._all_products_link
while next_link:
page_inventory, next_link = self._get_book_list_page(
next_link, 'next'
)
for i in page_inventory:
yield i
@property
def _all_products_link(self):
url = self.endpoint(
self.ALL_PRODUCTS_ENDPOINT,
collection_token=self.collection_token,
sort="dateAdded:desc"
)
return self.make_link_safe(url)
def _get_book_list_page(self, link, rel_to_follow='next',
extractor_class=None):
"""Process a page of inventory whose circulation we need to check.
Returns a 2-tuple: (availability_info, next_link).
`availability_info` is a list of dictionaries, each containing
basic availability and bibliographic information about
one book.
`next_link` is a link to the next page of results.
"""
extractor_class = extractor_class or OverdriveRepresentationExtractor
# We don't cache this because it changes constantly.
status_code, headers, content = self.get(link, {})
if isinstance(content, (bytes, str)):
content = json.loads(content)
# Find the link to the next page of results, if any.
next_link = extractor_class.link(content, rel_to_follow)
# Prepare to get availability information for all the books on
# this page.
availability_queue = (extractor_class.availability_link_list(content))
return availability_queue, next_link
def recently_changed_ids(self, start, cutoff):
"""Get IDs of books whose status has changed between the start time
and now.
"""
# `cutoff` is not supported by Overdrive, so we ignore it. All
# we can do is get events between the start time and now.
last_update_time = start-self.EVENT_DELAY
self.log.info(
"Asking for circulation changes since %s",
last_update_time
)
last_update = last_update_time.strftime(self.TIME_FORMAT)
next_link = self.endpoint(
self.EVENTS_ENDPOINT,
lastupdatetime=last_update,
sort="popularity:desc",
limit=self.PAGE_SIZE_LIMIT,
collection_token=self.collection_token
)
next_link = self.make_link_safe(next_link)
while next_link:
page_inventory, next_link = self._get_book_list_page(next_link)
# We won't be sending out any events for these books yet,
# because we don't know if anything changed, but we will
# be putting them on the list of inventory items to
# refresh. At that point we will send out events.
for i in page_inventory:
yield i
def metadata_lookup(self, identifier):
"""Look up metadata for an Overdrive identifier.
"""
url = self.endpoint(
self.METADATA_ENDPOINT,
collection_token=self.collection_token,
item_id=identifier.identifier
)
status_code, headers, content = self.get(url, {})
if isinstance(content, (bytes, str)):
content = json.loads(content)
return content
def metadata_lookup_obj(self, identifier):
url = self.endpoint(
self.METADATA_ENDPOINT,
collection_token=self.collection_token,
item_id=identifier
)
status_code, headers, content = self.get(url, {})
if isinstance(content, (bytes, str)):
content = json.loads(content)
return OverdriveRepresentationExtractor.book_info_to_metadata(content)
@classmethod
def make_link_safe(self, url):
"""Turn a server-provided link into a link the server will accept!
The {} part is completely obnoxious and I have complained about it to
Overdrive.
The availability part is to make sure we always use v2 of the
availability API, even if Overdrive sent us a link to v1.
"""
parts = list(urlsplit(url))
parts[2] = quote(parts[2])
endings = ("/availability", "/availability/")
if (parts[2].startswith("/v1/collections/")
and any(parts[2].endswith(x) for x in endings)):
parts[2] = parts[2].replace(
"/v1/collections/", "/v2/collections/", 1
)
query_string = parts[3]
query_string = query_string.replace("+", "%2B")
query_string = query_string.replace(":", "%3A")
query_string = query_string.replace("{", "%7B")
query_string = query_string.replace("}", "%7D")
parts[3] = query_string
return urlunsplit(tuple(parts))
def _do_get(self, url, headers):
"""This method is overridden in MockOverdriveAPI."""
url = self.endpoint(url)
return Representation.simple_http_get(
url, headers
)
def _do_post(self, url, payload, headers, **kwargs):
"""This method is overridden in MockOverdriveAPI."""
url = self.endpoint(url)
return HTTP.post_with_timeout(url, payload, headers=headers, **kwargs)
class MockOverdriveAPI(OverdriveAPI):
@classmethod
def mock_collection(self, _db, library=None,
name="Test Overdrive Collection",
client_key="a", client_secret="b",
library_id="c", website_id="d",
ils_name="e",
):
"""Create a mock Overdrive collection for use in tests."""
if library is None:
library = DatabaseTest.make_default_library(_db)
collection, ignore = get_one_or_create(
_db, Collection,
name=name,
create_method_kwargs=dict(
external_account_id=library_id
)
)
integration = collection.create_external_integration(
protocol=ExternalIntegration.OVERDRIVE
)
integration.username = client_key
integration.password = client_secret
integration.set_setting('website_id', website_id)
library.collections.append(collection)
OverdriveAPI.ils_name_setting(_db, collection, library).value = ils_name
return collection
def __init__(self, _db, collection, *args, **kwargs):
self.access_token_requests = []
self.requests = []
self.responses = []
# Almost all tests will try to request the access token, so
# set the response that will be returned if an attempt is
# made.
self.access_token_response = self.mock_access_token_response(
"bearer token"
)
super(MockOverdriveAPI, self).__init__(_db, collection, *args, **kwargs)
def queue_collection_token(self):
# Many tests immediately try to access the
# collection token. This is a helper method to make it easy to
# queue up the response.
self.queue_response(
200, content=self.mock_collection_token("collection token")
)
def token_post(self, url, payload, headers={}, **kwargs):
"""Mock the request for an OAuth token.
We mock the method by looking at the access_token_response
property, rather than inserting a mock response in the queue,
because only the first MockOverdriveAPI instantiation in a
given test actually makes this call. By mocking the response
to this method separately we remove the need to figure out
whether to queue a response in a given test.
"""
url = self.endpoint(url)
self.access_token_requests.append((url, payload, headers, kwargs))
response = self.access_token_response
return HTTP._process_response(url, response, **kwargs)
def mock_access_token_response(self, credential):
token = dict(access_token=credential, expires_in=3600)
return MockRequestsResponse(200, {}, json.dumps(token))
def mock_collection_token(self, token):
return json.dumps(dict(collectionToken=token))
def queue_response(self, status_code, headers={}, content=None):
self.responses.insert(
0, MockRequestsResponse(status_code, headers, content)
)
def _do_get(self, url, *args, **kwargs):
"""Simulate Representation.simple_http_get."""
response = self._make_request(url, *args, **kwargs)
return response.status_code, response.headers, response.content
def _do_post(self, url, *args, **kwargs):
return self._make_request(url, *args, **kwargs)
def _make_request(self, url, *args, **kwargs):
url = self.endpoint(url)
response = self.responses.pop()
self.requests.append((url, args, kwargs))
return HTTP._process_response(
url, response, kwargs.get('allowed_response_codes'),
kwargs.get('disallowed_response_codes')
)
class OverdriveRepresentationExtractor(object):
"""Extract useful information from Overdrive's JSON representations."""
log = logging.getLogger("Overdrive representation extractor")
def __init__(self, api):
"""Constructor.
:param api: An OverdriveAPI object. This will be used when deciding
which portions of a JSON representation are relevant to the active
Overdrive collection.
"""
self.library_id = api.advantage_library_id
@classmethod
def availability_link_list(cls, book_list):
""":return: A list of dictionaries with keys `id`, `title`, `availability_link`.
"""
l = []
if not 'products' in book_list:
return []
products = book_list['products']
for product in products:
if not 'id' in product:
cls.log.warn("No ID found in %r", product)
continue
book_id = product['id']
data = dict(
id=book_id,
title=product.get('title'),
author_name=None,
date_added=product.get('dateAdded')
)
if 'primaryCreator' in product:
creator = product['primaryCreator']
if creator.get('role') == 'Author':
data['author_name'] = creator.get('name')
links = product.get('links', [])
if 'availability' in links:
link = links['availability']['href']
data['availability_link'] = OverdriveAPI.make_link_safe(link)
else:
logging.getLogger("Overdrive API").warn(
"No availability link for %s", book_id)
l.append(data)
return l
@classmethod
def link(self, page, rel):
if 'links' in page and rel in page['links']:
raw_link = page['links'][rel]['href']
link = OverdriveAPI.make_link_safe(raw_link)
else:
link = None
return link
format_data_for_overdrive_format = {
"ebook-pdf-adobe" : (
Representation.PDF_MEDIA_TYPE, DeliveryMechanism.ADOBE_DRM
),
"ebook-pdf-open" : (
Representation.PDF_MEDIA_TYPE, DeliveryMechanism.NO_DRM
),
"ebook-epub-adobe" : (
Representation.EPUB_MEDIA_TYPE, DeliveryMechanism.ADOBE_DRM
),
"ebook-epub-open" : (
Representation.EPUB_MEDIA_TYPE, DeliveryMechanism.NO_DRM
),
"audiobook-mp3" : (
"application/x-od-media", DeliveryMechanism.OVERDRIVE_DRM
),
"music-mp3" : (
"application/x-od-media", DeliveryMechanism.OVERDRIVE_DRM
),
"ebook-overdrive" : [
(
MediaTypes.OVERDRIVE_EBOOK_MANIFEST_MEDIA_TYPE,
DeliveryMechanism.LIBBY_DRM
),
(
DeliveryMechanism.STREAMING_TEXT_CONTENT_TYPE,
DeliveryMechanism.STREAMING_DRM
),
],
"audiobook-overdrive" : [
(
MediaTypes.OVERDRIVE_AUDIOBOOK_MANIFEST_MEDIA_TYPE,
DeliveryMechanism.LIBBY_DRM,
),
(
DeliveryMechanism.STREAMING_AUDIO_CONTENT_TYPE,
DeliveryMechanism.STREAMING_DRM
),
],
'video-streaming' : (
DeliveryMechanism.STREAMING_VIDEO_CONTENT_TYPE,
DeliveryMechanism.STREAMING_DRM
),
"ebook-kindle" : (
DeliveryMechanism.KINDLE_CONTENT_TYPE,
DeliveryMechanism.KINDLE_DRM
),
"periodicals-nook" : (
DeliveryMechanism.NOOK_CONTENT_TYPE,
DeliveryMechanism.NOOK_DRM
),
}
@classmethod
def internal_formats(cls, overdrive_format):
"""Yield all internal formats for the given Overdrive format.
Some Overdrive formats become multiple internal formats.
:yield: A sequence of (content type, DRM system) 2-tuples
"""
result = cls.format_data_for_overdrive_format.get(overdrive_format)
if not result:
return
if isinstance(result, list):
for i in result:
yield i
else:
yield result
ignorable_overdrive_formats = set([])
overdrive_role_to_simplified_role = {
"actor" : Contributor.ACTOR_ROLE,
"artist" : Contributor.ARTIST_ROLE,
"book producer" : Contributor.PRODUCER_ROLE,
"associated name" : Contributor.ASSOCIATED_ROLE,
"author" : Contributor.AUTHOR_ROLE,
"author of introduction" : Contributor.INTRODUCTION_ROLE,
"author of foreword" : Contributor.FOREWORD_ROLE,
"author of afterword" : Contributor.AFTERWORD_ROLE,
"contributor" : Contributor.CONTRIBUTOR_ROLE,
"colophon" : Contributor.COLOPHON_ROLE,
"adapter" : Contributor.ADAPTER_ROLE,
"etc." : Contributor.UNKNOWN_ROLE,
"cast member" : Contributor.ACTOR_ROLE,
"collaborator" : Contributor.COLLABORATOR_ROLE,
"compiler" : Contributor.COMPILER_ROLE,
"composer" : Contributor.COMPOSER_ROLE,
"copyright holder" : Contributor.COPYRIGHT_HOLDER_ROLE,
"director" : Contributor.DIRECTOR_ROLE,
"editor" : Contributor.EDITOR_ROLE,
"engineer" : Contributor.ENGINEER_ROLE,
"executive producer" : Contributor.EXECUTIVE_PRODUCER_ROLE,
"illustrator" : Contributor.ILLUSTRATOR_ROLE,
"musician" : Contributor.MUSICIAN_ROLE,
"narrator" : Contributor.NARRATOR_ROLE,
"other" : Contributor.UNKNOWN_ROLE,
"performer" : Contributor.PERFORMER_ROLE,
"producer" : Contributor.PRODUCER_ROLE,
"translator" : Contributor.TRANSLATOR_ROLE,
"photographer" : Contributor.PHOTOGRAPHER_ROLE,
"lyricist" : Contributor.LYRICIST_ROLE,
"transcriber" : Contributor.TRANSCRIBER_ROLE,
"designer" : Contributor.DESIGNER_ROLE,
}
overdrive_medium_to_simplified_medium = {
"eBook" : Edition.BOOK_MEDIUM,
"Video" : Edition.VIDEO_MEDIUM,
"Audiobook" : Edition.AUDIO_MEDIUM,
"Music" : Edition.MUSIC_MEDIUM,
"Periodicals" : Edition.PERIODICAL_MEDIUM,
}
DATE_FORMAT = "%Y-%m-%d"
@classmethod
def parse_roles(cls, id, rolestring):
rolestring = rolestring.lower()
roles = [x.strip() for x in rolestring.split(",")]
if ' and ' in roles[-1]:
roles = roles[:-1] + [x.strip() for x in roles[-1].split(" and ")]
processed = []
for x in roles:
if x not in cls.overdrive_role_to_simplified_role:
cls.log.error(
"Could not process role %s for %s", x, id)
else:
processed.append(cls.overdrive_role_to_simplified_role[x])
return processed
def book_info_to_circulation(self, book):
""" Note: The json data passed into this method is from a different file/stream
from the json data that goes into the book_info_to_metadata() method.
"""
# In Overdrive, 'reserved' books show up as books on
# hold. There is no separate notion of reserved books.
licenses_reserved = 0
licenses_owned = None
licenses_available = None
patrons_in_hold_queue = None
# TODO: The only reason this works for a NotFound error is the
# circulation code sticks the known book ID into `book` ahead
# of time. That's a code smell indicating that this system
# needs to be refactored.
if 'reserveId' in book and not 'id' in book:
book['id'] = book['reserveId']
if not 'id' in book:
return None
overdrive_id = book['id']
primary_identifier = IdentifierData(
Identifier.OVERDRIVE_ID, overdrive_id
)
# TODO: We might be able to use this information to avoid the
# need for explicit configuration of Advantage collections, or
# at least to keep Advantage collections more up-to-date than
# they would be otherwise, as a side effect of updating
# regular Overdrive collections.
# TODO: this would be the place to handle simultaneous use
# titles -- these can be detected with
# availabilityType="AlwaysAvailable" and have their
# .licenses_owned set to LicensePool.UNLIMITED_ACCESS.
# see http://developer.overdrive.com/apis/library-availability-new
# TODO: Cost-per-circ titles
# (availabilityType="LimitedAvailablility") can be handled
# similarly, though those can abruptly become unavailable, so
# UNLIMITED_ACCESS is probably not appropriate.
error_code = book.get('errorCode')
# TODO: It's not clear what other error codes there might be.
# The current behavior will respond to errors other than
# NotFound by leaving the book alone, but this might not be
# the right behavior.
if error_code == 'NotFound':
licenses_owned = 0
licenses_available = 0
patrons_in_hold_queue = 0
elif book.get('isOwnedByCollections') is not False:
# We own this book.
for account in book.get('accounts', []):
# Only keep track of copies owned by the collection
# we're tracking.
if account.get('id') != self.library_id:
continue
if 'copiesOwned' in account:
if licenses_owned is None:
licenses_owned = 0
licenses_owned += int(account['copiesOwned'])
if 'copiesAvailable' in account:
if licenses_available is None:
licenses_available = 0
licenses_available += int(account['copiesAvailable'])
if 'numberOfHolds' in book:
if patrons_in_hold_queue is None:
patrons_in_hold_queue = 0
patrons_in_hold_queue += book['numberOfHolds']
return CirculationData(
data_source=DataSource.OVERDRIVE,
primary_identifier=primary_identifier,
licenses_owned=licenses_owned,
licenses_available=licenses_available,
licenses_reserved=licenses_reserved,
patrons_in_hold_queue=patrons_in_hold_queue,
)
@classmethod
def image_link_to_linkdata(cls, link, rel):
if not link or not 'href' in link:
return None
href = link['href']
if '00000000-0000-0000-0000' in href:
# This is a stand-in cover for preorders. It's better not
# to have a cover at all -- we might be able to get one
# later, or from another source.
return None
href = OverdriveAPI.make_link_safe(href)
media_type = link.get('type', None)
return LinkData(rel=rel, href=href, media_type=media_type)
@classmethod
def book_info_to_metadata(cls, book, include_bibliographic=True, include_formats=True):
"""Turn Overdrive's JSON representation of a book into a Metadata
object.
Note: The json data passed into this method is from a different file/stream
from the json data that goes into the book_info_to_circulation() method.
"""
if not 'id' in book:
return None
overdrive_id = book['id']
primary_identifier = IdentifierData(
Identifier.OVERDRIVE_ID, overdrive_id
)
# If we trust classification data, we'll give it this weight.
# Otherwise we'll probably give it a fraction of this weight.
trusted_weight = Classification.TRUSTED_DISTRIBUTOR_WEIGHT
if include_bibliographic:
title = book.get('title', None)
sort_title = book.get('sortTitle')
subtitle = book.get('subtitle', None)
series = book.get('series', None)
publisher = book.get('publisher', None)
imprint = book.get('imprint', None)
if 'publishDate' in book:
published = strptime_utc(
book['publishDate'][:10], cls.DATE_FORMAT)
else:
published = None
languages = [l['code'] for l in book.get('languages', [])]
if 'eng' in languages or not languages:
language = 'eng'
else:
language = sorted(languages)[0]
contributors = []
for creator in book.get('creators', []):
sort_name = creator['fileAs']
display_name = creator['name']
role = creator['role']
roles = cls.parse_roles(overdrive_id, role) or [Contributor.UNKNOWN_ROLE]
contributor = ContributorData(
sort_name=sort_name, display_name=display_name,
roles=roles, biography = creator.get('bioText', None)
)
contributors.append(contributor)
subjects = []
for sub in book.get('subjects', []):
subject = SubjectData(
type=Subject.OVERDRIVE, identifier=sub['value'],
weight=trusted_weight,
)
subjects.append(subject)
for sub in book.get('keywords', []):
subject = SubjectData(