-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathjianhelper.js
274 lines (234 loc) · 7.57 KB
/
jianhelper.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
var cheerio = require('cheerio');
var request = require('sync-request');
var fs = require('fs');
var process = require('process');
var ejs = require('ejs');
var jszip = require('jszip');
var types = {
unknown: 0,
user: 1,
notebook: 2,
collection: 3
}
var selectors = {
article: '.title a',
title: 'h1.title',
author: 'a.author-name span',
content: '.show-content'
};
var url = process.argv[2];
if(!url)
{
showUsage();
process.exit(0);
}
var startPage = process.argv[3];
startPage = startPage? Number.parseInt(startPage): 1;
var endPage = process.argv[4];
endPage = endPage? Number.parseInt(endPage): Number.POSITIVE_INFINITY;
var urlResult = checkUrl(url);
if(urlResult.type == types.unknown)
{
showUsage();
process.exit(0);
}
var url = getRealUrl(urlResult.type, urlResult.id, url);
var toc = [];
console.log('Init path...');
initPath();
for(var i = startPage; i <= endPage; i++)
{
console.log('page: ' + i.toString())
var pageUrl = url.replace(/\{page\}/, i.toString());
var html;
try {
html = request('GET', pageUrl).getBody().toString();
} catch(ex) { break; }
var li = getList(html);
if(li.length == 0)
break;
for(var j in li)
{
try {
var artUrl = li[j];
var fname = /\/p\/(\w{12})/.exec(artUrl)[1] + '.html';
var html = request('GET', artUrl).getBody().toString();
var co = getContent(html);
fs.writeFileSync('./out/OEBPS/Text/' + fname, co, {encoding: 'utf-8'});
var title = /<h1>(.+?)<\/h1>/.exec(co)[1];
toc.push({file: fname, title: title});
console.log('article: ' + fname + ', title: ' + title);
} catch(ex) { console.log(ex.toString()); }
}
}
var uuidGenerator = require('./uuid.js');
var uuid = uuidGenerator.uuid();
console.log('Generate content.opf...')
var contentOpf = getContentOpf(toc, uuid);
fs.writeFileSync('./out/OEBPS/content.opf', contentOpf, {encoding: 'utf-8'})
console.log('Generate toc.ncx...')
var tocNcx = getTocNcx(toc, uuid);
fs.writeFileSync('./out/OEBPS/toc.ncx', tocNcx, {encoding: 'utf-8'})
console.log('Gnerate epub...');
generate();
console.log('Done..');
function showUsage() {
var usage = "用法:node jianhelper url [start [end]]\n\n" +
" url:支持三种类型\n\n" +
" http://www.jianshu.com/users/{id} 用户\n" +
" http://www.jianshu.com/notebooks/{id} 文集\n" +
" http://www.jianshu.com/collection/{id} 专题\n\n" +
" start:起始页,默认为第一页\n\n" +
" end:终止页,默认为最后一页";
console.log(usage);
}
function checkUrl(url)
{
var regexes = {
user: /^https?:\/\/www\.jianshu\.com\/users\/(\w{6,12})\/?$/,
notebook: /^https?:\/\/www\.jianshu\.com\/notebooks\/(\d+)\/?$/,
collection: /^https?:\/\/www\.jianshu\.com\/collection\/(\w{6,12})\/?$/
};
var type = types.unknown;
var id;
for(var k in regexes)
{
var rms;
if(rms = regexes[k].exec(url))
{
type = types[k];
id = rms[1];
break;
}
}
return {type: type, id: id};
}
function getRealUrl(type, id, url)
{
if(type == types.user)
{
return 'http://www.jianshu.com/users/' + id +
'/latest_articles?page={page}';
}
else if(type == types.notebook)
{
return 'http://www.jianshu.com/notebooks/' + id +
'/latest?page={page}';
}
else if(type == types.collection)
{
var content = request('GET', url).getBody().toString();;
var realId = /href="\/collections\/(\d+)\//.exec(content)[1];
return 'http://www.jianshu.com/collections/' + realId +
'/notes?order_by=added_at&page={page}';
}
}
function getList(html) {
var $ = cheerio.load(html);
var $list = $(selectors.article);
var res = [];
for(var i = 0; i < $list.length; i++)
{
var url = $list.eq(i).attr('href');
if(!url.startsWith('http'))
url = 'http://www.jianshu.com' + url;
res.push(url);
}
return res;
}
function getContent(html) {
var $ = cheerio.load(html);
dealWithImg($);
var header = '<html>\r\n<head>\r\n<title></title>\r\n' +
'<link href="../Styles/Style.css" type="text/css" rel="stylesheet"/>' +
'</head>\r\n<body>\r\n';
var title = '<h1>' + $(selectors.title).text() + '</h1>';
var author = '<p>作者:' + $(selectors.author).text() + '</p>';
var content = $(selectors.content).html();
var footer = '\r\n</body>\r\n</html>';
return header + title + '\n' + author + '\n' + content + footer;
}
function initPath()
{
if(fs.existsSync('./out') &&
fs.statSync('./out').isDirectory())
rRmDir('./out');
fs.mkdirSync('./out');
fs.mkdirSync('./out/OEBPS');
fs.mkdirSync('./out/OEBPS/Text');
fs.mkdirSync('./out/OEBPS/Images');
//try {fs.mkdirSync('./out/OEBPS/Styles');} catch(ex) {}
//try {fs.mkdirSync('./out/META-INF');} catch(ex) {}
//fs.writeFileSync('./out/META-INF/container.xml', fs.readFileSync('./assets/container.xml'));
//fs.writeFileSync('./out/mimetype', fs.readFileSync('./assets/mimetype'));
//fs.writeFileSync('./out/OEBPS/Styles/Style.css', fs.readFileSync('./assets/Style.css'));
}
function dealWithImg($)
{
var imgs = $(selectors.content + ' img');
for(var i = 0; i < imgs.length; i++)
{
try {
var img = imgs.eq(i);
var url = img.attr('src');
var co = request('GET', url).getBody();
var fname = /[\w\-]+\.(?:jpg|png|gif)/.exec(url)[0];
console.log('img: ' + fname);
fs.writeFileSync('./out/OEBPS/Images/' + fname, co);
img.attr('src', '../Images/' + fname);
} catch(ex) { console.log(ex.toString()); }
}
}
function getContentOpf(toc, uuid)
{
var date = new Date();
var dateStr
= date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
return ejs.render(fs.readFileSync('assets/content.ejs', 'utf-8'), {
date: dateStr,
toc: toc,
uuid: uuid
});
}
function getTocNcx(toc, uuid)
{
return ejs.render(fs.readFileSync('assets/toc.ejs', 'utf-8'), {
toc: toc,
uuid: uuid
});
}
function generate()
{
var zip = new jszip();
zip.file('mimetype', fs.readFileSync('./assets/mimetype'));
zip.file('META-INF/container.xml', fs.readFileSync('./assets/container.xml'));
zip.file('OEBPS/Styles/Style.css', fs.readFileSync('./assets/Style.css'));
zip.file('OEBPS/content.opf', fs.readFileSync('./out/OEBPS/content.opf'));
zip.file('OEBPS/toc.ncx', fs.readFileSync('./out/OEBPS/toc.ncx'));
var articles = fs.readdirSync('./out/OEBPS/Text');
for(var i = 0; i < articles.length; i++)
{
var fname = articles[i];
zip.file('OEBPS/Text/' + fname, fs.readFileSync('./out/OEBPS/Text/' + fname));
}
var images = fs.readdirSync('./out/OEBPS/Images');
for(var i = 0; i < images.length; i++)
{
var fname = images[i];
zip.file('OEBPS/Images/' + fname, fs.readFileSync('./out/OEBPS/Images/' + fname));
}
fs.writeFileSync('out.epub', zip.generate({type: 'nodebuffer', 'compression':'DEFLATE'}));
}
function rRmDir(dir)
{
var flist = fs.readdirSync(dir);
for(var i = 0; i < flist.length; i++)
{
var item = dir + '/' + flist[i];
if(fs.statSync(item).isDirectory())
rRmDir(item);
else
fs.unlinkSync(item);
}
fs.rmdirSync(dir);
}