forked from datalogics-dans/circulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.py
566 lines (479 loc) · 18.1 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
from cStringIO import StringIO
from datetime import 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 api.lanes import make_lanes
from api.controller import CirculationManager
from api.monitor import SearchIndexMonitor
from api.threem import ThreeMCirculationSweep
from api.overdrive import OverdriveAPI
from core import log
from core.lane import Lane
from core.classifier import Classifier
from core.model import (
Contribution,
CustomList,
DataSource,
DeliveryMechanism,
Edition,
Hyperlink,
Identifier,
LicensePool,
LicensePoolDeliveryMechanism,
Representation,
Subject,
Work,
)
from core.scripts import (
Script as CoreScript,
RunCoverageProvidersScript,
RunCoverageProviderScript,
IdentifierInputScript,
RunMonitorScript,
)
from core.lane import (
Pagination,
Facets,
)
from api.config import Configuration
from core.opds_import import (
SimplifiedOPDSLookup,
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.opds import CirculationManagerAnnotator
from api.circulation import CirculationAPI
from api.overdrive import (
OverdriveAPI,
OverdriveBibliographicCoverageProvider,
)
from api.threem import (
ThreeMAPI,
ThreeMBibliographicCoverageProvider,
)
from api.axis import (
Axis360API,
)
from core.axis import Axis360BibliographicCoverageProvider
class Script(CoreScript):
def load_config(self):
if not Configuration.instance:
Configuration.load()
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):
self.metadata_url = (
metadata_web_app_url or Configuration.integration_url(
Configuration.METADATA_WRANGLER_INTEGRATION
)
)
self.lookup = SimplifiedOPDSLookup(self.metadata_url)
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.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 LaneSweeperScript(Script):
"""Do something to each lane in the application."""
def __init__(self):
os.environ['AUTOINITIALIZE'] = "False"
from api.app import app
del os.environ['AUTOINITIALIZE']
app.manager = CirculationManager(self._db)
self.app = app
self.base_url = Configuration.integration_url(
Configuration.CIRCULATION_MANAGER_INTEGRATION, required=True
)
def run(self):
begin = time.time()
client = self.app.test_client()
ctx = self.app.test_request_context(base_url=self.base_url)
ctx.push()
queue = [self.app.manager.top_level_lane]
while queue:
new_queue = []
self.log.debug("Beginning of loop: %d lanes to process", len(queue))
for l in queue:
if self.should_process_lane(l):
self.process_lane(l)
self._db.commit()
for sublane in l.sublanes:
new_queue.append(sublane)
queue = new_queue
ctx.pop()
end = time.time()
self.log.info("Entire process took %.2fsec", (end-begin))
def should_process_lane(self, lane):
return True
def process_lane(self, lane):
pass
class CacheRepresentationPerLane(LaneSweeperScript):
name = "Cache one representation per lane"
def __init__(self, max_depth=None):
self.max_depth = max_depth
super(CacheRepresentationPerLane, self).__init__()
def should_process_lane(self, lane):
if lane.name is None:
return False
if lane.parent is None and not isinstance(lane, Lane):
return False
if self.max_depth and lane.depth > self.max_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_lane(self, lane):
annotator = self.app.manager.annotator(lane)
a = time.time()
lane_key = "%s/%s" % (lane.language_key, lane.name)
self.log.info(
"Generating feed(s) for %s", lane_key
)
cached_feeds = self.do_generate(lane)
b = time.time()
if not isinstance(cached_feeds, list):
cached_feeds = [cached_feeds]
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_key, (b-a), total_size
)
class CacheFacetListsPerLane(CacheRepresentationPerLane):
"""Cache the first two pages of every facet list for this lane."""
name = "Cache first two pages of every facet list for each lane"
def do_generate(self, lane):
feeds = []
annotator = self.app.manager.annotator(lane)
if isinstance(lane, Lane):
languages = lane.language_key
lane_name = None
else:
languages = None
lane_name = None
url = self.app.manager.cdn_url_for(
"feed", languages=lane.languages, lane_name=lane_name
)
order_facets = Configuration.enabled_facets(
Facets.ORDER_FACET_GROUP_NAME
)
availability = Configuration.default_facet(
Facets.AVAILABILITY_FACET_GROUP_NAME
)
collection = Configuration.default_facet(
Facets.COLLECTION_FACET_GROUP_NAME
)
for sort_order in order_facets:
pagination = Pagination.default()
facets = Facets(
collection=collection, availability=availability,
order=sort_order, order_ascending=True
)
title = lane.display_name
for pagenum in (0, 2):
feeds.append(
AcquisitionFeed.page(
self._db, title, url, lane, annotator,
facets=facets, pagination=pagination,
force_refresh=True
)
)
pagination = pagination.next_page
return feeds
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.sublanes:
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):
languages = lane.language_key
lane_name = lane.name
else:
languages = None
lane_name = None
url = self.app.manager.cdn_url_for(
"acquisition_groups", languages=languages, lane_name=lane_name
)
return AcquisitionFeed.groups(
self._db, title, url, lane, annotator,
force_refresh=True
)
class BibliographicCoverageProvidersScript(RunCoverageProvidersScript):
"""Alternate between running bibliographic coverage providers for
all registered book sources.
"""
def __init__(self):
providers = []
if Configuration.integration('3M'):
providers.append(ThreeMBibliographicCoverageProvider)
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 = ThreeMCirculationSweep(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(Script):
"""List all the languages with at least one non-open access work
in the collection.
"""
def do_run(self):
query = self._db.query(Edition.language, func.count(Edition.language)).group_by(Edition.language)
query = query.join(Edition.primary_identifier).join(
Identifier.licensed_through
).join(LicensePool.delivery_mechanisms).join(
LicensePoolDeliveryMechanism.delivery_mechanism
)
# TODO: It would be more reliable to use
# Lane.only_show_ready_deliverable_works here, but that's
# geared towards operating on Work. It's not a big deal since
# this is just to get a general count.
query = query.filter(LicensePool.open_access==False).filter(
LicensePool.licenses_owned > 0
).filter(
Edition.medium==Edition.BOOK_MEDIUM
).filter(
Edition.language != None
).filter(
DeliveryMechanism.default_client_can_fulfill==True
)
name = LanguageCodes.name_for_languageset
sorted_languages = sorted(
query.all(), key=lambda x: (
-x[1], name(x[0])
)
)
sorted_languages = [
(language, count, name(language))
for (language, count) in sorted_languages
]
print "\n".join(["%s %i (%s)" % l for l in sorted_languages])
print json.dumps([l[0] for l in sorted_languages])
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 UpdateSearchIndexScript(RunMonitorScript):
def __init__(self):
parser = argparse.ArgumentParser()
parser.add_argument(
'--works-index',
help='The ElasticSearch index to update, if other than the default.'
)
parsed = parser.parse_args()
super(UpdateSearchIndexScript, self).__init__(
SearchIndexMonitor,
index_name=parsed.works_index,
)