Skip to content

Commit

Permalink
feat(options): override
Browse files Browse the repository at this point in the history
  • Loading branch information
yinyanfr committed Mar 31, 2023
1 parent d974388 commit 9709cfb
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 60 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Now this library is designed to be used with multiple manga / comic sites.
- [library] The code has been refactored and can now add sites as plugins.
- [CLI] The `-b, --batch` flag is now default to 1 when not set.
- Downloaded images are now renamed by index (01 ~ ).
- Downloaders now ignores downloaded chapters by default, set `configs.override` to `true` or for CLI use `-O, --override` if you want to override.
- Downloaders now ignores downloaded chapters by default, set `options.override` to `true` or for CLI use `-O, --override` if you want to override.

```typescript
// Before
Expand Down
109 changes: 64 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ This library is not for browsers.

## :green_book: Quick Start

### Stable

You need Node.js (LTS or the current version) to run this project.

```bash
Expand All @@ -44,53 +46,65 @@ npm i zerobyw-dl
npx zerobyw-dl help
```

### Nightly

```bash
npm i yinyanfr/comic-dl
# or
git clone https://github.com/yinyanfr/comic-dl.git
cd comic-dl
npm i
npx . help
```

## :wrench: Cli

```
Usage: comic-dl [options] [command]
Commands:
chapter, c, ch Download images from one chapter.
download, d, dl Download chapters from a manga serie.
help Display help
list, l, ls List all chapters of a manga serie.
version Display version
Options:
-a, --archive Optional: Output zip or cbz archive grouped by chapters.
-b, --batch Optional: Set the number or images to be downloaded simultaneously, default to 1.
-C, --chapters Optional: Only downloading given list of chapters, example: -C 1,2,4,7
-c, --cookie Optional (but recommanded): Provide the path to a text file that contains your cookie.
-F, --format Optional: the format of downloaded picture, depending on the modules, example: webp / jpg.
-f, --from Optional: Starting chapter when downloading a serie, default to 0.
-h, --help Output usage information
-i, --info Optional: Generate ComicInfo.xml.
-M, --max-title-length Optional: restrict the length of title as the folder name.
-m, --module Optional: Specify the module (site) name. Will attempt to detect module by url if not set.
-n, --name Optional: Proride the serie title and override the folder name.
-o, --output Optional: The path where downloaded files are saved (default to .), setting this flag when using list will save a ComicInfo.xml to the path.
-r, --retry Optional: Automatically re-download chapters with failed images.
-s, --slience Optional: Silence the console output, including the confirm prompt.
-T, --timeout Optional: Override the default 10s request timeout.
-t, --to Optional: Ending chapter when downloading a serie, defaults to chapter.length - 1.
-u, --url The url to the serie or the chapter.
-v, --verbose Optional: Display detailed error message, overrides silence.
-V, --version Output the version number
-y, --yes Optional: Skipping confirmation prompt when downloading series.
-z, --zip-level Optional: zip level for archive, default to 5.
Examples:
- Download a serie from its 10th chapter to 20th chapter to the given destination, 10 images at a time, output zip archives with ComicInfo.xml by chapter, retry if a chapter is not properly downloaded.
$ npx comic-dl dl -c cookie.txt -f 10 -t 20 -o ~/Download/manga -a zip -r -i -b 10 -u serie_url
- Download chapter index 0, 4, 12 from a serie
$ npx comic-dl dl -c cookie.txt -o ~/Download/manga -i -u serie_url -c 0,4,12
- List all chapters of the given serie.
$ npx comic-dl ls -u serie_url
- Download a chapter named Chapter1 to current path.
$ npx comic-dl ch -n Chapter1 -u chapter_url -c cookie.txt
Usage: comic-dl [options] [command]
Commands:
chapter, c, ch Download images from one chapter.
download, d, dl Download chapters from a manga serie.
help Display help
list, l, ls List all chapters of a manga serie.
version Display version
Options:
-a, --archive Optional: Output zip or cbz archive grouped by chapters.
-b, --batch Optional: Set the number or images to be downloaded simultaneously, default to 1.
-C, --chapters Optional: Only downloading given list of chapters, example: -C 1,2,4,7
-c, --cookie Optional (but recommanded): Provide the path to a text file that contains your cookie.
-F, --format Optional: the format of downloaded picture, depending on the modules, example: webp / jpg.
-f, --from Optional: Starting chapter when downloading a serie, default to 0.
-h, --help Output usage information
-i, --info Optional: Generate ComicInfo.xml.
-M, --max-title-length Optional: restrict the length of title as the folder name.
-m, --module Optional: Specify the module (site) name. Will attempt to detect module by url if not set.
-n, --name Optional: Proride the serie title and override the folder name.
-o, --output Optional: The path where downloaded files are saved (default to .), setting this flag when using list will save a ComicInfo.xml to the path.
-O, --override Optional: overrides downloaded chapters.
-r, --retry Optional: Automatically re-download chapters with failed images.
-s, --slience Optional: Silence the console output, including the confirm prompt.
-T, --timeout Optional: Override the default 10s request timeout.
-t, --to Optional: Ending chapter when downloading a serie, defaults to chapter.length - 1.
-u, --url The url to the serie or the chapter.
-v, --verbose Optional: Display detailed error message, overrides silence.
-V, --version Output the version number
-y, --yes Optional: Skipping confirmation prompt when downloading series.
-z, --zip-level Optional: zip level for archive, default to 5.
Examples:
- Download a serie from its 10th chapter to 20th chapter to the given destination, 10 images at a time, output zip archives with ComicInfo.xml by chapter, retry if a chapter is not properly downloaded.
$ npx comic-dl dl -c cookie.txt -f 10 -t 20 -o ~/Download/manga -a zip -r -i -b 10 -u serie_url
- Download chapter index 0, 4, 12 from a serie, overriding downloaded files.
$ npx comic-dl dl -c cookie.txt -o ~/Download/manga -i -O -u serie_url -c 0,4,12
- List all chapters of the given serie.
$ npx comic-dl ls -u serie_url
- Download a chapter named Chapter1 to current path.
$ npx comic-dl ch -n Chapter1 -u chapter_url -c cookie.txt
```

## :book: Library
Expand Down Expand Up @@ -122,6 +136,9 @@ const configs = {
// Restrict the length of title's length, in case your file system has such limitation (Optional: default to undefined)
maxTitleLength: 30,
// Zip level for archives (Optional: default to 5)
zipLevel: 5,
// Format of downloaded image, (Optional: depending on the modules, normally default to webp or jpg)
format: "webp",
}; // Optional

const downloader = new ZeroBywDownloader(destination, configs);
Expand Down Expand Up @@ -160,7 +177,8 @@ const options = {
rename: undefined, // Optional: Changing the folder name, default to undefined
retry: false, // Optional: Automatically re-download chapters with failed images.
info: true, // Optional: Generates ComicInfo.xml, default to **false**
chapters: undefined, // Optional: Automatically re-download chapters with failed images
chapters: undefined, // Optional: Array of chapter indexes to download, will download the entire serie if not provided
override: false, // Optional: Overriding downloaded chapters, default to false
onProgress: (progress) => {
console.log(progress);
}, // Optional: Called when a chapter is downloaded or failed to do so
Expand Down Expand Up @@ -190,6 +208,7 @@ const options = {
index: 0, // chapter index
title: "Serie Title",
info: ComicInfo, // Optional: Generates ComicInfo.xml, please refer to ComicInfo's Documentations
override: false, // Optional: Overriding downloaded chapters, default to false
onProgress: (progress) => {}, // Optional: Called when a chapter is downloaded or failed to do so, the same as in serie options
}; // Optional

Expand Down
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "comic-dl",
"version": "2.0.0-alpha",
"description": "Yet another batch downloader for manga / comic sites",
"version": "2.0.0",
"description": "Yet another batch downloader for manga/comic sites",
"main": "dist/index.js",
"bin": "dist/cli.js",
"types": "src/global.d.ts",
Expand All @@ -13,7 +13,12 @@
},
"keywords": [
"scraper",
"zerobyw"
"zerobyw",
"copymanga",
"manga",
"comic",
"manga-downloader",
"comic-downloader"
],
"repository": {
"type": "git",
Expand Down Expand Up @@ -44,4 +49,4 @@
"mime-types": "^2.1.35",
"yesno": "^0.4.0"
}
}
}
32 changes: 26 additions & 6 deletions src/comic-downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,33 @@ export default abstract class ComicDownloader {
? archiver("zip", { zlib: { level: this.configs?.zipLevel ?? 5 } })
: undefined;

const skippedProgress = {
index: options?.index,
name,
status: "skipped" as const,
failures: 0,
};
if (this.configs?.archive) {
const archiveStream = fs.createWriteStream(
path.join(
chapterWritePath,
`${name}.${this.configs?.archive === "cbz" ? "cbz" : "zip"}`
)
const archiveWritePath = path.join(
chapterWritePath,
`${name}.${this.configs?.archive === "cbz" ? "cbz" : "zip"}`
);
archive?.pipe(archiveStream);
if (!options.override && fs.existsSync(archiveWritePath)) {
this.log(
`Skipped: ${options?.index} - ${name} has already been downloaded`
);
return skippedProgress;
} else {
const archiveStream = fs.createWriteStream(archiveWritePath);
archive?.pipe(archiveStream);
}
} else {
if (!options.override && fs.existsSync(chapterWritePath)) {
this.log(
`Skipped: ${options?.index} - ${name} has already been downloaded`
);
return skippedProgress;
}
}

let failures = 0;
Expand Down Expand Up @@ -313,6 +332,7 @@ export default abstract class ComicDownloader {
index: chapter.index,
title: options.rename ?? serie.title,
info: options.info ? serie.info : undefined,
override: options.override,
onProgress: options?.onProgress,
});
summary.push(progress);
Expand Down
5 changes: 4 additions & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export const downloadCommand: Command = async (name, sub, options = {}) => {
retry,
chapters,
info,
override,
} = options;

let current: Partial<DownloadProgress> = {};
Expand All @@ -132,6 +133,7 @@ export const downloadCommand: Command = async (name, sub, options = {}) => {
rename,
retry,
info,
override,
chapters:
chapters !== undefined
? `${chapters}`.split(",").map((e) => parseInt(e))
Expand Down Expand Up @@ -168,7 +170,7 @@ export const downloadCommand: Command = async (name, sub, options = {}) => {
};

export const chapterCommand: Command = async (name, sub, options = {}) => {
const { url, name: chapterName, verbose, output } = options;
const { url, name: chapterName, verbose, output, override } = options;

try {
if (url) {
Expand All @@ -179,6 +181,7 @@ export const chapterCommand: Command = async (name, sub, options = {}) => {
}
await downloader.downloadChapter(chapterName ?? "Untitled", url, {
info: serie?.info,
override,
});
} else {
console.log("Please Provide URL.");
Expand Down
5 changes: 3 additions & 2 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ interface Configs {
maxTitleLength?: number;
zipLevel?: number;
format?: string;
override?: boolean;
}

interface Chapter {
Expand Down Expand Up @@ -66,7 +65,7 @@ interface DownloadProgress {
index?: number;
name: string;
uri?: string;
status: "completed" | "failed";
status: "completed" | "failed" | "skipped";
failed?: number;
}

Expand All @@ -78,6 +77,7 @@ interface SerieDownloadOptions {
chapters?: number[];
confirm?: boolean;
info?: boolean;
override?: boolean;
onProgress?: (progress: DownloadProgress) => void;
}

Expand All @@ -91,6 +91,7 @@ interface ChapterDownloadOptions {
index?: number;
title?: string;
info?: ComicInfo;
override?: boolean;
onProgress?: (progress: DownloadProgress) => void;
}

Expand Down
1 change: 0 additions & 1 deletion src/modules/copymanga/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export default class CopymangaDownloader extends ComicDownloader {
static siteName = "copymanga";

static canHandleUrl(url: string): boolean {
console.log(url);
return /copymanga/.test(url);
}

Expand Down

0 comments on commit 9709cfb

Please sign in to comment.