Skip to content

Commit

Permalink
feat(server): output log info
Browse files Browse the repository at this point in the history
  • Loading branch information
orzyyyy committed Jun 13, 2019
1 parent bc89fa0 commit c635a56
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@types/react-dom": "^16.8.4",
"@types/react-router-dom": "^4.3.3",
"blueimp-md5": "^2.10.0",
"chalk": "^2.4.2",
"date-fns": "^2.0.0-alpha.32",
"gh-pages": "^2.0.1",
"koa-bodyparser": "^4.2.1",
Expand Down
22 changes: 21 additions & 1 deletion server/controller/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@ const path = require('path');
const puppeteer = require('puppeteer-core');
const { format } = require('date-fns');
const toml = require('toml');
const chalk = require('chalk');

const dateFormat = 'yyyy-MM-dd HH:mm:ss';
const getTimestamp = () => `[${format(new Date(), dateFormat)}] `;
const success = (text: string) =>
// tslint:disable-next-line: no-console
console.log(chalk.greenBright(getTimestamp() + '✅ ' + text));
const error = (text: string) =>
// tslint:disable-next-line: no-console
console.log(chalk.red(getTimestamp() + text));
// tslint:disable-next-line: no-console
// const info = (text: string) => console.log(chalk.yellowBright(text));
const trace = (text: string) =>
// tslint:disable-next-line: no-console
console.log(chalk.cyanBright(getTimestamp() + text));

const { exHentai: exHentaiCookie } = toml.parse(
fs.readFileSync(path.join(__dirname, '../resource/cookie.toml'), 'utf-8'),
Expand Down Expand Up @@ -73,24 +88,29 @@ const getExhentai = async (ctx: any) => {
args: exHentai.lauchArgs,
devtools: exHentai.devtools,
});
success('launch puppeteer');
const page = await browser.newPage();

setExHentaiCookie(page);
success('set cookie');
let results: ExHentaiInfoItem[] = [];
for (let i = 0; i < exHentai.maxPageIndex; i++) {
const result = await getExHentaiInfo({ pageIndex: i, page });
results = [...results, ...result];
trace(`fetching pageIndex => ${i + 1}`);
await page.waitFor(exHentai.waitTime);
}
await browser.close();

trace('write into json');
const createTime = format(new Date(), 'yyyyMMddHHmmss');
fs.outputJSON(
path.join(process.cwd(), `src/assets/exhentai/${createTime}.json`),
results,
).catch((err: any) => {
console.error(err);
error('write into json' + err);
});
success('write into json');

ctx.response.body = `./assets/${createTime}.json`;
};
Expand Down

0 comments on commit c635a56

Please sign in to comment.