Skip to content

Commit 9e15dc6

Browse files
authored
Handle DOI request failures
1 parent 6b764f8 commit 9e15dc6

1 file changed

Lines changed: 26 additions & 8 deletions

File tree

bin/arxiv_to_publications_correct.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55

66
import bibtexparser
77
from bibtexparser.bwriter import BibTexWriter
8+
from requests.exceptions import RequestException
9+
10+
11+
def fetch_doi_content(url, accept_header, description):
12+
try:
13+
response = requests.get(url, headers={'Accept': accept_header}, timeout=30)
14+
response.raise_for_status()
15+
except RequestException as exc:
16+
print(f'Ignoring {url}, failed to fetch {description}: {exc}\n\n')
17+
return None
18+
return response
819

920

1021
if __name__ == '__main__':
@@ -21,16 +32,18 @@
2132

2233
for url, id_db in zip(doi_list, id_list):
2334
print(f'Working on {id_db} with URL {url}')
24-
req = requests.get(url, headers={'Accept': 'application/x-bibtex'})
25-
if not req.status_code == 200:
26-
print(f'Ignoring {url}, got status code {req.status_code}\n\n')
35+
req = fetch_doi_content(url, 'application/x-bibtex', 'BibTeX')
36+
if req is None:
2737
continue
2838
bib = req.content.decode()
29-
req = requests.get(url, headers={'Accept': 'application/json'})
30-
if not req.status_code == 200:
31-
print(f'Ignoring {url}, got status code {req.status_code}\n\n')
39+
req = fetch_doi_content(url, 'application/json', 'metadata')
40+
if req is None:
41+
continue
42+
try:
43+
data = req.json()
44+
except ValueError as exc:
45+
print(f'Ignoring {url}, invalid metadata response: {exc}\n\n')
3246
continue
33-
data = req.json()
3447

3548
if len(data['author']) > 1:
3649
id = data['author'][0]['family'] + 'EtAl' + str(data['issued']['date-parts'][0][0])
@@ -39,7 +52,12 @@
3952
id = id.replace(" ", "_")
4053

4154
entries = db.get_entry_dict()
42-
assert entries[id_db]["ENTRYTYPE"] == 'unpublished', "original entry in bib file was NOT unpublished !"
55+
if id_db not in entries:
56+
print(f'Ignoring {id_db}, entry not found in bibliography.\n\n')
57+
continue
58+
if entries[id_db]["ENTRYTYPE"] != 'unpublished':
59+
print(f'Ignoring {id_db}, original entry in bib file was not unpublished.\n\n')
60+
continue
4361
db.entries.remove(entries[id_db])
4462

4563
# Check for duplicate keys in the remaining database and add letter suffixes if needed

0 commit comments

Comments
 (0)