-
Notifications
You must be signed in to change notification settings - Fork 3
/
exportsci.py
340 lines (265 loc) · 10.9 KB
/
exportsci.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
# coding: utf-8
from datetime import datetime
import os
import argparse
import logging
import tools
import utils
from lxml import etree
from utils import earlier_yyyymmdd
logger = logging.getLogger(__name__)
config = utils.Configuration.from_env()
settings = dict(config.items())['main:exportsci']
FTP_HOST = settings['ftp_host']
FTP_USER = settings['ftp_user']
FTP_PASSWD = settings['ftp_passwd']
MONGODB_HOST = settings['mongodb_host']
MONGODB_SLAVEOK = bool(settings['mongodb_slaveok'])
WOS_COLLECTIONS_ALLOWED = settings['wos_collections_allowed'].strip().split(",")
def _config_logging(logging_level='INFO', logging_file=None):
allowed_levels = {
'DEBUG': logging.DEBUG,
'INFO': logging.INFO,
'WARNING': logging.WARNING,
'ERROR': logging.ERROR,
'CRITICAL': logging.CRITICAL
}
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger.setLevel(allowed_levels.get(logging_level, 'INFO'))
if logging_file:
hl = logging.FileHandler(logging_file, mode='a')
else:
hl = logging.StreamHandler()
hl.setFormatter(formatter)
hl.setLevel(allowed_levels.get(logging_level, 'INFO'))
logger.addHandler(hl)
return logger
def run(task='add', clean_garbage=False, normalize=True):
working_dir = os.listdir('.')
logger.debug('Validating working directory %s' % working_dir)
if not 'controller' in working_dir:
logger.error('Working dir does not have controller directory')
exit()
if not 'reports' in working_dir:
logger.error('Working dir does not have reports directory')
exit()
if not 'xml' in working_dir:
logger.error('Working dir does not have xml directory')
exit()
# Setup a connection to SciELO Network Collection
logger.debug("Connecting to mongodb with DataHandler thru %s" % (MONGODB_HOST))
dh = tools.DataHandler(MONGODB_HOST)
now = datetime.now().isoformat()[0:10]
index_issn = 0
collections = dh.load_collections_metadata()
if clean_garbage:
logger.debug("Removing previous XML files")
os.system('rm -f xml/*.xml')
logger.debug("Removing previous zip files")
os.system('rm -f *.zip')
logger.debug("Removing previous error report files")
os.system('rm -f report/*errors.txt')
if task == 'update':
logger.debug("Loading toupdate.txt ISSN's file from FTP controller directory")
tools.get_to_update_file_from_ftp(ftp_host=FTP_HOST,
user=FTP_USER,
passwd=FTP_PASSWD)
issns = tools.load_journals_list(journals_file='controller/toupdate.txt')
elif task == 'add':
logger.debug("Loading keepinto.txt ISSN's file from FTP controller directory")
tools.get_keep_into_file_from_ftp(ftp_host=FTP_HOST,
user=FTP_USER,
passwd=FTP_PASSWD)
issns = tools.load_journals_list(journals_file='controller/keepinto.txt')
logger.debug("Remove previous inbound files")
tools.remove_previous_unbound_files_from_ftp(ftp_host=FTP_HOST,
user=FTP_USER,
passwd=FTP_PASSWD)
logger.debug("Syncing XML's status according to WoS validated files")
tools.get_sync_file_from_ftp(ftp_host=FTP_HOST,
user=FTP_USER,
passwd=FTP_PASSWD)
dh.sync_sent_documents(remove_origin=clean_garbage)
logger.debug("Creating file with a list of documents to be removed from WoS")
tools.get_take_off_files_from_ftp(ftp_host=FTP_HOST,
user=FTP_USER,
passwd=FTP_PASSWD,
remove_origin=clean_garbage)
ids_to_remove = dh.load_pids_list_to_be_removed()
tools.send_take_off_files_to_ftp(ftp_host=FTP_HOST,
user=FTP_USER,
passwd=FTP_PASSWD,
remove_origin=clean_garbage)
logger.debug("Defining document types elegible to send to SCI")
dh.set_elegible_document_types()
xml_validator = tools.XMLValidator()
if not os.path.exists('xml'):
os.makedirs('xml')
# Loading XML files
for issn in issns:
index_issn = index_issn + 1
if issn in ids_to_remove:
logger.debug("Issn {0} is available in the takeoff and keepinto file. For now this ISSN was ignored, and will not be send to WoS until it is removed from the takeoff file.".format(issn))
continue
proc_date_ctrl = ProcessingDateController(issn)
if task == 'update':
try:
documents = dh.sent_to_wos_with_proc_date(
issn,
proc_date_ctrl.from_date,
)
except:
documents = None
if documents is None:
documents = dh.sent_to_wos(issn)
xml_file_name = "xml/SciELO_COR_{0}_{1}.xml".format(now, issn)
elif task == 'add':
try:
documents = dh.not_sent_with_proc_date(
WOS_COLLECTIONS_ALLOWED,
issn,
proc_date_ctrl.from_date,
publication_year=2002,
)
except:
documents = None
if documents is None:
documents = dh.not_sent(
WOS_COLLECTIONS_ALLOWED,
issn, publication_year=2002)
xml_file_name = "xml/SciELO_{0}_{1}.xml".format(now, issn)
if os.path.exists(xml_file_name):
logger.warning("File {0} already exists".format(xml_file_name))
continue
nsmap = {
'xml': 'http://www.w3.org/XML/1998/namespace',
'xlink': 'http://www.w3.org/1999/xlink'
}
global_xml = etree.Element('articles', nsmap=nsmap)
global_xml.set('dtd-version', '1.12')
global_xml.set('{http://www.w3.org/2001/XMLSchema-instance}noNamespaceSchemaLocation', 'Clarivate_publishing_1.12.xsd')
pids = []
for total, current, document in documents:
try:
if current == 1:
logger.info("validating xml's {0} for {1}".format(total, issn))
logger.info("validating xml {0}/{1}".format(current, total))
#skip ahead documents
if 'v32' in document['article'] and 'ahead' in document['article']['v32'][0]['_'].lower():
continue
if skip_because_of_processing_date(proc_date_ctrl, document):
continue
xml = xml_validator.validate_xml(document['collection'], document['code'])
if xml:
global_xml.append(xml.find('article'))
pids.append(document['code'])
except Exception as exc:
logger.exception('unhandled exception during validation of "%s"', document['code'])
# Convertendo XML para texto
try:
textxml = etree.tostring(global_xml, encoding='utf-8', method='xml')
except:
pass
if len(global_xml.findall('article')) == 0:
continue
xml_file = open(xml_file_name, 'w')
xml_file.write(textxml)
xml_file.close()
dh.mark_documents_as_sent_to_wos(pids)
#zipping files
files = os.listdir('xml')
zipped_file_name = tools.packing_zip(files)
#sending to ftp.scielo.br
tools.send_to_ftp(zipped_file_name,
ftp_host=FTP_HOST,
user=FTP_USER,
passwd=FTP_PASSWD)
def skip_because_of_processing_date(proc_date_ctrl, document):
try:
processing_date = _get_processing_date(document)
if processing_date and processing_date < proc_date_ctrl.from_date:
logger.info(
'Skipping because of the processing date: %s < %s' %
(processing_date, proc_date_ctrl.from_date)
)
return True
proc_date_ctrl.save_most_recent_processing_date(processing_date)
except Exception as e:
logger.exception(
"Processing date [%s]: %s" % (document['code'], e)
)
def _get_processing_date(document):
try:
return document['article'].get('v91', [{'_': ''}])[0]['_']
except (KeyError, IndexError, ValueError, TypeError):
return None
class ProcessingDateController:
def __init__(self, issn, safer_days=None):
self._issn = issn
self._from_date = None
self._safer_days = safer_days or 10
self._file_path = "processing_dates/{}.txt".format(self._issn)
_dirname = os.path.dirname(self._file_path)
if not os.path.isdir(_dirname):
os.makedirs(_dirname)
@property
def from_date(self):
if self._from_date is None:
most_recent = self._read_most_recent_processing_date()
if most_recent:
self._from_date = earlier_yyyymmdd(
most_recent, days=self._safer_days)
else:
self._from_date = earlier_yyyymmdd()
return self._from_date
def _read_most_recent_processing_date(self):
try:
with open(self._file_path, "r") as fp:
return fp.read()
except:
return None
def save_most_recent_processing_date(self, processing_date):
try:
if processing_date and processing_date > self.from_date:
with open(self._file_path, "w") as fp:
fp.write(processing_date)
except:
return None
def main():
here = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(here, 'VERSION')) as f:
VERSION = f.read()
print("Export SciELOCI %s" % VERSION)
parser = argparse.ArgumentParser(
description="Control the process of sending metadata to WoS")
parser.add_argument(
'-t',
'--task',
default='add',
choices=['add', 'update'],
help='Task that will be executed.'
)
parser.add_argument(
'-c',
'--clean_garbage',
action='store_true',
default=False,
help='Remove processed files from FTP.'
)
parser.add_argument(
'--logging_file',
'-o',
default='/var/log/exportsci/export_sci.log',
help='Full path to the log file'
)
parser.add_argument(
'--logging_level',
'-l',
default='DEBUG',
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
help='Logggin level'
)
args = parser.parse_args()
_config_logging(args.logging_level, args.logging_file)
logging.debug("Export SciELOCI %s" % VERSION)
run(task=str(args.task), clean_garbage=bool(args.clean_garbage))