-
Notifications
You must be signed in to change notification settings - Fork 1
/
arxivGrabber.js
56 lines (48 loc) · 2.01 KB
/
arxivGrabber.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
// This is largely taken from the MIT-licensed https://github.com/chauff/paper-note-filler/blob/master/main.ts
const arXivRestAPI = "https://export.arxiv.org/api/query?id_list=";
function getIdentifierFromUrl(url) {
//if url ends in / remove it
if (url.endsWith("/"))
url = url.slice(0, -1);
return url.split("/").slice(-1)[0];
}
function extractFromArxiv(url) {
let id = getIdentifierFromUrl(url);
return fetch(arXivRestAPI + id)
.then((response) => response.text())
.then(async (data) => {
//parse the XML
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(data, "text/xml");
let title =
xmlDoc.getElementsByTagName("title")[1].textContent;
let abstract =
xmlDoc.getElementsByTagName("summary")[0]
.textContent;
let authors = xmlDoc.getElementsByTagName("author");
let authorString = "";
for (let i = 0; i < authors.length; i++) {
if (i > 0) {
authorString += ", ";
}
authorString +=
authors[i].getElementsByTagName("name")[0]
.textContent;
}
let date =
xmlDoc.getElementsByTagName("published")[0]
.textContent;
if (date) date = date.split("T")[0]; //make the date human-friendly
if (title == null) title = "undefined";
let linkUrls = xmlDoc.getElementsByTagName("link");
let pdfUrl = "";
for (let i = 0; i < linkUrls.length; i++) {
if (linkUrls[i].getAttribute("title") == "pdf") {
pdfUrl = linkUrls[i].getAttribute("href");
if (!pdfUrl.endsWith(".pdf")) pdfUrl = pdfUrl + ".pdf";
}
}
return {title, date, authors, authorString, abstract, id, pdfUrl};
});
}
module.exports = extractFromArxiv;