Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(exhentai): re-initiate request for download after fail #186

Merged
merged 1 commit into from
Aug 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"@types/react-dom": "^16.8.5",
"@types/react-lazyload": "^2.5.0",
"@types/react-router-dom": "^4.3.4",
"@types/request-promise": "^4.1.44",
"@types/request": "^2.48.2",
"@types/socket.io": "^2.1.2",
"@types/socket.io-client": "^1.4.32",
"@types/supertest": "^2.0.8",
Expand All @@ -87,7 +87,6 @@
"npm-run-all": "^4.1.5",
"post-compile-webpack-plugin": "^0.1.2",
"react-router-dom": "^5.0.1",
"request-promise": "^4.2.4",
"rimraf": "^3.0.0",
"socket.io": "^2.2.0",
"socket.io-client": "^2.2.0",
Expand Down
67 changes: 40 additions & 27 deletions server/service/ExhentaiService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import puppeteer from 'puppeteer-core';
import fs from 'fs-extra';
import request from 'request-promise';
import request from 'request';
import { success, info, error, trace } from '../utils/log';
import { getTargetResource } from '../utils/resource';
import { ExHentaiInfoItem } from '../controller/ExhentaiController';
Expand Down Expand Up @@ -243,34 +243,47 @@ export default class ExhentaiService {
for (let i = 0; i < imageUrl.length; i++) {
const item = imageUrl[i];
const pageIndex = i + 1;
const targetUrl = joinWithRootPath(`${prefixPath}/${pageIndex}.jpg`);

trace('download begin: ' + item);

await request
.get({ url: item } as {
url: string;
proxy: string;
})
.on('error', (err: any) => {
error(err + ' => ' + item + ': ' + pageIndex);
})
.pipe(
fs
.createWriteStream(
joinWithRootPath(`${prefixPath}/${pageIndex}.jpg`),
)
.on('finish', () => {
counter.push(pageIndex);
success(`${pageIndex}.jpg ${counter.length}/${imageUrl.length}`);

if (counter.length === imageUrl.length) {
success('download completed');
}
})
.on('error', (err: any) =>
error(`${pageIndex}.jpg failed, ${err}`),
),
const handleDownloadStream = async () => {
trace('download begin: ' + item);
const imageStream = fs.createWriteStream(
joinWithRootPath(`${prefixPath}/${pageIndex}.jpg`),
);
const timer = Date.now();
let status = true;
await request(item)
.on('data', () => {
const newTimer = Date.now();
if (
newTimer - timer >= 30 * 1000 &&
fs.existsSync(targetUrl) &&
status
) {
imageStream.close();
trace(`unlink: ${pageIndex}.jpg`);
error('time out at page index: ' + pageIndex);
status = false;
}
})
.pipe(
imageStream.on('finish', () => {
if (status) {
counter.push(pageIndex);
success(
`${pageIndex}.jpg ${counter.length}/${imageUrl.length}`,
);
if (counter.length === imageUrl.length) {
success('download completed');
}
} else {
fs.unlinkSync(targetUrl);
handleDownloadStream();
}
}),
);
};
await handleDownloadStream();
if (isBrowserExist && i % 2 === 0) {
await this.browser.close();
await this.page.waitFor(this.config.waitTime);
Expand Down