Skip to content

Commit

Permalink
feat(exhentai): check missing img of download failed (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
orzyyyy committed Sep 24, 2019
1 parent d981532 commit 610bb02
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
30 changes: 8 additions & 22 deletions server/controller/ExhentaiController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import ExhentaiService from '../service/ExhentaiService';
import {
getLatestListInfo,
getLatestListFileName,
getBaseNameOfImage,
getListFiles,
getEmptyRestDetailUrlInfo,
getLatestDownloadDirName,
getMissedImgInfo,
} from '../utils/exhentai';
import path from 'path';
import { Context } from 'koa';
Expand Down Expand Up @@ -87,27 +88,12 @@ export default class ExhentaiController {
service.downloadImages(detailImageUrls, prefixPath);
}

@Request({ url: '/download/stat', method: 'get' })
async statisticsFailedDownloadImgUrls(ctx: Context) {
const { name, dateStamp } = ctx.query;
const prefixPath = `exhentai/${dateStamp}/${name}`;
const detailImageUrls: string[] = readJsonFile(
joinWithRootPath(`${prefixPath}/detailImageUrls.json`),
);
const indexImageUrls: string[] = readJsonFile(
joinWithRootPath(`${prefixPath}/restDetailUrls.json`),
);
const imgUrls: string[] = getBaseNameOfImage(prefixPath);
const result: { detail: string; index: string; i: number }[] = [];
for (let i = 1; i < indexImageUrls.length + 1; i++) {
if (!imgUrls.includes(i.toString())) {
result.push({
detail: detailImageUrls[i - 1],
i,
index: indexImageUrls[i - 1],
});
}
}
@Request({ url: '/download/status', method: 'get' })
async checkMissedDownloadImgs(ctx: Context) {
const { dateStamp } = ctx.query;
const latestDirName = getLatestDownloadDirName(dateStamp);
const latestDirPath = joinWithRootPath(`exhentai/${latestDirName}`);
const result = getMissedImgInfo(latestDirPath);
return result;
}

Expand Down
42 changes: 38 additions & 4 deletions server/utils/exhentai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const getLatestListFileName = () => {
const result = infoFiles
.filter((item: string) => item !== '.gitkeep')
.map((item: string) => parseInt(item, 10))
.sort((a: any, b: any) => b - a);
.sort((a: number, b: number) => b - a);
if (result.length > 0) {
return result[0].toString();
}
Expand All @@ -35,16 +35,50 @@ export const getListFiles = (): string[] => {
return [];
};

export const getLatestDownloadDirName = (dateStamp?: string) => {
let result = dateStamp;
if (!dateStamp) {
const downloadDir = fs
.readdirSync(joinWithRootPath(downloadPath))
.filter((item: string) => item !== '.gitkeep');
result = downloadDir[downloadDir.length - 1];
}
return result;
};

export const getMissedImgInfo = (latestDirPath: string) => {
const result: { detail: string; index: string; i: number }[] = [];
fs.readdirSync(latestDirPath).map(item => {
const prefix = `${latestDirPath}/${item}`;
const files = fs
.readdirSync(prefix)
.map(f => parseInt(f.replace(/[.jpg|.png]/g, '')))
.filter(item => item)
.sort((a: number, b: number) => a - b)
.map(item => item.toString());
const detail: string[] = readJsonFile(`${prefix}/detailImageUrls.json`);
const rest: string[] = readJsonFile(`${prefix}/restDetailUrls.json`);

for (let i = 1; i < rest.length + 1; i++) {
if (!files.includes(i.toString())) {
result.push({
detail: detail[i - 1],
i,
index: rest[i - 1],
});
}
}
});
return result;
};

export const getLatestListInfo = () => {
const tempPath = getLatestListFileName() || '';
const newestListFilePath = joinWithRootPath(listInfoPath + tempPath);
const result = readJsonFile(newestListFilePath + '.json');
return result && result[0];
};

export const getBaseNameOfImage = (dir: string) =>
fs.readdirSync(joinWithRootPath(dir)).map(f => f.replace(/[.jpg|.png]/g, ''));

export const getEmptyRestDetailUrlInfo = () =>
glob
.sync(joinWithRootPath(downloadPath) + '/**/restDetailUrls.json')
Expand Down

0 comments on commit 610bb02

Please sign in to comment.