-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·118 lines (101 loc) · 3.07 KB
/
index.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
#!/usr/bin/env node
'use strict';
const program = require('commander');
const Xray = require('x-ray');
const Download = require('download');
const x = new Xray();
program
.version('0.1.0')
.option('-l, --list', 'output full manga list information (title, url)', getFullMangaList)
.option('-r, --releases', 'output latest releases information (title, latest chapter, url)', getLatestReleaseList)
.option('-d, --download <url>', 'download all available chapters from given url (e.g. http://mangastream.com/manga/toriko)')
.parse(process.argv);
if (program.download) getChaptersFrom(program.download);
process.on('uncaughtException', (err) => {
console.log(err);
});
function getFullMangaList() {
let manga_list_url = 'http://mangastream.com/manga';
x(manga_list_url, '.main-body .table strong a',
[{
title: '',
url: '@href'
}]
)((err, results) => {
if (err) throw err;
console.log(results);
});
}
function getLatestReleaseList() {
let manga_list_url = 'http://mangastream.com/manga';
x(manga_list_url, '.main-body .table tr',
[{
title: 'strong a',
latest: 'a.chapter-link',
url: '.chapter-link@href'
}]
)((err, results) => {
if (err) throw err;
console.log(results);
});
}
function getChaptersFrom(url) {
const getChapters = new Promise((resolve, reject) => {
x(url, '.main-body', {
title: 'h1',
chapters: x('.table a', [{
name: '',
url: '@href'
}])
})((err, result) => {
if (err) {
reject(new Error(err));
} else {
resolve(result);
}
});
});
getChapters.then((results) => {
console.log('Downloading...');
downloadChapters(results);
});
}
function downloadChapters(chapter_list) {
chapter_list.chapters.forEach((chapter) => {
let url = chapter.url;
let length = 0;
const getLength = new Promise((resolve, reject) => {
x(url, '.main-body .btn-group:last-child .dropdown-menu li:last-child a')((err, result) => {
if (err) {
reject(new Error(err));
} else {
let regExp = /\(([^)]+)\)/g;
let matches = regExp.exec(result);
length = matches[1];
resolve(result);
}
});
});
getLength.then((result) => {
// remove last digit from http://readms.com/r/platinum_end/001/3013/1
let sliced_url = url.slice(0, -1);
for (let i = 1; i <= length; i++) {
x(sliced_url + i, 'img#manga-page@src')((err, image) => {
return new Promise((resolve, reject) => {
if (err) reject(new Error(err));
let download = new Download();
let filename = i + '.png';
if (i < 10) {
filename = '0'.concat(i, '.png');
}
download.get(image);
download.rename(filename);
download.dest('./downloads/' + chapter_list.title + '/Chapter ' + chapter.name);
console.log(chapter_list.title + ' - Chapter ' + chapter.name + ' - Page ' + i);
resolve(download.run());
});
});
}
});
});
}