-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
189 lines (173 loc) · 5.1 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
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
// @ts-check
const Rx = require('@reactivex/rxjs')
const fs = require('fs')
const request = require('requestretry')
const progress = require('request-progress')
const chalk = require('chalk')
const Listr = require('listr')
const mangaFox = require('node-mangafox')
const prettyBytes = require('pretty-bytes')
const logSymbols = require('log-symbols')
const gradient = require('gradient-string')
const webp = require('webp-converter')
const dinfo = chalk.green.italic.bold
const { white } = chalk
const {
progressiveBar,
getPageUrl,
getImages,
getFilename,
searchManga,
zipFolder,
getAllChapters,
pad
} = require('./utils')
const log = console.log
process.title = "mangafox-dl"
const dlPage = (url, page, filename, chapterName) => {
const headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0',
'Referer': "",
}
return new Rx.Observable(observer => {
progress(
request({
url,
headers,
maxAttempts: 15,
retryDelay: 5000,
})
)
.on('response', res => {
if (res.statusCode === 200) {
observer.next("Starting...")
}
// "[:bar] :percent :etas | :current of :total bytes"
})
.on('progress', ({percent, size}) => {
observer.next(`${progressiveBar({title: "Downloading", value: percent})} ${prettyBytes(size.transferred)}/${prettyBytes(size.total)}`)
})
.on('error', err => {
log(logSymbols.error, err)
observer.complete()
})
.on('end', () => {
observer.complete()
})
.pipe(fs.createWriteStream(`${chapterName}/${filename}`))
.on('error', err => {
throw new Error(`Pipe Error: ${err}`)
})
})
}
const compress = (input, ouput) => new Promise((resolve, reject) => {
webp.cwebp(input, ouput, '-quiet -q 5', status => {
if (status === '101') reject(status)
else resolve(status)
})
})
const remove = filename => new Promise((resolve, reject) => {
fs.unlink(filename, (err) => {
if (err) reject(err)
resolve()
})
})
const downloadChapter = (manga, ch, volume, url = null) => {
// Array to store urls
let images = []
// create the folder
const chapterName = `${manga}-${ch.toString().padStart(3, "0")}`
if (!fs.existsSync(chapterName)) {
fs.mkdirSync(chapterName)
}
const listrTask = new Listr([
{
title: chalk.whiteBright(`Get imgUrls of ${dinfo(manga)} chapter ${dinfo(ch)}`),
task: () => new Promise((resolve, reject) => {
getImages({
manga,
chapter: ch,
volume
}).then( urls => {
images = urls
resolve()
})
})
},
{
title: chalk.whiteBright("Download images"),
task: () => new Listr(images.map((url, page) => ({
title: chalk.whiteBright(getFilename(page + 1)),
task: () => dlPage(url, page + 1, getFilename(page + 1), chapterName),
})), {concurrent: false, exitOnError: true})
},{
title: chalk.whiteBright("Compress to webp"),
task: () => new Listr(images.map((_, page) => ({
title: `compressing ${(page + 1).toString().padStart(4, '0')}.jpg`,
task: () => compress(
`${chapterName}/${(page + 1).toString().padStart(4, '0')}.jpg`,
`${chapterName}/${(page + 1).toString().padStart(4, '0')}.webp`,
)
})), {concurrent: false, exitOnError: true})
},{
title: chalk.whiteBright("Remove jpg"),
task: () => new Listr(images.map((_, page) => ({
title: `removing ${(page + 1).toString().padStart(4, '0')}.jpg`,
task: () => remove(
`${chapterName}/${(page + 1).toString().padStart(4, '0')}.jpg`,
)
})), {concurrent: false, exitOnError: true})
},{
title: chalk.whiteBright("Zipping chapter"),
task: () => {
zipFolder(chapterName)
.catch(err => log(`${logSymbols} ${err}`))
},
},
])
return listrTask
}
const multiDownload = ({manga, from, volume, to, links = null}) => {
downloadChapter(manga, from, volume, links ? links.shift() : null)
.run().then(() => {
if (from < to) {
multiDownload({manga, from: from + 1, to, links, volume})
}
else {
return
}
}).then(()=> log(gradient.rainbow("---Downloaded!---")))
}
const downloadPage = (manga, ch, page) => {
const chapterName = `${manga}-${pad(ch)}`
let url
const tasks = new Listr([
{
title: white('Get img url'),
task: () => getPageUrl(manga, ch, page).then(uri => {
url = uri
})
},
{
title: white(`Downloading ${dinfo(manga)} chapter ${dinfo(ch)} page ${dinfo(page)}`),
task: () => dlPage(url, page, getFilename(page), chapterName)
}
])
tasks.run().then(_ => log(gradient.rainbow("---Downloaded!---")))
}
const downloadAll = ({manga}) => {
mangaFox.getChapters(manga, n => {
multiDownload({manga, from: 1, to: Number(n)})
})
}
module.exports = {
dlPage,
downloadChapter,
downloadPage,
multiDownload,
downloadAll,
}
// downloadPage("gantz", 174, 1)
// mangaFox.getDetails(35, data => log(data))
// searchManga("gan")
// gantz , id:35