-
Notifications
You must be signed in to change notification settings - Fork 0
/
books.mjs
161 lines (136 loc) · 4.37 KB
/
books.mjs
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
160
161
/**
* Copyright (C) 2024 Louka Ménard Blondin
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {
existsSync as exists,
createWriteStream,
promises as fs,
} from "node:fs"
import { Readable } from "node:stream"
import { finished } from "node:stream/promises"
import path from "node:path"
import { parse } from "node-html-parser"
const baseUrl = "https://www.alislam.org"
// Ruse to prevent rate limitation.
const debounce = true
const debouceTime = 500
const blacklist = [
"Other Authors",
"Selected Audiobooks",
"eBooks on Apple, Google and Kindle",
"Other Languages",
]
async function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
async function processLibrary(outPath, href, authorId) {
const libraryPage = await (await fetch(href)).text()
const libraryHtml = parse(libraryPage)
const books = []
const bookTags = libraryHtml.querySelectorAll(".LibraryBookDiv")
for (const bookTag of bookTags) {
const bookLink = bookTag.querySelector(".LibraryBookTitle a")
const bookName = bookLink.innerText
const bookId = `${authorId
.split("-")
.map((n) => n[0])
.join("")}-${bookLink.attributes.href.split("/")[2]}`
const bookHref = `${baseUrl}${bookLink.attributes.href}`
if (debounce) {
await sleep(debouceTime)
}
// Get book information.
const bookInfoPage = await (await fetch(bookHref)).text()
const bookInfoHtml = parse(bookInfoPage)
const bookDescription = bookInfoHtml.querySelector(
'span[itemprop="description"]'
)?.innerText
const bookPdfLink = bookInfoHtml.querySelector('a[title="Download PDF"]')
?.attributes.href
if (debounce) {
await sleep(debouceTime)
}
// Download book.
let bookPath
if (bookPdfLink === undefined) {
console.log(`Couldn't download ${bookId}. PDF link not found.`)
} else {
bookPath = path.join(outPath, `${bookId}.pdf`)
console.log(`Downloading "${bookId}" into ${bookPath}`)
const stream = createWriteStream(bookPath)
const { body } = await fetch(`${baseUrl}${bookPdfLink}`)
await finished(Readable.fromWeb(body).pipe(stream))
stream.close()
}
// Push book.
books.push({
id: bookId,
name: bookName,
href: bookHref,
description: bookDescription,
path: bookPath,
uri_pdf: `${baseUrl}${bookPdfLink}`,
})
}
return books
}
async function processAuthor(tagOfAuthor) {
// Retrieve basic information.
const nameTag = tagOfAuthor.querySelector(".AuthorNameDiv a")
const name = nameTag.innerText
// Check against blacklist.
if (blacklist.includes(name)) {
throw new Error(`Author "${name}" blacklisted.`)
}
// Get id of author and make directory.
const authorId = nameTag.attributes.href.split("/")[2]
const authorDir = `./library/${authorId}`
await fs.mkdir(authorDir)
// Retrieve & parse the library.
const booksDir = path.join(authorDir, "books")
await fs.mkdir(booksDir)
const libraryHref = `${baseUrl}${nameTag.attributes.href}`
const books = await processLibrary(booksDir, libraryHref, authorId)
return {
id: authorId,
name: name,
uri_library: libraryHref,
books,
}
}
if (exists("./library")) {
await fs.rm("./library", { recursive: true })
}
await fs.mkdir("./library")
const booksPage = await (await fetch(`${baseUrl}/books/`)).text()
const booksHtml = parse(booksPage)
const authors = []
const authorTags = booksHtml.querySelectorAll(".AuthorDiv")
for (const authorTag of authorTags) {
try {
authors.push(await processAuthor(authorTag))
if (debounce) {
await sleep(debouceTime)
}
} catch (e) {
console.log(`${e}`)
}
}
await fs.writeFile(
path.join("./library", "authors.json"),
JSON.stringify(authors, undefined, 4),
"utf-8"
)