Newer
Older
# Adapted from https://gitlab.kwant-project.org/qt/basnijholt/thesis-bas-nijholt/blob/master/create_bib_file.py
import functools
import glob
import os
from concurrent.futures import ThreadPoolExecutor
import requests
import yaml
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
bib_type, *_ = bib_entry.split("{")
_, *rest = bib_entry.split(",")
rest = ",".join(rest)
# Now only modify `rest` because we don't want to touch the key.
# XXX: I am not sure whether these substitutions are needed.
# the problem seemed to be the utf-8 `requests.get` encoding.
to_replace = [("ö", r"\"{o}"), ("ü", r"\"{u}"), ("ë", r"\"{e}"), ("ï", r"\"{i}")]
for old, new in to_replace:
rest = rest.replace(old.upper(), new.upper())
rest = rest.replace(old.lower(), new.lower())
to_replace = [
(r"a{\r}", r"\r{a}"), # "Nyga{\r}rd" -> "Nyg\r{a}rd", bug in doi.org
("Josephson", "{J}osephson"),
("Majorana", "{M}ajorana"),
("Andreev", "{A}ndreev"),
("Kramers", "{K}ramers"),
("Kitaev", "{K}itaev"),
(
r"metastable0and$\uppi$states",
r"metastable $0$ and $\pi$ states",
), # fix for 10.1103/physrevb.63.214512
(
r"Land{\'{e}}{gFactors}",
r"Land{\'{e}} {$g$} Factors",
), # fix for PhysRevLett.96.026804
]
journals = [
("Advanced Materials", "Adv. Mater."),
("Annals of Physics", "Ann. Phys."),
("Applied Physics Letters", "Appl. Phys. Lett."),
("JETP Lett", "JETP Lett."),
("Journal de Physique", "J. Phys."),
("Journal of Computational Physics", "J. Comput. Phys."),
("Journal of Experimental and Theoretical Physics", "J. Exp. Theor. Phys."),
("Journal of Low Temperature Physics", "J. Low Temp. Phys."),
(
"Journal of Physics A: Mathematical and Theoretical",
"J. Phys. A: Math. Theor.",
),
("Journal of Physics: Condensed Matter", "J. Phys.: Condens. Matter"),
("Nano Letters", "Nano Lett."),
("Nature Communications", "Nat. Commun."),
("Nature Materials", "Nat. Mater."),
("Nature Nanotechnology", "Nat. Nanotechnol."),
("Nature Physics", "Nat. Phys."),
("New Journal of Physics", "New J. Phys."),
("Physical Review B", "Phys. Rev. B"),
("Physical Review Letters", "Phys. Rev. Lett."),
("Physical Review X", "Phys. Rev. X"),
("Physical Review", "Phys. Rev."), # should be before the above subs
("Physics-Uspekhi", "Phys. Usp."),
("Reports on Progress in Physics", "Rep. Prog. Phys."),
("Review of Scientific Instruments", "Rev. Sci. Instrum."),
("Reviews of Modern Physics", "Rev. Mod. Phys."),
("Science Advances", "Sci. Adv."),
("Scientific Reports", "Sci. Rep."),
("Semiconductor Science and Technology", "Semicond. Sci. Technol."),
(
"Annual Review of Condensed Matter Physics",
"Annu. Rev. Condens. Matter Phys.",
),
("{EPL} (Europhysics Letters)", "{EPL}"),
("Nature Reviews Materials", "Nat. Rev. Mater."),
("Physics Letters", "Phys. Lett."),
("The European Physical Journal B", "Eur. Phys. J. B"),
("{SIAM} Journal on Numerical Analysis", "{SIAM} J. Numer. Anal."),
("{AIP} Conference Proceedings", "{AIP} Conf. Proc."),
]
for old, new in to_replace + journals:
rest = rest.replace(old, new)
result = bib_type + "{" + key + "," + rest
print(result, "\n")
return result
@functools.lru_cache()
def doi2bib(doi):
"""Return a bibTeX string of metadata for a given DOI."""
url = "http://dx.doi.org/" + doi
headers = {"accept": "application/x-bibtex"}
r = requests.get(url, headers=headers)
r.encoding = "utf-8"
return r.text
with ThreadPoolExecutor() as ex:
futs = ex.map(doi2bib, list(dois.values()))
bibs = list(futs)
entries = [edit_raw_bibtex_entry(key, bib) for key, bib in zip(dois.keys(), bibs)]
out_file.write("@preamble{ {\\providecommand{\\BIBYu}{Yu} } }\n\n")
out_file.write(f"\n% Below is from `{fname}`.\n\n")
with open(fname) as in_file:
out_file.write(in_file.read())
out_file.write("\n% Below is from `paper.yaml`.\n\n")