/
manga-services.js
219 lines (191 loc) · 5.85 KB
/
manga-services.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
import fs from "fs";
import mkdirp from "mkdirp";
import http from "http";
import https from "https";
import rimraf from "rimraf";
import fetch from "fetch-retry";
import {
addDownloadedChapter,
setDownloadMangaStatus
} from "../actions/list_actions";
import { store } from "../store";
import { DownloadStatus } from "../consts/download-status";
import CompressionService from "./compression-services";
import logo from "../../res/images/logo.png";
const { shell } = require("electron").remote.require("electron");
const SERVER_URL = "http://localhost:55235";
const RETRY_OPTIONS = { retries: 5, retryDelay: 3000 };
const searchManga = async title => {
const mangas = await fetch(`${SERVER_URL}/manga?title=${title}`);
return await mangas.json();
};
const getManga = async location => {
const l = encryptParam(location);
const manga = await fetch(`${SERVER_URL}/manga/${l}`);
return await manga.json();
};
const getChapters = async location => {
const l = encryptParam(location);
const chapters = await fetch(`${SERVER_URL}/chapter/${l}`, RETRY_OPTIONS);
return await chapters.json();
};
const getPages = async location => {
const pages = await fetch(`${SERVER_URL}/page/${location}`, RETRY_OPTIONS);
return await pages.json();
};
const updateMangaProviders = async () => {
return await fetch(`${SERVER_URL}/provider`, {
method: "POST",
body: {}
});
};
const getSourceFromLocation = async location => {
const l = encryptParam(location);
const source = await fetch(`${SERVER_URL}/source/${l}`);
return await source.json();
};
const download = async (
id,
location,
compressToCbz,
title,
chapters,
startingChapter = 0
) => {
// create the manga folder
const baseFolder = `${location}/${title}`;
mkdirp.sync(baseFolder);
// iterate the chapters list
const slicedChapters = chapters.slice(startingChapter);
for (const chapter of slicedChapters) {
// create the folder for the chapter
const chapterPrefix = chapter.index.toString().padStart(4, "0");
const chapterFolder = `${baseFolder}/${chapterPrefix} - ${chapter.title}`;
mkdirp.sync(chapterFolder);
// get the pages for the chapter
let pages = [];
try {
pages = await getPages(encryptParam(chapter.location));
} catch (err) {
updateError(id);
throw new Error(
"There was an issue retrieving the manga pages. Kindly retry the download. If the problem persist, report to the dev."
);
}
// download each pages
for (const page of pages) {
await checkIfPaused(id);
const pagePrefix = page.index.toString().padStart(4, "0");
const pageLocation = `${chapterFolder}/${pagePrefix}.jpg`;
try{
await downloadImage(page.image, pageLocation);
} catch (err) {
updateError(id);
throw new Error(
`There was an issue retrieving the specific manga page ${page.image}. Kindly retry the download. If the problem persist, report to the dev.`
);
}
}
// compress folder
if (compressToCbz) {
await CompressionService.compressChapter(chapterFolder, true);
}
// update status
updateProgress(id, chapter);
}
updateFinished(id);
await notifyUserFinished(id, location);
return { status: "success" };
};
const downloadImage = async (url, location) => {
return await new Promise((resolve, reject) => {
try {
// check url, if https use https module, if http use http module
const retrieverProtocol = url.split(":")[0] == "https" ? https : http;
retrieverProtocol.get(url, res => {
if (res.statusCode === 200) {
const fileStream = fs.createWriteStream(location);
res.pipe(fileStream);
res.on("error", err => {
console.log("error downloadImage: ", err);
reject(err);
});
res.on("timeout", err => {
console.log("timeout downloadImage: ", err);
reject(err);
});
fileStream.on("finish", function() {
resolve();
});
} else {
console.log("error downloadImage outside statusCode 200", res);
reject();
}
});
} catch (err) {
console.log("error on downloadImage httpGet", err);
reject(err);
}
});
};
const checkIfPaused = id => {
return new Promise((resolve, reject) => {
// check if manga state is paused
const checker = () => {
const downloadedMangas = store.getState().downloadedMangas;
const manga = _.find(downloadedMangas, m => m.info.location == id);
if (!manga) {
return;
}
if (
manga.status == DownloadStatus.PAUSED ||
manga.status == DownloadStatus.ERROR
) {
console.log(`${manga.info.title} is paused or error occured`);
setTimeout(() => {
checker();
}, 1000);
} else if (manga.status == DownloadStatus.ONGOING) {
resolve();
}
};
checker();
});
};
const updateProgress = (id, chapter) => {
store.dispatch(addDownloadedChapter(id, chapter));
};
const updateFinished = id => {
store.dispatch(setDownloadMangaStatus(id, DownloadStatus.DOWNLOADED));
};
const updateError = id => {
store.dispatch(setDownloadMangaStatus(id, DownloadStatus.ERROR));
};
const notifyUserFinished = async (id, location) => {
const manga = await getManga(id);
const notification = new Notification("Manga download complete", {
body: manga.title,
icon: manga.cover
});
notification.onclick = evt => {
shell.showItemInFolder(`${location}/${manga.title}`);
};
};
const deleteDownloadedManga = location => {
rimraf(location, () => {
console.log(`Deleted ${location}`);
});
};
const encryptParam = param => {
return btoa(param);
};
const MangaService = {
searchManga,
getManga,
getChapters,
updateMangaProviders,
getSourceFromLocation,
download,
deleteDownloadedManga
};
export default MangaService;