forked from apachecn/doctool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
epub-crawler.js
198 lines (158 loc) · 4.71 KB
/
epub-crawler.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
npm install sync-request
npm install cheerio
npm install gen-epub@git+https://github.com/258ch/gen-epub
npm install iconv-lite
npm install sleep
apt install imagemagick
apt install pngquant
*/
var cheerio = require('cheerio');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
var {requestRetry} = require('./util.js');
var fs = require('fs');
var {URL} = require('url')
var genEpub = require('gen-epub')
var crypto = require('crypto');
var processImg = require('./img.js')
var iconv = require('iconv-lite')
var sleep = require('sleep')
var configFname = process.argv[2] || 'config.json'
var config = JSON.parse(fs.readFileSync(configFname, 'utf-8'))
function main() {
var toc = getTocFromCfg()
var articles = []
var imgs = new Map()
if(config.name) {
articles.push({
title: config.name,
content: `<p>来源:<a href='${config.url}'>${config.url}</a></p>`
})
}
for(var i = 0; i< toc.length; i++) {
try {
var url = toc[i];
console.log('page: ' + url);
if(url.startsWith('http')) {
var html = requestRetry('GET', url, {
headers: config.hdrs,
retry: config.n_retry,
}).body
html = iconv.decode(html, config.encoding)
var res = getArticle(html, url)
res.content = processImg(res.content, imgs, {
pageUrl: url,
imgPrefix: '../Images/'
})
articles.push(res)
}
else
articles.push({title: url, content: ''})
sleep.sleep(config.wait)
} catch(ex) {
console.log(ex);
i--;
}
}
genEpub(articles, imgs)
console.log('Done..');
}
function getTocFromCfg() {
if(config.list && config.list.length > 0)
return config.list;
if (!config.url) {
console.log('请指定 URL')
process.exit()
}
var html = requestRetry('GET', config.url, {
retry: config.n_retry,
headers: config.hdrs,
}).body
html = iconv.decode(html, config.encoding)
var toc = getToc(html);
return toc;
}
function getToc(html) {
var $ = cheerio.load(html);
if(config.remove)
$(config.remove).remove()
var $list = $(config.link);
var vis = new Set()
var res = [];
for(var i = 0; i < $list.length; i++)
{
var $link = $list.eq(i)
var url = $link.attr('href');
if(!url) {
var text = $link.text()
res.push(text)
continue
}
url = url.replace(/#.*$/, '')
if(config.base)
url = new URL(url, config.base).toString()
if(vis.has(url)) continue
vis.add(url)
res.push(url)
}
return res;
}
function getArticle(html, url) {
if(config.processMath)
html = processMath(html)
if(config.processDecl)
html = processDecl(html)
var $ = cheerio.load(html);
if(config.remove)
$(config.remove).remove()
// 只取一个元素
var $title = $(config.title).eq(0)
var title = $title.text().trim()
$title.remove()
// 解决 Cheerio 的 .html 多播问题
var $co = $(config.content)
var co = []
for(var i = 0; i < $co.length; i++)
co.push($co.eq(i).html())
co = co.join('\r\n')
if(config.credit) {
var credit = `<blockquote>原文:<a href='${url}'>${url}</a></blockquote>`
co = credit + co
}
return {title: title, content: co}
}
////////////////////////////////////////////////////
// for sphinx docs
function processDecl(html) {
var $ = cheerio.load(html)
$('colgroup').remove()
var $dts = $('dt')
for(var i = 0; i < $dts.length; i++) {
var $dt = $dts.eq(i)
$dt.find('a.reference').remove()
var t = $dt.text()
var $pre = $('<pre></pre>')
$pre.text(t)
$dt.replaceWith($pre)
}
return $.html()
}
function processMath(html){
var $ = cheerio.load(html)
var $maths = $('span.math, div.math');
for(var i = 0; i < $maths.length; i++) {
var $m = $maths.eq(i)
var tex = $m.text().replace(/\\\[|\\\]|\\\(|\\\)/g, '')
var url = 'http://latex.codecogs.com/gif.latex?' + encodeURIComponent(tex)
var $img = $('<img />')
$img.attr('src', url)
if($m.is('div')) {
var $p = $('<p></p>')
$p.append($img)
$img = $p
}
$m.replaceWith($img)
}
return $.html()
}
if(module == require.main) main()