Skip to content

Commit

Permalink
Finished Version 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
StereoPT committed Nov 25, 2020
1 parent 7a06795 commit 6928d74
Show file tree
Hide file tree
Showing 8 changed files with 739 additions and 669 deletions.
1,321 changes: 677 additions & 644 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "Manhwa to PDF",
"main": "src/app.js",
"bin": "src/app.js",
"scripts": {
"start": "node src/app.js",
"all": "node src/app.js --all",
Expand All @@ -22,7 +23,7 @@
"eslint": "^7.14.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.22.1",
"nodemon": "^2.0.6"
"pkg": "^4.4.9"
},
"dependencies": {
"axios": "^0.21.0",
Expand Down
8 changes: 6 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Object.keys(manhwaParsers).map((key) => {
});

let { url } = args;
const { all, amount } = args;
const {
all, amount, output,
} = args;

console.log('[PDF Manhwa]');

Expand All @@ -27,7 +29,9 @@ async function GetChapter(chapterUrl) {
}

try {
parserMap.get(host).getChapter(chapterUrl, { all, amount });
parserMap.get(host).getChapter(chapterUrl, {
all, amount, output,
});
} catch(error) {
console.error(error);
}
Expand Down
19 changes: 17 additions & 2 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
const os = require('os');
const fs = require('fs');
const path = require('path');

const imagesFolder = path.join(__dirname, '..', 'images');
const pdfsFolder = path.join(__dirname, '..', 'pdfs');
const imagesFolder = () => {
let imagesDir = path.join(__dirname, '..', 'images');

if(!fs.existsSync(imagesDir)) imagesDir = os.tmpdir();

return imagesDir;
};

const pdfsFolder = () => {
const pdfsDir = path.join(__dirname, '..', 'pdfs');

if(!fs.existsSync(pdfsDir)) fs.mkdirSync(pdfsDir);

return pdfsDir;
};

module.exports = {
imagesFolder,
Expand Down
11 changes: 7 additions & 4 deletions src/lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const yargs = require('yargs');

const getCommandLineArgs = (processArgv) => yargs(processArgv)
.usage('Usage:\n'
+ ' -u <manhwa chapter url>\n'
+ ' -a <download all chapters>')
.usage('Usage: $0 <cmd> [args]')
.option({
u: {
alias: 'url',
Expand All @@ -23,7 +21,12 @@ const getCommandLineArgs = (processArgv) => yargs(processArgv)
demandOption: false,
describe: 'Amount of Chapters to Download',
type: 'integer',
default: 1,
},
o: {
alias: 'output',
demandOption: false,
describe: 'PDF Output Location',
type: 'string',
},
}).argv;

Expand Down
8 changes: 6 additions & 2 deletions src/lib/pdfHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path');
const PDFDocument = require('pdfkit');
const { pdfsFolder } = require('../config');

async function generatePDF(pdfName, imagesPath) {
async function generatePDF(pdfName, imagesPath, output) {
const pdfDocument = new PDFDocument({
autoFirstPage: false,
info: { Author: 'StereoPT' },
Expand All @@ -18,7 +18,11 @@ async function generatePDF(pdfName, imagesPath) {
return total + pdfDocument.openImage(image).height;
}, 0);

pdfDocument.pipe(fs.createWriteStream(path.join(pdfsFolder, actualPdfName)));
let pdfPath;
if(output !== undefined) pdfPath = path.join(output, actualPdfName);
else pdfPath = path.join(pdfsFolder(), actualPdfName);

pdfDocument.pipe(fs.createWriteStream(pdfPath));
pdfDocument.addPage({
size: [pageWidth, pageHeight],
margin: 0,
Expand Down
29 changes: 20 additions & 9 deletions src/parsers/mangazuki.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ async function scrapeChapterPart($, mangaTitle) {
}

async function scrapeChapter(chapterUrl, html, args) {
const { all } = args;
const { all, amount, output } = args;
let imagesPath = [];

if(amount <= 0) {
console.log('[PDF Manhwa] Finished!');
return;
}

let $ = cheerio.load(html);
const mangaTitle = createName($('meta[name="twitter:title"]').attr('content'));
let nextPage;
Expand All @@ -55,23 +60,29 @@ async function scrapeChapter(chapterUrl, html, args) {

const generatingSpinner = ora('Generating...').start();
const pdfName = chapterUrl.split('/').pop();
await generatePDF(pdfName, imagesPath);
await generatePDF(pdfName, imagesPath, output);
generatingSpinner.succeed('Generated!');

await removeImages(imagesPath);
console.log('');

if(all === false) {
getNextChapter(async () => {
const nextPageHtml = await getHtml(nextPage);
scrapeChapter(nextPage, nextPageHtml, args);
});
if(all === false && amount === undefined) {
getNextChapter(() => downloadNextChapter(nextPage, args));
} else {
const nextPageHtml = await getHtml(nextPage);
scrapeChapter(nextPage, nextPageHtml, args);
// Download All Chapters
const newArgs = { ...args };
if(amount !== undefined) {
newArgs.amount = amount - 1;
}
await downloadNextChapter(nextPage, newArgs);
}
}

async function downloadNextChapter(nextPage, args) {
const nextPageHtml = await getHtml(nextPage);
scrapeChapter(nextPage, nextPageHtml, args);
}

module.exports = {
name: 'Mangazuki',
host: 'mangazuki.me',
Expand Down
9 changes: 4 additions & 5 deletions src/parsers/toonily.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function scrapeChapterPart($, mangaTitle) {
const imgSrc = img.attribs['data-src'].trim();
const imgPadding = img.attribs.id;
const imgName = mangaTitle.concat('-', imgPadding, '.png');
const imgPath = path.join(imagesFolder, imgName);
const imgPath = path.join(imagesFolder(), imgName);

imagesPath.push(imgPath);
imagePromises.push(downloadImage(imgSrc, imgPath));
Expand All @@ -29,8 +29,7 @@ async function scrapeChapterPart($, mangaTitle) {
}

async function scrapeChapter(chapterUrl, html, args) {
const { all } = args;
const { amount } = args;
const { all, amount, output } = args;

if(amount <= 0) {
console.log('[PDF Manhwa] Finished!');
Expand All @@ -48,7 +47,7 @@ async function scrapeChapter(chapterUrl, html, args) {
downloadingSpinner.succeed('Downloaded!');

const generatingSpinner = ora('Generating...').start();
await generatePDF(mangaTitle, imagesPath);
await generatePDF(mangaTitle, imagesPath, output);
generatingSpinner.succeed('Generated!');

await removeImages(imagesPath);
Expand Down Expand Up @@ -81,4 +80,4 @@ module.exports = {
},
};

// https://toonily.com/webtoon/solmis-channel/chapter-19/
// https://toonily.com/webtoon/solmis-channel/chapter-46/

0 comments on commit 6928d74

Please sign in to comment.