-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
159 lines (117 loc) · 5.67 KB
/
main.ts
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
149
150
151
152
153
154
155
156
157
158
159
import { DOMParser, Text } from "jsr:@b-fuze/[email protected]";
import ProgressBar from "jsr:@deno-library/[email protected]";
const headers: any = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0",
}
Deno.mkdirSync("./files", { recursive: true });
const username = prompt("Enter your username") || "Anonymous";
const list: string[] = [];
console.log("Generating main page...");
await generateMainPage();
const progress = new ProgressBar({
title: "Downloading:",
total: list.length
})
for(const urlIndex in list) {
try {
await downloadAsHTML(list[urlIndex]);
} catch (_) {}
await progress.render(+urlIndex + 1);
}
try {
await Deno.remove("zlatyfondsme.zim");
} catch (_) {}
console.log("Creating ZIM file...");
new Deno.Command("zimwriterfs", {
args: [
"--welcome", "index.html",
"--illustration", "../logo.png",
"--language", "slk",
"--title", "Zlatý fond denníka SME",
"--description", "Diela zo Zlatého fondu denníka SME",
"--longDescription", "Diela zo Zlatého fondu denníka SME (https://zlatyfond.sme.sk/) pod licenciou CC BY-NC-ND 2.5",
"--creator", "SME.sk",
"--publisher", username,
"--name", "zlatyfondsme",
"--source", "https://zlatyfond.sme.sk/",
"./files",
"zlatyfondsme.zim"
]
}).outputSync()
console.log("Removing failed downloads...");
await removeFailed();
console.log("Done!");
async function downloadAsHTML(url: string) {
const id = url.split("/").at(-1);
try {
await Deno.lstat(`./files/${id}.html`);
} catch (_) {
await fetch(url, { headers });
const response = await fetch("https://zlatyfond.sme.sk/download/html", { headers });
const buffer = await response.arrayBuffer();
const textUTF8 = new TextDecoder("utf8").decode(buffer);
const encoding = textUTF8.match(/(?<=charset=)[^"]+(?=")/)![0].toLowerCase();
const html = new TextDecoder(encoding).decode(buffer);
const dom = new DOMParser().parseFromString(html, "text/html");
dom.querySelectorAll("#spodok, #hlavicka, .toc, .book > .titlepage, .bibliography, meta[name=generator], .mediaobject").forEach(element => element.remove());
dom.querySelector("meta[http-equiv='Content-Type']")?.setAttribute("content", "text/html; charset=utf-8");
[...dom.body.attributes].forEach(attr => dom.body.removeAttribute(attr.name));
dom.body.innerHTML = dom.querySelector("div")!.innerHTML.replace(/<p>\s*<\/p>/g, "");
dom.querySelectorAll(".chapter").forEach(element => {
element.removeAttribute("lang");
if(element.querySelector(".titlepage")) element.querySelector(".titlepage")!.outerHTML = `<h1>${element.querySelector(".titlepage")!.textContent}</h1>`;
})
dom.querySelector("style")!.textContent = `
body {
font-family: Georgia, "Times New Roman", Times, serif;
}
.chapter {
line-height: 1.7em;
margin-bottom: 50px;
}
h1 {
color: navy;
}
`
dom.querySelectorAll(".emphasis").forEach(element => {
element.outerHTML = `<em>${element.textContent}</em>`;
})
dom.querySelectorAll(".bold").forEach(element => {
element.outerHTML = `<strong>${element.textContent}</strong>`;
})
dom.querySelectorAll(".literallayout").forEach(element => {
element.removeAttribute("class");
})
Deno.writeTextFileSync(`./files/${id}.html`, dom.documentElement!.outerHTML);
}
}
async function generateMainPage() {
const response = await fetch("https://zlatyfond.sme.sk/diela", { headers });
headers.Cookie = response.headers.get("Set-Cookie")!;
const html = await response.text();
const dom = new DOMParser().parseFromString(html, "text/html");
const pageDom = new DOMParser().parseFromString("<html><head><meta charset='utf-8'></head><body><main><ul></ul></main><br><br><footer>Všetky diela sú zo <a href='https://zlatyfond.sme.sk/'>Zlatého fondu SME</a> a sú dostupné pod licenciou Creative Commons Attribution-NonCommercial-NoDerivs 2.5 License. Viac informácií na <a href='https://zlatyfond.sme.sk/dokument/autorske-prava'>zlatyfond.sme.sk/dokument/autorske-prava</a>. Zoznam digitalizátorov je dostupný na <a href='https://zlatyfond.sme.sk/digitalizatori'>zlatyfond.sme.sk/digitalizatori</a>.</footer></body></html>", "text/html");
dom.querySelectorAll("#tu-budu-spisovatelia span:not([id])").forEach(element => {
const a = element.querySelector("a")!;
list.push(`https://zlatyfond.sme.sk/${a.getAttribute("href")}`);
const author = (element.lastChild as Text).textContent.trim();
const isAnonymous = author.toLowerCase().includes("anonym") || author.toLowerCase().includes("neznámy");
const li = pageDom.createElement("li");
li.innerHTML = `<a href="${a.getAttribute("href")!.split("/").at(-1)}.html">${a.textContent}</a>${!isAnonymous ? ` - ${author}` : ""}`;
pageDom.querySelector("main")!.querySelector("ul")!.appendChild(li);
})
Deno.writeTextFileSync("./files/index.html", pageDom.documentElement!.outerHTML);
}
async function removeFailed() {
const html = await Deno.readTextFile("./files/index.html");
const dom = new DOMParser().parseFromString(html, "text/html");
dom.querySelectorAll("li").forEach(element => {
const url = `./files/${element.querySelector("a")!.getAttribute("href")}`;
try {
Deno.lstatSync(url);
} catch (_) {
element.remove();
}
})
Deno.writeTextFileSync("./files/index.html", dom.documentElement!.outerHTML);
}