-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
59 lines (50 loc) · 1.59 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import chalk from 'chalk';
import { Command } from 'commander';
import { assureDirExists } from './src/fsUtils.ts';
import { Gallery } from './src/Gallery.ts';
import { getAlbumUrl, getPassword } from './src/inquirerUtils.ts';
type OptionsShape = {
maxItems: string;
outDir: string;
};
async function processAlbum(album: Gallery) {
try {
await album.process();
} catch (e: any) {
if (e.name === 'AlbumLoadingError') {
console.log(chalk.magenta('Album is probably protected by password!'));
const albumPass = await getPassword();
if (albumPass) {
album.setPassword(albumPass);
}
await album.process();
} else {
throw e;
}
}
}
async function run(url: string | undefined, password: string | undefined, { maxItems, outDir }: OptionsShape) {
try {
assureDirExists(outDir);
const albumUrl = await getAlbumUrl(url);
const album = new Gallery(albumUrl, parseInt(maxItems, 10), outDir);
if (password) {
album.setPassword(password);
}
await processAlbum(album);
console.log(chalk.green('SUCCESS'));
} catch (e: any) {
console.error(chalk.red('Error: ') + e.message);
process.exit(1);
}
}
const program = new Command();
program
.name('zonerama-downloader')
.argument('[url]', 'url of the album')
.argument('[password]', 'password of the album')
.description('Download all images from Zonerama album')
.option('-o, --out-dir <directory>', 'directory to save the images', './images')
.option('-m, --max-items <number>', 'maximum number of images to download', '500')
.action(run)
.parse(process.argv);