-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathload_languages.py
More file actions
526 lines (428 loc) · 16.2 KB
/
load_languages.py
File metadata and controls
526 lines (428 loc) · 16.2 KB
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
# coding: utf-8
"""
This scripts loads a list of PDF, Translation HTML's and XML's available
in a given SciELO Website and try to figure out the available versions of the
fulltexts for each SciELO Document stored into the ArticleMeta.
The lists of PDF, HTML's and XML's must be available in the SciELO Website
according to the following examples:
PDF: http://www.scielo.br/static_pdf_catalog.txt
Translation HTML: http://www.scielo.br/static_html_catalog.txt
XML: http://www.scielo.br/static_xml_catalog.txt
"""
import re
import os
import sys
import argparse
import logging
import logging.config
from datetime import datetime, timedelta
import requests
from articlemeta import controller
from xylose.scielodocument import Article
logger = logging.getLogger(__name__)
SENTRY_DSN = os.environ.get('SENTRY_DSN', None)
LOGGING_LEVEL = os.environ.get('LOGGING_LEVEL', 'DEBUG')
STATIC_CATALOG_FALLBACK_DOMAIN = os.environ.get('STATIC_CATALOG_FALLBACK_DOMAIN', None)
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'console': {
'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s',
'datefmt': '%H:%M:%S',
},
},
'handlers': {
'console': {
'level': LOGGING_LEVEL,
'class': 'logging.StreamHandler',
'formatter': 'console'
}
},
'loggers': {
'': {
'handlers': ['console'],
'level': LOGGING_LEVEL,
'propagate': False,
},
'processing.load_languages': {
'level': LOGGING_LEVEL,
'propagate': True,
},
}
}
# if SENTRY_DSN:
# # com raven (python < 3.7)
# # LOGGING['handlers']['sentry'] = {
# # 'level': 'ERROR',
# # 'class': 'raven.handlers.logging.SentryHandler',
# # 'dsn': SENTRY_DSN,
# # }
# # LOGGING['loggers']['']['handlers'].append('sentry')
# sentry_sdk.init(
# dsn=SENTRY_DSN,
# integrations=[sentry_sdk.integrations.LoggingIntegration()]
# )
FROM = datetime.now() - timedelta(days=15)
FROM = FROM.isoformat()[:10]
FILE_REGEX = re.compile(r'serial.*.htm|.*.xml|.*.pdf')
data_struct_regex = re.compile(r'^fulltexts\.(pdf|html)\.[a-z][a-z]$')
def get_acron_issueid_fname_without_extension(file_path):
"""
Recebe file_path que é o arquivo fonte do artigo,
podendo ter os seguintes padrões:
- xml: delta/v32n2/1678-460X-delta-32-02-00543.xml
- html: V:\\Scielo\\serial\\dpjo\\v15n3\\markup\\05.htm
- pdf: d:/c.917173/scielo/serial.lilacs//mioc/v51/markup/v51/tomo51(f1)_17-74.pdf
Retorna uma lista cujo itens sao
acron, issue, nome do arquivo sem extensao, em minúsculas
- xml: (delta, v32n2, 1678-460x-delta-32-02-00543)
- html: (dpjo, v15n3, 05)
- pdf: (mioc, v51, tomo51(f1)_17-74)
"""
_file_path = os.path.normpath(file_path.replace('\\', '/')).lower()
try:
file_id = FILE_REGEX.search(_file_path).group()
file_id, ext = os.path.splitext(file_id)
folders = file_id.split('/')
if not ext == '.xml':
folders = [item for item in folders if item not in ['markup', '']]
if folders[-2] == folders[-3]:
del folders[-2]
folders = folders[-3:]
if len(folders) != 3:
logger.error(
u'Fail to parse file_path %s for %s', (file_path, file_id))
return
except:
logger.error(
u'Fail to parse file_path %s for %s', (file_path, file_id))
return
return folders
def collections_acronym(articlemeta_db):
collections = articlemeta_db['collections'].find({}, {'_id': 0})
return [i['code'] for i in collections]
def collection_info(collection, articlemeta_db):
info = articlemeta_db['collections'].find_one({'acron': collection}, {'_id': 0})
return info
def do_request(url, json=True):
headers = {
'User-Agent': 'SciELO Processing ArticleMeta: LoadLanguage'
}
try:
document = requests.get(url, headers=headers)
except:
logger.error(u'HTTP request error for: %s', url)
else:
if json:
return document.json()
else:
return document
def load_documents(collection, articlemeta_db, all_records=False):
"""
Carrega dos documentos da base de dados mongodb do AM.
"""
fltr = {
'collection': collection
}
if all_records is False:
fltr['fulltexts'] = {'$exists': 0}
documents = articlemeta_db['articles'].find(
fltr,
{'code': 1}, no_cursor_timeout=True
)
pids = []
for document in documents:
pids.append(document['code'])
if 'fulltexts' in fltr:
del(fltr['fulltexts'])
for pid in pids:
fltr['code'] = pid
document = articlemeta_db['articles'].find_one(
fltr,
{'_id': 0, 'citations': 0}
)
yield Article(document)
documents.close()
class StaticCatalog(object):
def __init__(self, collection, fallback_domain=None):
self.catalog = {}
self.fallback_domain = fallback_domain
self._load_static_catalog(collection, 'pdf')
self._load_static_catalog(collection, 'html')
self._load_static_catalog(collection, 'xml')
def _load_static_catalog(self, source, tipe):
"""
source: www.scielo.br
type: in ['pdf', 'html', 'xml']
Download the static files text lists from the selected SciELO Domain.
Parse the txt file to produce a dictionary with the following structure:
{
'jbco': {
'v6n3': {
'pdf': [
'0001',
'0002',
'0003',
'0004',
'en_0004',
...
],
'html': [
'0001',
'0002',
'0003',
'0004',
'en_0004',
...
]
}
},
...
}
"""
logger.info(u'Loading static_%s_files.txt from server %s', tipe, source)
filename = 'static_%s_files.txt' % tipe
url = '/'.join(['http:/', source, filename])
response = do_request(url, json=False)
# If primary domain fails and fallback domain is configured, try fallback
if response is None and self.fallback_domain:
logger.warning(u'Failed to load from %s, trying fallback domain %s', source, self.fallback_domain)
fallback_url = '/'.join(['http:/', self.fallback_domain, filename])
response = do_request(fallback_url, json=False)
if response is None:
logger.error(u'Failed to load static catalog from %s (and fallback if configured)', source)
return
content = response.iter_lines(decode_unicode='utf-8')
for line in sorted([i for i in content]):
splitedline = line.lower().split('/')[1:]
if not len(splitedline) == 3:
continue
self.catalog.setdefault(splitedline[0], {})
self.catalog[splitedline[0]].setdefault(splitedline[1], {'pdf': [], 'html': [], 'xml': []})
self.catalog[splitedline[0]][splitedline[1]][tipe].append(splitedline[2].replace('.html', '.htm')[:-4])
def _file_id(self, file_path):
return get_acron_issueid_fname_without_extension(file_path)
def _file_name(self, file_code):
file_code = file_code.replace('/', '___').replace('\\', '___')
file_code = file_code.replace('.html', '.htm')
return file_code.split('___')[-1][:-4]
def is_file_available(self, file_id, tipe, language, original_language):
"""
This method checks the existence of the file_id agains the catalog.
file_id:
Is a array with the keys to find a file inside the catalog.
Ex:
['rsp', 'v48n6', '0034-8910-rsp-48-6-0873']
language:
ISO 2 letters.
tipe:
'html' or 'pdf'
"""
logger.debug(
u'Checking fulltexts in %s %s for %s, %s, %s',
tipe,
language,
file_id[0],
file_id[1],
file_id[2]
)
if language == original_language:
file_name = file_id[2]
else:
file_name = '_'.join([language, file_id[2]])
files = []
try:
files = self.catalog[file_id[0]][file_id[1]][tipe]
except:
logger.warning(
u'Issue not found int catalog for %s %s for %s, %s, %s',
tipe,
language,
file_id[0],
file_id[1],
file_id[2]
)
if file_name.lower() in files:
return True
return False
def fulltexts(self, document):
"""
This method retrieve a dictionary of the available fulltexts in each
document type ['pdf', 'html', 'xml']
input:
xylose.scielo_document.Article()
ex:
output:
{
'pdf': {
'pt': 'url',
'es': 'url',
'en': 'url'
},
'html': {
'pt': 'url',
'es': 'url',
'en': 'url'
}
}
"""
file_id = self._file_id(document.file_code(fullpath=True))
if not file_id:
logger.error(u'Fail to parse file_id for %s_%s', document.collection_acronym, document.publisher_id)
return None
if not document.journal.languages:
logger.info(u'Journal without publication languages defined %s', file_id[0])
return None
data = {'fulltexts.pdf': set(), 'fulltexts.html': set()}
data['fulltexts.html'].add(document.original_language()) # Original language must have fulltext in html.
if document.data_model_version == 'xml':
for lang in document.xml_languages() or []:
data['fulltexts.html'].add(lang)
languages = document.journal.languages + document.languages()
languages.append(document.original_language())
for language in set(languages):
if self.is_file_available(file_id, 'pdf', language, document.original_language()):
data['fulltexts.pdf'].add(language)
logger.info(
u'Fulltext available in pdf %s for %s, %s, %s',
language,
file_id[0],
file_id[1],
file_id[2]
)
if self.is_file_available(file_id, 'html', language, document.original_language()):
data['fulltexts.html'].add(language)
logger.info(
u'Fulltext available in html %s for %s, %s, %s',
language,
file_id[0],
file_id[1],
file_id[2]
)
ldata = {}
if len(data['fulltexts.pdf']) > 0:
for lang in data['fulltexts.pdf']:
if lang != document.original_language():
fname = '_'.join([
lang,
self._file_name(document.file_code(fullpath=True))]
)
else:
fname = self._file_name(document.file_code(fullpath=True))
ldata['fulltexts.pdf.%s' % lang] = 'http://%s' % '/'.join([
document.scielo_domain,
'pdf',
file_id[0],
file_id[1],
'%s.pdf' % fname
]
)
if len(data['fulltexts.html']) > 0:
for lang in data['fulltexts.html']:
ldata['fulltexts.html.%s' % lang] = 'http://%s/scielo.php?script=sci_arttext&pid=%s&tlng=%s' % (
document.scielo_domain,
document.publisher_id,
lang
)
return ldata
def run(collections, articlemeta_db, all_records=False, forced_url=None, fallback_domain=None):
if not isinstance(collections, list):
logger.error('Collections must be a list o collection acronym')
exit()
for collection in collections:
coll_info = collection_info(collection, articlemeta_db)
collection_domain = forced_url if forced_url else coll_info['domain']
logger.info(u'Loading languages for %s', collection_domain)
logger.info(u'Using mode all_records %s', str(all_records))
# Use environment variable fallback if not provided as parameter
effective_fallback_domain = fallback_domain if fallback_domain else STATIC_CATALOG_FALLBACK_DOMAIN
if effective_fallback_domain:
logger.info(u'Using fallback domain: %s', effective_fallback_domain)
static_catalogs = StaticCatalog(collection_domain, fallback_domain=effective_fallback_domain)
for document in load_documents(collection, articlemeta_db,
all_records=all_records):
logger.debug(
u'Checking fulltexts for %s_%s',
collection,
document.publisher_id
)
fulltexts = static_catalogs.fulltexts(document)
if not isinstance(fulltexts, dict):
logger.warning(
u'Document not loaded for %s_%s',
collection,
document.publisher_id
)
continue
for key in fulltexts.keys():
if not data_struct_regex.match(key):
logger.warning(
u'Document not loaded for %s_%s',
collection,
document.publisher_id
)
continue
articlemeta_db['articles'].update_one(
{'code': document.publisher_id, 'collection': document.collection_acronym},
{'$set': static_catalogs.fulltexts(document)}
)
logger.debug(
u'Update made for %s_%s',
collection,
document.publisher_id
)
def main():
db_dsn = os.environ.get('MONGODB_HOST', 'mongodb://localhost:27017/articlemeta')
try:
articlemeta_db = controller.get_dbconn(db_dsn)
except:
print('Fail to connect to:', db_dsn)
sys.exit(1)
parser = argparse.ArgumentParser(
description="Load Languages from SciELO static files available in the file system"
)
_collections_acronyms = collections_acronym(articlemeta_db)
parser.add_argument(
'--collection',
'-c',
choices=_collections_acronyms,
help='Collection acronym'
)
parser.add_argument(
'--all_records',
'-a',
action='store_true',
help='Apply processing to all records or just records without the languages parameter'
)
parser.add_argument(
'--logging_file',
'-o',
help='Full path to the log file'
)
parser.add_argument(
'--logging_level',
'-l',
default=LOGGING_LEVEL,
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
help='Logggin level'
)
parser.add_argument(
'--domain',
'-d',
help='Collection domain to get Static catalog'
)
parser.add_argument(
'--fallback_domain',
'-f',
help='Fallback domain to try if primary domain fails (e.g., antigo.scielo.br)'
)
args = parser.parse_args()
LOGGING['handlers']['console']['level'] = args.logging_level
for lg, content in LOGGING['loggers'].items():
content['level'] = args.logging_level
logging.config.dictConfig(LOGGING)
collections = [args.collection] if args.collection else _collections_acronyms
run(collections, articlemeta_db, args.all_records, args.domain, args.fallback_domain)
if __name__ == '__main__':
main()