Add episode ranges parameter

This commit is contained in:
KaKi87 2023-03-10 03:57:00 +01:00
parent 56d285bf6d
commit b09506deca
3 changed files with 30 additions and 11 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.idea
data

0
data/.gitkeep Normal file
View File

View File

@ -1,15 +1,32 @@
const [ comicId ] = Deno.args;
const
parseRanges = str => str
.split(',')
.flatMap(chunk => {
const
[
strA,
strB
] = chunk.split('-'),
a = parseInt(strA),
b = strB && parseInt(strB);
return Array(strB ? b + 1 - a : 1).fill(a).map((x, y) => x + y);
}),
[
comicId,
episodeRanges
] = Deno.args,
episodeNumbers = parseRanges(episodeRanges),
comicRes = await (await fetch(`https://www.webtoons.com/en/*/*/list?title_no=${comicId}`)).text(),
comicTitle = comicRes.match(/<title>(.+) \|/)[1],
comicEpisodeCount = episodeNumbers.length,
rootDirectory = `./data/${comicTitle}`;
const comicReq = await fetch('https://www.webtoons.com/en/*/*/list?title_no=' + comicId);
const comicRes = await comicReq.text();
try { await Deno.mkdir(rootDirectory); } catch(_){}
const comicTitle = comicRes.match(/<title>(.+) \|/)[1];
const comicEpisodeCount = parseInt(comicRes.match(/data-episode-no="([0-9]+)"/)[1]);
for(let episodeIndex = 0; episodeIndex < comicEpisodeCount; episodeIndex++){
const comicEpisodeNumber = episodeNumbers[episodeIndex];
try { await Deno.mkdir('./' + comicTitle); } catch(_){}
for(let comicEpisodeNumber = 1; comicEpisodeNumber <= comicEpisodeCount; comicEpisodeNumber++){
console.log(`Downloading episode ${comicEpisodeNumber}/${comicEpisodeCount}`);
console.log(`Downloading episode ${comicEpisodeNumber} (${episodeIndex}/${comicEpisodeCount})`);
const episodeReq = await fetch(`https://www.webtoons.com/en/*/*/*/viewer?title_no=${comicId}&episode_no=${comicEpisodeNumber}`);
const episodeRes = await episodeReq.text();
@ -18,7 +35,7 @@ for(let comicEpisodeNumber = 1; comicEpisodeNumber <= comicEpisodeCount; comicEp
const episodeStripsCount = episodeStrips.length;
for(let stripIndex = 0; stripIndex < episodeStripsCount; stripIndex++){
console.log(`Downloading episode ${comicEpisodeNumber}/${comicEpisodeCount} strip ${stripIndex + 1}/${episodeStripsCount}`);
console.log(`Downloading episode ${comicEpisodeNumber} (${episodeIndex}/${comicEpisodeCount}) strip ${stripIndex + 1}/${episodeStripsCount}`);
const stripReq = await fetch(episodeStrips[stripIndex], {
headers: {
@ -27,6 +44,6 @@ for(let comicEpisodeNumber = 1; comicEpisodeNumber <= comicEpisodeCount; comicEp
});
const stripBuffer = await stripReq.arrayBuffer();
await Deno.writeFile(`./${comicTitle}/${comicEpisodeNumber}_${stripIndex + 1}.jpg`, new Uint8Array(stripBuffer));
await Deno.writeFile(`${rootDirectory}/${comicEpisodeNumber}_${stripIndex + 1}.jpg`, new Uint8Array(stripBuffer));
}
}