forked from NYPL-Simplified/circulation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscripts.py
1066 lines (906 loc) · 36.8 KB
/
scripts.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
# encoding: utf-8
from cStringIO import StringIO
from datetime import (
datetime,
timedelta,
)
from nose.tools import set_trace
import csv
import json
import os
import sys
import time
import urlparse
import logging
import argparse
from sqlalchemy import (
or_,
func,
)
from sqlalchemy.orm import (
contains_eager,
defer
)
from psycopg2.extras import NumericRange
from core import log
from core.lane import Lane
from core.classifier import Classifier
from core.model import (
CirculationEvent,
ConfigurationSetting,
Contribution,
Credential,
CustomList,
DataSource,
DeliveryMechanism,
Edition,
ExternalIntegration,
get_one,
Hold,
Hyperlink,
Identifier,
Library,
LicensePool,
LicensePoolDeliveryMechanism,
Loan,
Representation,
Subject,
Timestamp,
Work,
)
from core.scripts import (
Script as CoreScript,
DatabaseMigrationInitializationScript,
RunCoverageProvidersScript,
RunCoverageProviderScript,
IdentifierInputScript,
LaneSweeperScript,
LibraryInputScript,
PatronInputScript,
RunMonitorScript,
)
from core.lane import (
Pagination,
Facets,
)
from core.opds_import import (
MetadataWranglerOPDSLookup,
OPDSImporter,
)
from core.opds import (
AcquisitionFeed,
)
from core.util.opds_writer import (
OPDSFeed,
)
from core.external_list import CustomListFromCSV
from core.external_search import ExternalSearchIndex
from core.util import LanguageCodes
from api.config import (
CannotLoadConfiguration,
Configuration,
)
from api.adobe_vendor_id import (
AdobeVendorIDModel,
AuthdataUtility,
)
from api.lanes import create_default_lanes
from api.controller import CirculationManager
from api.overdrive import OverdriveAPI
from api.circulation import CirculationAPI
from api.opds import CirculationManagerAnnotator
from api.overdrive import (
OverdriveAPI,
OverdriveBibliographicCoverageProvider,
)
from api.bibliotheca import (
BibliothecaBibliographicCoverageProvider,
BibliothecaCirculationSweep
)
from api.axis import (
Axis360API,
)
from api.nyt import NYTBestSellerAPI
from core.axis import Axis360BibliographicCoverageProvider
from api.opds_for_distributors import (
OPDSForDistributorsImporter,
OPDSForDistributorsImportMonitor,
OPDSForDistributorsReaperMonitor,
)
from api.odl import (
ODLBibliographicImporter,
ODLBibliographicImportMonitor,
)
from core.scripts import OPDSImportScript
class Script(CoreScript):
def load_config(self):
if not Configuration.instance:
Configuration.load(self._db)
class CreateWorksForIdentifiersScript(Script):
"""Do the bare minimum to associate each Identifier with an Edition
with title and author, so that we can calculate a permanent work
ID.
"""
to_check = [Identifier.OVERDRIVE_ID, Identifier.THREEM_ID,
Identifier.GUTENBERG_ID]
BATCH_SIZE = 100
name = "Create works for identifiers"
def __init__(self, metadata_web_app_url=None):
if metadata_web_app_url:
self.lookup = MetadataWranglerOPDSLookup(metadata_web_app_url)
else:
self.lookup = MetadataWranglerOPDSLookup.from_config(_db)
def run(self):
# We will try to fill in Editions that are missing
# title/author and as such have no permanent work ID.
#
# We will also try to create Editions for Identifiers that
# have no Edition.
either_title_or_author_missing = or_(
Edition.title == None,
Edition.sort_author == None,
)
edition_missing_title_or_author = self._db.query(Identifier).join(
Identifier.primarily_identifies).filter(
either_title_or_author_missing)
no_edition = self._db.query(Identifier).filter(
Identifier.primarily_identifies==None).filter(
Identifier.type.in_(self.to_check))
for q, descr in (
(edition_missing_title_or_author,
"identifiers whose edition is missing title or author"),
(no_edition, "identifiers with no edition")):
batch = []
self.log.debug("Trying to fix %d %s", q.count(), descr)
for i in q:
batch.append(i)
if len(batch) >= self.BATCH_SIZE:
self.process_batch(batch)
batch = []
def process_batch(self, batch):
response = self.lookup.lookup(batch)
if response.status_code != 200:
raise Exception(response.text)
content_type = response.headers['content-type']
if content_type != OPDSFeed.ACQUISITION_FEED_TYPE:
raise Exception("Wrong media type: %s" % content_type)
importer = OPDSImporter(
self._db, response.text,
overwrite_rels=[Hyperlink.DESCRIPTION, Hyperlink.IMAGE])
imported, messages_by_id = importer.import_from_feed()
self.log.info("%d successes, %d failures.",
len(imported), len(messages_by_id))
self._db.commit()
class MetadataCalculationScript(Script):
"""Force calculate_presentation() to be called on some set of Editions.
This assumes that the metadata is in already in the database and
will fall into place if we just call
Edition.calculate_presentation() and Edition.calculate_work() and
Work.calculate_presentation().
Most of these will be data repair scripts that do not need to be run
regularly.
"""
name = "Metadata calculation script"
def q(self):
raise NotImplementedError()
def run(self):
q = self.q()
search_index_client = ExternalSearchIndex(self._db)
self.log.info("Attempting to repair metadata for %d works" % q.count())
success = 0
failure = 0
also_created_work = 0
def checkpoint():
self._db.commit()
self.log.info("%d successes, %d failures, %d new works.",
success, failure, also_created_work)
i = 0
for edition in q:
edition.calculate_presentation()
if edition.sort_author:
success += 1
work, is_new = edition.license_pool.calculate_work(
search_index_client=search_index_client)
if work:
work.calculate_presentation()
if is_new:
also_created_work += 1
else:
failure += 1
i += 1
if not i % 1000:
checkpoint()
checkpoint()
class FillInAuthorScript(MetadataCalculationScript):
"""Fill in Edition.sort_author for Editions that have a list of
Contributors, but no .sort_author.
This is a data repair script that should not need to be run
regularly.
"""
name = "Fill in missing authors"
def q(self):
return self._db.query(Edition).join(
Edition.contributions).join(Contribution.contributor).filter(
Edition.sort_author==None)
class UpdateStaffPicksScript(Script):
DEFAULT_URL_TEMPLATE = "https://docs.google.com/spreadsheets/d/%s/export?format=csv"
def run(self):
inp = self.open()
tag_fields = {
'tags': Subject.NYPL_APPEAL,
}
integ = Configuration.integration(Configuration.STAFF_PICKS_INTEGRATION)
fields = integ.get(Configuration.LIST_FIELDS, {})
importer = CustomListFromCSV(
DataSource.LIBRARY_STAFF, CustomList.STAFF_PICKS_NAME,
**fields
)
reader = csv.DictReader(inp, dialect='excel-tab')
importer.to_customlist(self._db, reader)
self._db.commit()
def open(self):
if len(sys.argv) > 1:
return open(sys.argv[1])
url = Configuration.integration_url(
Configuration.STAFF_PICKS_INTEGRATION, True
)
if not url.startswith('https://') or url.startswith('http://'):
url = self.DEFAULT_URL_TEMPLATE % url
self.log.info("Retrieving %s", url)
representation, cached = Representation.get(
self._db, url, do_get=Representation.browser_http_get,
accept="text/csv", max_age=timedelta(days=1))
if representation.status_code != 200:
raise ValueError("Unexpected status code %s" %
representation.status_code)
if not representation.media_type.startswith("text/csv"):
raise ValueError("Unexpected media type %s" %
representation.media_type)
return StringIO(representation.content)
class CacheRepresentationPerLane(LaneSweeperScript):
name = "Cache one representation per lane"
@classmethod
def arg_parser(cls, _db):
parser = LaneSweeperScript.arg_parser(_db)
parser.add_argument(
'--language',
help='Process only lanes that include books in this language.',
action='append'
)
parser.add_argument(
'--max-depth',
help='Stop processing lanes once you reach this depth.',
type=int,
default=None
)
parser.add_argument(
'--min-depth',
help='Start processing lanes once you reach this depth.',
type=int,
default=1
)
return parser
def __init__(self, _db=None, cmd_args=None, testing=False, *args, **kwargs):
super(CacheRepresentationPerLane, self).__init__(_db, *args, **kwargs)
self.parse_args(cmd_args)
from api.app import app
app.manager = CirculationManager(self._db, testing=testing)
self.app = app
self.base_url = ConfigurationSetting.sitewide(self._db, Configuration.BASE_URL_KEY).value
def parse_args(self, cmd_args=None):
parser = self.arg_parser(self._db)
parsed = parser.parse_args(cmd_args)
self.languages = []
if parsed.language:
for language in parsed.language:
alpha = LanguageCodes.string_to_alpha_3(language)
if alpha:
self.languages.append(alpha)
else:
self.log.warn("Ignored unrecognized language code %s", alpha)
self.max_depth = parsed.max_depth
self.min_depth = parsed.min_depth
# Return the parsed arguments in case a subclass needs to
# process more args.
return parsed
def should_process_lane(self, lane):
if not isinstance(lane, Lane):
return False
language_ok = False
if not self.languages:
# We are considering lanes for every single language.
language_ok = True
if not lane.languages:
# The lane has no language restrictions.
language_ok = True
for language in self.languages:
if language in lane.languages:
language_ok = True
break
if not language_ok:
return False
if self.max_depth is not None and lane.depth > self.max_depth:
return False
if self.min_depth is not None and lane.depth < self.min_depth:
return False
return True
def cache_url(self, annotator, lane, languages):
raise NotImplementedError()
def generate_representation(self, *args, **kwargs):
raise NotImplementedError()
# The generated document will probably be an OPDS acquisition
# feed.
ACCEPT_HEADER = OPDSFeed.ACQUISITION_FEED_TYPE
cache_url_method = None
def process_library(self, library):
begin = time.time()
client = self.app.test_client()
ctx = self.app.test_request_context(base_url=self.base_url)
ctx.push()
super(CacheRepresentationPerLane, self).process_library(library)
ctx.pop()
end = time.time()
self.log.info(
"Processed library %s in %.2fsec", library.short_name, end-begin
)
def process_lane(self, lane):
annotator = self.app.manager.annotator(lane)
a = time.time()
self.log.info("Generating feed(s) for %s", lane.full_identifier)
cached_feeds = list(self.do_generate(lane))
b = time.time()
total_size = sum(len(x.content) for x in cached_feeds if x)
self.log.info(
"Generated %d feed(s) for %s. Took %.2fsec to make %d bytes.",
len(cached_feeds), lane.full_identifier, (b-a), total_size
)
return cached_feeds
class CacheFacetListsPerLane(CacheRepresentationPerLane):
"""Cache the first two pages of every relevant facet list for this lane."""
name = "Cache OPDS feeds"
@classmethod
def arg_parser(cls, _db):
parser = CacheRepresentationPerLane.arg_parser(_db)
available = Facets.DEFAULT_ENABLED_FACETS[Facets.ORDER_FACET_GROUP_NAME]
order_help = 'Generate feeds for this ordering. Possible values: %s.' % (
", ".join(available)
)
parser.add_argument(
'--order',
help=order_help,
action='append',
default=[],
)
available = Facets.DEFAULT_ENABLED_FACETS[Facets.AVAILABILITY_FACET_GROUP_NAME]
availability_help = 'Generate feeds for this availability setting. Possible values: %s.' % (
", ".join(available)
)
parser.add_argument(
'--availability',
help=availability_help,
action='append',
default=[],
)
available = Facets.DEFAULT_ENABLED_FACETS[Facets.COLLECTION_FACET_GROUP_NAME]
collection_help = 'Generate feeds for this collection within each lane. Possible values: %s.' % (
", ".join(available)
)
parser.add_argument(
'--collection',
help=collection_help,
action='append',
default=[],
)
default_pages = 2
parser.add_argument(
'--pages',
help="Number of pages to cache for each facet. Default: %d" % default_pages,
type=int,
default=default_pages
)
return parser
def parse_args(self, cmd_args=None):
parsed = super(CacheFacetListsPerLane, self).parse_args(cmd_args)
self.orders = parsed.order
self.availabilities = parsed.availability
self.collections = parsed.collection
self.pages = parsed.pages
return parsed
def do_generate(self, lane):
feeds = []
annotator = self.app.manager.annotator(lane)
if isinstance(lane, Lane):
lane_id = lane.id
else:
# Presumably this is the top-level WorkList.
lane_id = None
library = lane.get_library(self._db)
url = self.app.manager.cdn_url_for(
"feed", lane_identifier=lane_id,
library_short_name=library.short_name
)
default_order = library.default_facet(Facets.ORDER_FACET_GROUP_NAME)
allowed_orders = library.enabled_facets(Facets.ORDER_FACET_GROUP_NAME)
chosen_orders = self.orders or [default_order]
default_availability = library.default_facet(
Facets.AVAILABILITY_FACET_GROUP_NAME
)
allowed_availabilities = library.enabled_facets(
Facets.AVAILABILITY_FACET_GROUP_NAME
)
chosen_availabilities = self.availabilities or [default_availability]
default_collection = library.default_facet(
Facets.COLLECTION_FACET_GROUP_NAME
)
allowed_collections = library.enabled_facets(
Facets.COLLECTION_FACET_GROUP_NAME
)
chosen_collections = self.collections or [default_collection]
for order in chosen_orders:
if order not in allowed_orders:
logging.warn("Ignoring unsupported ordering %s" % order)
continue
for availability in chosen_availabilities:
if availability not in allowed_availabilities:
logging.warn("Ignoring unsupported availability %s" % availability)
continue
for collection in chosen_collections:
if collection not in allowed_collections:
logging.warn("Ignoring unsupported collection %s" % collection)
continue
pagination = Pagination.default()
facets = Facets(
library=library, collection=collection,
availability=availability,
order=order, order_ascending=True
)
title = lane.display_name
for pagenum in range(0, self.pages):
yield AcquisitionFeed.page(
self._db, title, url, lane, annotator,
facets=facets, pagination=pagination,
force_refresh=True
)
pagination = pagination.next_page
class CacheOPDSGroupFeedPerLane(CacheRepresentationPerLane):
name = "Cache OPDS group feed for each lane"
def should_process_lane(self, lane):
# OPDS group feeds are only generated for lanes that have sublanes.
if not lane.children:
return False
if self.max_depth and lane.depth > self.max_depth:
return False
return True
def do_generate(self, lane):
feeds = []
annotator = self.app.manager.annotator(lane)
title = lane.display_name
if isinstance(lane, Lane):
lane_id = lane.id
else:
# Presumably this is the top-level WorkList.
lane_id = None
library = lane.get_library(self._db)
url = self.app.manager.cdn_url_for(
"acquisition_groups", lane_identifier=lane_id,
library_short_name=library.short_name
)
yield AcquisitionFeed.groups(
self._db, title, url, lane, annotator,
force_refresh=True
)
class AdobeAccountIDResetScript(PatronInputScript):
@classmethod
def arg_parser(cls):
parser = PatronInputScript.arg_parser()
parser.add_argument(
'--delete',
help="Actually delete credentials as opposed to showing what would happen.",
action='store_true'
)
return parser
def do_run(self, *args, **kwargs):
parsed = self.parse_command_line(self._db, *args, **kwargs)
patrons = parsed.patrons
self.delete = parsed.delete
if not self.delete:
self.log.info(
"This is a dry run. Nothing will actually change in the database."
)
self.log.info(
"Run with --delete to change the database."
)
if patrons and self.delete:
self.log.warn(
"""This is not a drill.
Running this script will permanently disconnect %d patron(s) from their Adobe account IDs.
They will be unable to fulfill any existing loans that involve Adobe-encrypted files.
Sleeping for five seconds to give you a chance to back out.
You'll get another chance to back out before the database session is committed.""",
len(patrons)
)
time.sleep(5)
self.process_patrons(patrons)
if self.delete:
self.log.warn("All done. Sleeping for five seconds before committing.")
time.sleep(5)
self._db.commit()
def process_patron(self, patron):
"""Delete all of a patron's Credentials that contain an Adobe account
ID _or_ connect the patron to a DelegatedPatronIdentifier that
contains an Adobe account ID.
"""
self.log.info(
'Processing patron "%s"',
patron.authorization_identifier or patron.username
or patron.external_identifier
)
types = (AdobeVendorIDModel.VENDOR_ID_UUID_TOKEN_TYPE,
AuthdataUtility.ADOBE_ACCOUNT_ID_PATRON_IDENTIFIER)
credentials = self._db.query(
Credential).filter(Credential.patron==patron).filter(
Credential.type.in_(types)
)
for credential in credentials:
self.log.info(
' Deleting "%s" credential "%s"',
credential.type, credential.credential
)
if self.delete:
self._db.delete(credential)
class BibliographicCoverageProvidersScript(RunCoverageProvidersScript):
"""Alternate between running bibliographic coverage providers for
all registered book sources.
"""
def __init__(self):
providers = []
if Configuration.integration('3M'):
providers.append(BibliothecaBibliographicCoverageProvider)
if Configuration.integration('Overdrive'):
providers.append(OverdriveBibliographicCoverageProvider)
if Configuration.integration('Axis 360'):
providers.append(Axis360BibliographicCoverageProvider)
if not providers:
raise Exception("No licensed book sources configured, nothing to get coverage from!")
super(BibliographicCoverageProvidersScript, self).__init__(providers)
class AvailabilityRefreshScript(IdentifierInputScript):
"""Refresh the availability information for a LicensePool, direct from the
license source.
"""
def do_run(self):
args = self.parse_command_line(self._db)
if not args.identifiers:
raise Exception(
"You must specify at least one identifier to refresh."
)
# We don't know exactly how big to make these batches, but 10 is
# always safe.
start = 0
size = 10
while start < len(args.identifiers):
batch = args.identifiers[start:start+size]
self.refresh_availability(batch)
self._db.commit()
start += size
def refresh_availability(self, identifiers):
provider = None
identifier = identifiers[0]
if identifier.type==Identifier.THREEM_ID:
sweeper = BibliothecaCirculationSweep(self._db)
sweeper.process_batch(identifiers)
elif identifier.type==Identifier.OVERDRIVE_ID:
api = OverdriveAPI(self._db)
for identifier in identifiers:
api.update_licensepool(identifier.identifier)
elif identifier.type==Identifier.AXIS_360_ID:
provider = Axis360BibliographicCoverageProvider(self._db)
provider.process_batch(identifiers)
else:
self.log.warn("Cannot update coverage for %r" % identifier.type)
class LanguageListScript(LibraryInputScript):
"""List all the languages with at least one non-open access work
in the collection.
"""
def process_library(self, library):
print library.short_name
for item in self.languages(library):
print item
def languages(self, library):
":yield: A list of output lines, one per language."
for abbreviation, count in library.estimated_holdings_by_language(
include_open_access=False
).most_common():
display_name = LanguageCodes.name_for_languageset(abbreviation)
yield "%s %i (%s)" % (abbreviation, count, display_name)
class CompileTranslationsScript(Script):
"""A script to combine translation files for circulation, core
and the admin interface, and compile the result to be used by the
app. The combination step is necessary because Flask-Babel does not
support multiple domains yet.
"""
def run(self):
languages = Configuration.localization_languages()
for language in languages:
base_path = "translations/%s/LC_MESSAGES" % language
if not os.path.exists(base_path):
logging.warn("No translations for configured language %s" % language)
continue
os.system("rm %(path)s/messages.po" % dict(path=base_path))
os.system("cat %(path)s/*.po > %(path)s/messages.po" % dict(path=base_path))
os.system("pybabel compile -f -d translations")
class InstanceInitializationScript(Script):
"""An idempotent script to initialize an instance of the Circulation Manager.
This script is intended for use in servers, Docker containers, etc,
when the Circulation Manager app is being installed. It initializes
the database and sets an appropriate alias on the ElasticSearch index.
Because it's currently run every time a container is started, it must
remain idempotent.
"""
def do_run(self, ignore_search=False):
# Creates a "-current" alias on the Elasticsearch client.
if not ignore_search:
try:
search_client = ExternalSearchIndex(self._db)
except CannotLoadConfiguration as e:
# Elasticsearch isn't configured, so do nothing.
pass
# Set a timestamp that represents the new database's version.
db_init_script = DatabaseMigrationInitializationScript(_db=self._db)
existing = get_one(self._db, Timestamp, service=db_init_script.name)
if existing:
# No need to run the script. We already have a timestamp.
return
db_init_script.run()
# Create a secret key if one doesn't already exist.
ConfigurationSetting.sitewide_secret(self._db, Configuration.SECRET_KEY)
class LoanReaperScript(Script):
"""Remove expired loans and holds whose owners have not yet synced
with the loan providers.
This stops the library from keeping a record of the final loans and
holds of a patron who stopped using the circulation manager.
If a loan or (more likely) hold is removed incorrectly, it will be
restored the next time the patron syncs their loans feed.
"""
def do_run(self):
now = datetime.utcnow()
# Reap loans and holds that we know have expired.
for obj, what in ((Loan, 'loans'), (Hold, 'holds')):
qu = self._db.query(obj).filter(obj.end < now)
self._reap(qu, "expired %s" % what)
for obj, what, max_age in (
(Loan, 'loans', timedelta(days=90)),
(Hold, 'holds', timedelta(days=365)),
):
# Reap loans and holds which have no end date and are very
# old. It's very likely these loans and holds have expired
# and we simply don't have the information.
older_than = now - max_age
qu = self._db.query(obj).join(obj.license_pool).filter(
obj.end == None).filter(
obj.start < older_than).filter(
LicensePool.open_access == False
)
explain = "%s older than %s" % (
what, older_than.strftime("%Y-%m-%d")
)
self._reap(qu, explain)
def _reap(self, qu, what):
"""Delete every database object that matches the given query.
:param qu: The query that yields objects to delete.
:param what: A human-readable explanation of what's being
deleted.
"""
counter = 0
print "Reaping %d %s." % (qu.count(), what)
for o in qu:
self._db.delete(o)
counter += 1
if not counter % 100:
print counter
self._db.commit()
self._db.commit()
class DisappearingBookReportScript(Script):
"""Print a TSV-format report on books that used to be in the
collection, or should be in the collection, but aren't.
"""
def do_run(self):
qu = self._db.query(LicensePool).filter(
LicensePool.open_access==False).filter(
LicensePool.suppressed==False).filter(
LicensePool.licenses_owned<=0).order_by(
LicensePool.availability_time.desc())
first_row = ["Identifier",
"Title",
"Author",
"First seen",
"Last seen (best guess)",
"Current licenses owned",
"Current licenses available",
"Changes in number of licenses",
"Changes in title availability",
]
print "\t".join(first_row)
for pool in qu:
self.explain(pool)
def investigate(self, licensepool):
"""Find when the given LicensePool might have disappeared from the
collection.
:param licensepool: A LicensePool.
:return: a 3-tuple (last_seen, title_removal_events,
license_removal_events).
`last_seen` is the latest point at which we knew the book was
circulating. If we never knew the book to be circulating, this
is the first time we ever saw the LicensePool.
`title_removal_events` is a query that returns CirculationEvents
in which this LicensePool was removed from the remote collection.
`license_removal_events` is a query that returns
CirculationEvents in which LicensePool.licenses_owned went
from having a positive number to being zero or a negative
number.
"""
first_activity = None
most_recent_activity = None
# If we have absolutely no information about the book ever
# circulating, we act like we lost track of the book
# immediately after seeing it for the first time.
last_seen = licensepool.availability_time
# If there's a recorded loan or hold on the book, that can
# push up the last time the book was known to be circulating.
for l in (licensepool.loans, licensepool.holds):
for item in l:
if not last_seen or item.start > last_seen:
last_seen = item.start
# Now we look for relevant circulation events. First, an event
# where the title was explicitly removed is pretty clearly
# a 'last seen'.
base_query = self._db.query(CirculationEvent).filter(
CirculationEvent.license_pool==licensepool).order_by(
CirculationEvent.start.desc()
)
title_removal_events = base_query.filter(
CirculationEvent.type==CirculationEvent.DISTRIBUTOR_TITLE_REMOVE
)
if title_removal_events.count():
candidate = title_removal_events[-1].start
if not last_seen or candidate > last_seen:
last_seen = candidate
# Also look for an event where the title went from a nonzero
# number of licenses to a zero number of licenses. That's a
# good 'last seen'.
license_removal_events = base_query.filter(
CirculationEvent.type==CirculationEvent.DISTRIBUTOR_LICENSE_REMOVE,
).filter(
CirculationEvent.old_value>0).filter(
CirculationEvent.new_value<=0
)
if license_removal_events.count():
candidate = license_removal_events[-1].start
if not last_seen or candidate > last_seen:
last_seen = candidate
return last_seen, title_removal_events, license_removal_events
format = "%Y-%m-%d"
def explain(self, licensepool):
edition = licensepool.presentation_edition
identifier = licensepool.identifier
last_seen, title_removal_events, license_removal_events = self.investigate(
licensepool
)
data = ["%s %s" % (identifier.type, identifier.identifier)]
if edition:
data.extend([edition.title, edition.author])
if licensepool.availability_time:
first_seen = licensepool.availability_time.strftime(self.format)
else:
first_seen = ''
data.append(first_seen)
if last_seen:
last_seen = last_seen.strftime(self.format)
else:
last_seen = ''
data.append(last_seen)
data.append(licensepool.licenses_owned)
data.append(licensepool.licenses_available)
license_removals = []
for event in license_removal_events:
description =u"%s: %s→%s" % (
event.start.strftime(self.format), event.old_value,
event.new_value
)
license_removals.append(description)
data.append(", ".join(license_removals))
title_removals = [event.start.strftime(self.format)
for event in title_removal_events]
data.append(", ".join(title_removals))
print "\t".join([unicode(x).encode("utf8") for x in data])
class NYTBestSellerListsScript(Script):
def __init__(self, include_history=False):
super(NYTBestSellerListsScript, self).__init__()
self.include_history = include_history
def do_run(self):
self.api = NYTBestSellerAPI.from_config(self._db)
self.data_source = DataSource.lookup(self._db, DataSource.NYT)
# For every best-seller list...
names = self.api.list_of_lists()
for l in sorted(names['results'], key=lambda x: x['list_name_encoded']):
name = l['list_name_encoded']
self.log.info("Handling list %s" % name)
best = self.api.best_seller_list(l)
if self.include_history:
self.api.fill_in_history(best)
else:
self.api.update(best)
# Mirror the list to the database.
customlist = best.to_customlist(self._db)
self.log.info(
"Now %s entries in the list.", len(customlist.entries))
self._db.commit()
class OPDSForDistributorsImportScript(OPDSImportScript):
"""Import all books from the OPDS feed associated with a collection
that requires authentication."""
IMPORTER_CLASS = OPDSForDistributorsImporter
MONITOR_CLASS = OPDSForDistributorsImportMonitor
PROTOCOL = OPDSForDistributorsImporter.NAME
class OPDSForDistributorsReaperScript(OPDSImportScript):
"""Get all books from the OPDS feed associated with a collection
to find out if any have been removed."""
IMPORTER_CLASS = OPDSForDistributorsImporter
MONITOR_CLASS = OPDSForDistributorsReaperMonitor
PROTOCOL = OPDSForDistributorsImporter.NAME
class LaneResetScript(LibraryInputScript):
"""Reset a library's lanes based on language configuration or estimates
of the library's current collection."""