-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathissn_portal.py
330 lines (256 loc) · 9.38 KB
/
issn_portal.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
import argparse
import json
import os
import threading
import time
from queue import Queue, Empty
import boto3
import requests
from tenacity import retry, stop_after_attempt, wait_exponential, \
retry_if_exception
from common import S3_BUCKET, LOGGER
BATCH_SIZE = 1000
token_lock = threading.Lock()
current_token = None
def get_auth_token():
username = os.environ.get('ISSN_PORTAL_USERNAME')
password = os.environ.get('ISSN_PORTAL_PASS')
if not username or not password:
raise ValueError("ISSN Portal credentials not found in environment variables")
auth_url = f"https://api.issn.org/authenticate/{username}/{password}"
response = requests.get(auth_url, headers={"Accept": "application/json"})
response.raise_for_status()
return response.json()['token']
def get_current_token():
global current_token
with token_lock:
if current_token is None:
current_token = get_auth_token()
return current_token
def refresh_token():
global current_token
with token_lock:
current_token = get_auth_token()
LOGGER.info("Successfully refreshed authentication token")
return current_token
def should_retry_exception(exception):
if isinstance(exception, requests.exceptions.HTTPError):
if exception.response.status_code == 403:
try:
refresh_token()
return True
except Exception as e:
LOGGER.error(f"Failed to refresh token: {e}")
return False
return False
@retry(
retry=retry_if_exception(should_retry_exception),
wait=wait_exponential(multiplier=1, min=4, max=300),
stop=stop_after_attempt(5),
before_sleep=lambda retry_state: LOGGER.info(
f"Got 403, retrying with fresh token. Attempt {retry_state.attempt_number}")
)
def fetch_issn_record(issn):
url = f"https://api.issn.org/notice/{issn}"
headers = {
"Accept": "application/json",
"Authorization": f"JWT {get_current_token()}"
}
params = {
"json": "true"
}
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
return response.json()
@retry(
retry=retry_if_exception(should_retry_exception),
wait=wait_exponential(multiplier=1, min=4, max=300),
stop=stop_after_attempt(5),
before_sleep=lambda retry_state: LOGGER.info(
f"Got 403, retrying with fresh token. Attempt {retry_state.attempt_number}")
)
def search_by_display_name(display_name):
search_params = {
"search": [f"keytitle={display_name}"],
"page": 0,
"size": 1
}
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": f"JWT {get_current_token()}"
}
response = requests.post("https://api.issn.org/search?json=true",
headers=headers, json=search_params)
response.raise_for_status()
j = response.json()
if len(j) > 0:
return json.loads(j[0])
return None
def load_sources():
s3 = boto3.client('s3')
response = s3.get_object(Bucket='openalex-ingest',
Key='issn-portal/sources.json')
return json.loads(response['Body'].read().decode('utf-8'))
def get_resume_index():
s3 = boto3.client('s3')
bucket_name = 'openalex-ingest'
folder_path = 'issn-portal/journals/'
try:
files_count = 0
paginator = s3.get_paginator('list_objects_v2')
for page in paginator.paginate(Bucket=bucket_name, Prefix=folder_path):
if 'Contents' in page:
for file in page['Contents']:
if file['Key'] != folder_path:
files_count += 1
resume_index = files_count * BATCH_SIZE
starting_batch = files_count
LOGGER.info(
f"Found {files_count} files in S3, resuming from index {resume_index} with batch {starting_batch}")
return resume_index, starting_batch
except Exception as e:
LOGGER.error(f"Error getting number of files from S3: {e}")
return 0, 0
def upload_batch(batch_records, batch_number, s3_client):
try:
object_key = f"issn-portal/journals/{batch_number}.json"
s3_client.put_object(
Bucket=S3_BUCKET,
Key=object_key,
Body=json.dumps(batch_records),
ContentType='application/json'
)
except Exception as e:
LOGGER.error(f"Error uploading batch {batch_number}: {e}")
class ProgressTracker:
def __init__(self, total_sources):
self.total_sources = total_sources
self.completed_sources = 0
self.successful_sources = 0
self.failed_sources = 0
self.lock = threading.Lock()
def increment(self, success=True):
with self.lock:
self.completed_sources += 1
if success:
self.successful_sources += 1
else:
self.failed_sources += 1
def get_stats(self):
with self.lock:
return (self.completed_sources, self.successful_sources,
self.failed_sources, self.total_sources)
def progress_logger(tracker, stop_event):
start_time = time.time()
while not stop_event.is_set():
completed, successes, failures, total = tracker.get_stats()
elapsed_hours = (time.time() - start_time) / 3600
rate_per_hour = completed / elapsed_hours if elapsed_hours > 0 else 0
percent_complete = (completed / total * 100) if total > 0 else 0
success_rate = (successes / completed * 100) if completed > 0 else 0
failure_rate = (failures / completed * 100) if completed > 0 else 0
LOGGER.info(
f"Progress: {completed}/{total} sources ({percent_complete:.1f}%) | "
f"Success: {successes} ({success_rate:.1f}%) | "
f"Failed: {failures} ({failure_rate:.1f}%) | "
f"Rate: {rate_per_hour:.0f}/hour")
time.sleep(5)
def worker(queue, results_queue, tracker):
while True:
item = queue.get()
if item is None:
break
try:
source = item
record = None
for issn in source['issns']:
try:
record = fetch_issn_record(issn)
break
except Exception as e:
LOGGER.error(f'Error fetching ISSN {issn}: {e}')
continue
if not record:
record = search_by_display_name(source['display_name'])
if record:
results_queue.put(record)
tracker.increment(success=True)
else:
tracker.increment(success=False)
except Exception as e:
LOGGER.error(f"Error processing source {source['display_name']}: {e}")
tracker.increment(success=False)
queue.task_done()
def upload_worker(results_queue, starting_batch=0):
s3 = boto3.client('s3')
current_batch = []
batch_number = starting_batch
while True:
try:
record = results_queue.get(timeout=300)
current_batch.append(record)
if len(current_batch) >= BATCH_SIZE:
upload_batch(current_batch, batch_number, s3)
batch_number += 1
current_batch = []
except Empty:
if current_batch:
upload_batch(current_batch, batch_number, s3)
break
results_queue.task_done()
def main(num_threads):
try:
refresh_token()
LOGGER.info("Successfully authenticated with ISSN Portal")
except Exception as e:
LOGGER.error(f"Authentication failed: {e}")
return
sources = load_sources()
resume_index, starting_batch = get_resume_index()
if resume_index > 0:
sources = sources[resume_index:]
LOGGER.info(
f"Resuming from index {resume_index}, {len(sources)} sources remaining, starting at batch {starting_batch}")
total_sources = len(sources)
LOGGER.info(f"Loaded {total_sources} sources to process")
if not total_sources:
LOGGER.info("No sources to process")
return
tracker = ProgressTracker(total_sources)
stop_event = threading.Event()
progress_thread = threading.Thread(
target=progress_logger,
args=(tracker, stop_event)
)
progress_thread.start()
source_queue = Queue()
results_queue = Queue()
workers = []
for _ in range(num_threads):
t = threading.Thread(target=worker,
args=(source_queue, results_queue, tracker))
t.start()
workers.append(t)
upload_thread = threading.Thread(target=upload_worker,
args=(results_queue, starting_batch))
upload_thread.start()
for source in sources:
source_queue.put(source)
for _ in workers:
source_queue.put(None)
for w in workers:
w.join()
results_queue.join()
upload_thread.join()
stop_event.set()
progress_thread.join()
completed, successes, failures, total = tracker.get_stats()
LOGGER.info(
f"Completed processing {completed}/{total} sources. "
f"Successes: {successes}, Failures: {failures}")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--threads', type=int, default=10)
args = parser.parse_args()
main(args.threads)