-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_citations.js
62 lines (57 loc) · 1.58 KB
/
get_citations.js
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
import fs from "fs";
import { setTimeout } from "timers/promises";
const data = JSON.parse(fs.readFileSync("LINKS.json", "utf8"));
(async () => {
let timeout = 1000;
for (let i = 0; i < data.links.length; ) {
const d = data.links[i];
try {
if (d.pub) {
const doi = d.pub.doi;
if (
doi.includes("zenodo") ||
doi.includes("figshare") ||
doi.includes("10.13140/RG.2.2.15289.39522") ||
doi.includes("micropub")
) {
i++;
continue;
}
if (d.pub.year && !process.env.ALL_CITATIONS) {
i++;
continue;
}
console.log(
i + "/" + data.links.length,
"curr waittime",
timeout,
"doi",
doi,
);
const url = doi.startsWith("http") ? doi : "https://doi.org/" + doi;
const response = await fetch(url, {
headers: { Accept: "application/json" },
});
if (!response.ok) {
throw new Error(
`failed ${response.statusText} ${await response.text()}`,
);
}
const json = await response.json();
d.pub.year = +json.published["date-parts"][0][0];
d.pub.title = json.title;
d.pub.citations = +json["is-referenced-by-count"];
timeout = 1000;
}
i++;
} catch (e) {
console.error("got error, retrying", e);
await setTimeout(timeout);
timeout = timeout * 2;
if (timeout >= 4000) {
i++;
}
}
}
fs.writeFileSync("LINKS.json", JSON.stringify(data, null, 2));
})();