forked from apachecn/doctool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightnovel.js
185 lines (147 loc) · 5.75 KB
/
lightnovel.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
var request = require('sync-request')
var cheerio = require('cheerio')
var iconv = require('iconv-lite')
var genEpub = require('gen-epub')
var fs = require('fs')
var chp = require('child_process')
var {URL} = require('url')
var dtMap = loadDtMap()
var cookie = fs.readFileSync('COOKIE', 'utf-8')
function sleep(s) {
chp.spawnSync('sleep', [s])
}
function loadDtMap() {
if(!fs.existsSync('dt.txt'))
return {}
var lines = fs.readFileSync('dt.txt', 'utf-8')
.split('\n')
.map(x => x.trim())
.filter(x => x)
var dtMap = {}
lines.map(x => x.split(' '))
.filter(x => x.length >= 2)
.forEach(x => dtMap[x[0]] = x[1])
return dtMap
}
function fnameEscape(name){
return name.replace(/\\/g, '\')
.replace(/\//g, '/')
.replace(/:/g, ':')
.replace(/\*/g, '*')
.replace(/\?/g, '?')
.replace(/"/g, '"')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/\|/g, '|')
}
function requestRetry(method, url, options={}) {
var retry = options.retry || 5
for(var i = 0; i < retry; i++) {
try {
return request(method, url, options)
} catch(ex) {
if(i == retry - 1) throw ex;
}
}
}
function formatText(text) {
return text.replace(/(\r\n)+/g, '\r\n') // 多个换行变为一个
.replace(/^.+?\r\n.+?\r\n/, '') // 去掉前两行
.replace(/\r\n.+?\r\n.+?$/, '') // 去掉后两行
.replace(/^(.+?)$/gm, s => { // 划分标题和段落
if(s.startsWith(' '))
return '<p>' + s.slice(4) + '</p>'
else
return '<!--split--><h1>' + s + '</h1>'
})
.split('<!--split-->') // 拆分章节
.filter(x => x) // 过滤空白章节
.map(x => {
// 将章节拆分为标题和内容
var title = /<h1>(.+?)<\/h1>/.exec(x)[1]
var co = x.replace(/<h1>.+?<\/h1>/, '')
return {title: title, content: co}
})
}
function getInfo(html) {
var $ = cheerio.load(html)
var dt = $('#content > div:nth-child(1) > table:nth-child(1) tr:nth-child(2) > td:nth-child(4)').text().slice(5).replace(/-/g, '')
var url = $('#content > div:nth-child(1) > div:nth-child(6) > div > span:nth-child(1) > fieldset > div > a').attr('href')
var title = $('#content > div:nth-child(1) > table:nth-child(1) tr:nth-child(1) > td > table tr > td:nth-child(1) > span > b').text()
var author = $('#content > div:nth-child(1) > table:nth-child(1) tr:nth-child(2) > td:nth-child(2)').text().slice(5)
return {dt: dt, url: url, title: fnameEscape(title), author: fnameEscape(author)}
}
function download(id) {
if(id == null) {
console.log('invalid id')
return
}
var url = `https://www.wenku8.net/book/${id}.htm`
var html = iconv.decode(requestRetry('GET', url).body, 'gbk')
var info = getInfo(html)
var dt = info.dt || dtMap[id] || 'UNKNOWN';
console.log(info.title, info.author, dt)
try {fs.mkdirSync('out')} catch(ex) {}
var path = `out/${info.title} - ${info.author} - ${dt}.epub`
if(fs.existsSync(path)) {
console.log(`文件已存在`)
return
}
var articles = []
articles.push({title: info.title, content: `<p>作者:${info.author}</p>`})
url = `http://dl.wenku8.com/down.php?type=utf8&id=${id}`
var text = requestRetry('GET', url).body.toString()
var chs = formatText(text)
articles = articles.concat(chs)
genEpub(articles, new Map(), null, path)
}
function getToc(html) {
var $ = cheerio.load(html)
var $links = $('table.grid b a')
var $dts = $('table.grid div > div:nth-child(2) > p:nth-child(3)')
var res = []
for(var j = 0; j < $links.length; j++) {
var id = /\/(\d+)\.htm/.exec($links.eq(j).attr('href'))[1]
var dt = $dts.eq(j).text().slice(5).replace(/-/g, '')
res.push({id: id, dt: dt})
}
return res
}
function fetch(fname, st, ed, withDt=false) {
var ofile = fs.openSync(fname, 'a')
outer:
for(var i = 1; ; i++) {
console.log(i)
var url = `https://www.wenku8.net/modules/article/index.php?page=${i}`
var html = iconv.decode(requestRetry('GET', url, {headers: {Cookie: cookie}}).body, 'gbk')
var toc = getToc(html)
if(toc.length == 0) break;
for(var bk of toc) {
if(ed && bk.dt > ed)
continue;
if(st && bk.dt < st)
break outer;
console.log(bk.id, bk.dt)
if(withDt)
fs.writeSync(ofile, `${bk.id} ${bk.dt}`)
else
fs.writeSync(ofile, bk.id)
fs.writeSync(ofile, '\r\n')
}
}
fs.closeSync(ofile)
}
function batch(fname) {
var li = fs.readFileSync(fname, 'utf-8')
.split('\n').map(x => x.trim()).filter(x => x)
for(var id of li) download(id.split(' ')[0])
}
function main() {
var cmd = process.argv[2]
var arg = process.argv[3]
if(cmd == 'fetch') fetch(arg, process.argv[4], process.argv[5])
else if(cmd == 'batch') batch(arg)
else if(cmd == 'dl') download(arg)
else if(cmd == 'fetchdt') fetch(arg, process.argv[4], process.argv[5], true)
}
if(require.main == module) main()