-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
148 lines (135 loc) · 3.81 KB
/
helpers.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const axios = require("axios");
const fs = require("fs");
const util = require("util");
const semlog = require("semlog");
const readFile = util.promisify(fs.readFile);
const { log } = semlog;
/**
* Checks if title is longer than 240 bytes required by MediaWiki
* 225 = 240 - 15 for id and extension
* @param {string} title
*/
function isTitleTooLong(title) {
return new util.TextEncoder("utf-8").encode(title).length > 225;
}
function shortenTitle(title) {
const newTitle = `${title.substring(0, title.lastIndexOf(" "))}...`;
return isTitleTooLong(newTitle) ? shortenTitle(newTitle) : newTitle;
}
/**
* Removes chars forbidden in MediaWiki file title
* @param {string} title
*/
function convertToMediaWikiTitle(title) {
const escapedTitle = title
.replace(/[\[\]{}\|#<>%\+\?]/gi, "") // eslint-disable-line no-useless-escape
.replace(/( : |: | = )/g, " - ")
.replace(/:/g, "-");
return isTitleTooLong(escapedTitle)
? shortenTitle(escapedTitle)
: escapedTitle;
}
/**
* Takes data from Polona endpoint and retuens object with data
* @param {string} identifier Polona identifier
*/
async function fetchData(identifier) {
return new Promise(resolve => {
log(`Fetching ${identifier}...`);
axios
.get(`https://polona.pl/api/entities/${identifier}/?format=json`)
.then(response => {
const { data } = response;
const {
id,
academica_id,
slug,
title,
creator_name,
date_descriptive,
country,
imprint,
publisher,
physical_description,
categories,
keywords,
notes,
rights,
digital_copy_by,
main_scan,
links
} = data;
const { academica_url, catalogue_url } = links;
const name = `${convertToMediaWikiTitle(title)} (${academica_id}).jpg`;
const file_url = main_scan.resources.pop().url;
resolve({
file: `${file_url}.jpg`,
name,
file_url,
academica_id,
title,
creator_name,
date_descriptive,
country,
imprint,
publisher,
physical_description,
categories: categories.join(),
keywords: keywords && keywords.join(),
notes,
rights: rights.join(),
digital_copy_by: digital_copy_by.join(),
polona_url: `https://polona.pl/item/${slug},${id}/0/`,
academica_url,
catalogue_url
});
})
.catch(() => {
log("error");
resolve({});
});
});
}
/**
* Reads input file
* @param {string} fileName
*/
async function readInputFile(fileName) {
const data = await readFile(fileName, "utf8");
return data.split(/\n/).filter(value => value);
}
function wikify(data) {
return `=={{int:filedesc}}==
{{Book
|Author = ${data.creator_name || ""}
|Translator =
|Editor =
|Illustrator =
|Title = ${data.title || ""}
|Subtitle =
|Series title =
|Volume =
|Edition =
|Publisher = ${data.publisher || ""}${
data.publisher && data.imprint ? "<br />" : ""
}${data.imprint ? "Imprint: " : ""}${data.imprint}
|Printer =
|Date = ${data.date_descriptive || ""}
|Description = {{pl|${data.physical_description}}}${
data.notes ? "\n\n" : ""
}${
data.notes && data.notes.length
? data.notes.map(note => `{{pl|${note}}}`).join("\n\n")
: ""
}
|Source = https://polona.pl/item/${data.academica_id}/0
|Institution = {{Institution:National Library, Warsaw}} ${
data.digital_copy_by
}
}}
=={{int:license-header}}==
{{PD-old-70}}
{{National Library in Warsaw partnership}}
{{Uncategorized-Polona|year={{subst:CURRENTYEAR}}|month={{subst:CURRENTMONTHNAME}}|day={{subst:CURRENTDAY}}}}`;
}
module.exports = { fetchData, log, readInputFile, wikify };