-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinspire-extract-metadata.js
More file actions
79 lines (67 loc) · 2.07 KB
/
inspire-extract-metadata.js
File metadata and controls
79 lines (67 loc) · 2.07 KB
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
const fs = require("fs");
function print_variable(var_name, val) {
val = val.replace(/'/g, '\'\\\'\'');
console.log(`${var_name}='${val}'`);
}
if (process.argv.length < 2) {
console.error("usage: node inspire-extract-metadata.js <search.json>");
process.exit(2);
}
const input_filename = process.argv[2];
const json = JSON.parse(fs.readFileSync(input_filename, "utf8"));
if (!json.hits || !json.hits.hits || !json.hits.hits[0]) {
console.error("no item is found");
process.exit(1);
} else if (json.hits.hits.length != 1) {
console.error("Query is ambiguous. Multiple articles are found.");
process.exit(1);
}
const hit = json.hits.hits[0];
if (!hit.id) {
console.error("FATAL hit.id is not found");
process.exit(3);
}
if (!hit.metadata) {
console.error("FATAL hit.metadata is not found");
process.exit(3);
}
const metadata = hit.metadata;
// print inspirehep literature id
print_variable('inspire', hit.id);
// texkey
if (metadata.texkeys && metadata.texkeys[0]) {
const texkey = metadata.texkeys[0];
print_variable('texkey', texkey);
}
// url: the URL to the full text
if (metadata.arxiv_eprints && metadata.arxiv_eprints[0]) {
const arxiv_number = metadata.arxiv_eprints[0].value;
print_variable('url', `https://arxiv.org/pdf/${arxiv_number}`);
print_variable('arxiv', arxiv_number);
} else if (metadata.documents) {
const fulls = metadata.documents.filter(e => e.fulltext);
if (fulls[0])
print_variable('url', fulls[0].url);
}
// title
if (metadata.titles && metadata.titles[0]) {
print_variable('title', metadata.titles[0].title);
}
// authors
if (metadata.authors && metadata.authors.length) {
const authors = [];
metadata.authors.forEach(author => {
if (author.full_name) {
var full_name = author.full_name;
const m = full_name.match(/^(.*), (.*)$/);
if (m) full_name = `${m[2]} ${m[1]}`;
authors.push(full_name);
}
});
if (authors.length)
print_variable('authors', authors.join(', '));
}
// abstract
if (metadata.abstracts && metadata.abstracts[0]) {
print_variable('abstract', metadata.abstracts[0].value);
}