Comparing version
#!/usr/bin/env node | ||
import{a as i,b as d,c}from"./chunk-HYWEAAWU.js";import p from"cli-progress";import f from"meow";import h from"fs";import u from"path";var s=f(` | ||
import{a as i,b as p,c}from"./chunk-SGMKDPTA.js";import g from"cli-progress";import u from"meow";import f from"fs";import b from"path";var d=u(` | ||
USAGE | ||
@@ -16,3 +16,5 @@ $ imgdl <url> ... [OPTIONS] | ||
-h, --help Show this help message | ||
-H, --header=<header> The header to send with the request. Can be used multiple times | ||
-i, --increment Enable increment mode. Default: false | ||
--interval=<number> The interval between each batch of requests in milliseconds | ||
-n, --name=<filename> The filename. Default: original filename or timestamp | ||
@@ -22,2 +24,3 @@ --max-retry=<number> Set the maximum number of times to retry the request if it fails | ||
--start=<number> The start index for increment mode. Default: 0 | ||
--step=<number> The number of requests to make at the same time. Default: 5 | ||
-t, --timeout=<number> Set timeout for each request in milliseconds | ||
@@ -32,5 +35,6 @@ -v, --version Show the version number | ||
$ imgdl https://example.com/image-{i}.jpg --increment --start=1 --end=10 | ||
`,{importMeta:import.meta,description:"Download an image from a URL",booleanDefault:void 0,flags:{dir:{shortFlag:"d",type:"string"},ext:{shortFlag:"e",type:"string"},increment:{shortFlag:"i",type:"boolean"},start:{type:"number"},end:{type:"number"},name:{shortFlag:"n",type:"string"},maxRetry:{type:"number"},silent:{type:"boolean"},timeout:{shortFlag:"t",type:"number"}}});async function x(){let t=s.input,{flags:e}=s;if(t.length||s.showHelp(),e.increment){if(t.length>1)throw new i("Only one URL is allowed in increment mode");let n=t[0];if(!n.includes("{i}"))throw new i("The URL must contain {i} placeholder for the index");if(!e.end)throw new i("The end index is required in increment mode");if(e.start&&e.start>e.end)throw new i("The start index cannot be greater than the end index");let{start:m=0,end:g}=e;t=[];for(let l=m;l<=g;l+=1)t.push(n.replace("{i}",l.toString()))}e.silent||console.log(` | ||
Downloading...`);let o=new p.SingleBar({format:"{percentage}% [{bar}] {value}/{total} | Success: {success} | ETA: {eta_formatted} | Elapsed: {duration_formatted}",hideCursor:null,barsize:24}),a=0,r=0;!e.silent&&t.length>1&&o.start(t.length,0,{success:a}),await c(t.length===1?t[0]:t,{directory:e.dir,name:e.name,extension:e.ext,onSuccess:()=>{e.silent||(a+=1,o.increment({success:a}))},onError:(n,m)=>{if(r+=1,e.silent||o.increment(),n instanceof i||n instanceof d)throw n;h.appendFileSync(u.resolve(e.dir||process.cwd(),"error.log"),`${new Date().toISOString()} failed download from ${m}, ${n.name}: ${n.message} | ||
`)},maxRetry:e.maxRetry,timeout:e.timeout}),e.silent||(o.stop(),console.log("Done!"),r&&console.log(`${r} image${r>1?"s":""} failed to download. See ./error.log for details.`))}x().catch(t=>{console.error(`${t.name}: ${t.message}`),process.exit(1)}); | ||
$ imgdl https://example.com/image.jpg --header="User-Agent: Mozilla/5.0" --header="Cookie: foo=bar" | ||
`,{importMeta:import.meta,booleanDefault:void 0,flags:{dir:{shortFlag:"d",type:"string"},ext:{shortFlag:"e",type:"string"},header:{shortFlag:"H",type:"string",isMultiple:!0},increment:{shortFlag:"i",type:"boolean"},interval:{type:"number"},start:{type:"number"},end:{type:"number"},name:{shortFlag:"n",type:"string"},maxRetry:{type:"number"},silent:{type:"boolean"},step:{type:"number"},timeout:{shortFlag:"t",type:"number"}}});async function w(){let t=d.input,{flags:e}=d;if(t.length||d.showHelp(),e.increment){if(t.length>1)throw new i("Only one URL is allowed in increment mode");let n=t[0];if(!n.includes("{i}"))throw new i("The URL must contain {i} placeholder for the index");if(!e.end)throw new i("The end index is required in increment mode");if(e.start&&e.start>e.end)throw new i("The start index cannot be greater than the end index");let{start:r=0,end:m}=e;t=[];for(let s=r;s<=m;s+=1)t.push(n.replace("{i}",s.toString()))}e.silent||console.log(` | ||
Downloading...`);let o=new g.SingleBar({format:"{percentage}% [{bar}] {value}/{total} | Success: {success} | ETA: {eta_formatted} | Elapsed: {duration_formatted}",hideCursor:null,barsize:24}),l=0,a=0;!e.silent&&t.length>1&&o.start(t.length,0,{success:l});let h={};e.header&&e.header.forEach(n=>{let[r,m]=n.split(":");if(!r||!m)throw new i("Invalid header format");h[r.trim()]=m.trim()}),await c(t.length===1?t[0]:t,{directory:e.dir,name:e.name,extension:e.ext,headers:h,interval:e.interval,onSuccess:()=>{e.silent||(l+=1,o.increment({success:l}))},onError:(n,r)=>{if(a+=1,e.silent||o.increment(),n instanceof i||n instanceof p)throw n;f.appendFileSync(b.resolve(e.dir||process.cwd(),"error.log"),`${new Date().toISOString()} failed download from ${r}, ${n.name}: ${n.message} | ||
`)},maxRetry:e.maxRetry,step:e.step,timeout:e.timeout}),e.silent||(o.stop(),console.log("Done!"),a&&console.log(`${a} image${a>1?"s":""} failed to download. See ./error.log for details.`))}w().catch(t=>{console.error(`${t.name}: ${t.message}`),process.exit(1)}); | ||
//# sourceMappingURL=cli.js.map |
@@ -9,2 +9,6 @@ type DownloadOptions = { | ||
/** | ||
* The headers to send with the request. | ||
*/ | ||
headers?: Record<string, string | string[] | undefined>; | ||
/** | ||
* The name of the image file. | ||
@@ -106,2 +110,14 @@ * | ||
onError?: (error: Error, url: string) => void; | ||
/** | ||
* The number of requests to make at the same time. | ||
* | ||
* @default 5 | ||
*/ | ||
step?: number; | ||
/** | ||
* The interval between each batch of requests in milliseconds. | ||
* | ||
* @default 100 | ||
*/ | ||
interval?: number; | ||
}; | ||
@@ -108,0 +124,0 @@ declare function imgdl(url: string, options?: Options): Promise<Image>; |
@@ -1,2 +0,2 @@ | ||
import{c as a}from"./chunk-HYWEAAWU.js";export{a as default}; | ||
import{c as a}from"./chunk-SGMKDPTA.js";export{a as default}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "img-dl", | ||
"version": "0.2.0", | ||
"description": "Download public image(s), by command or programmatically", | ||
"version": "0.3.0", | ||
"description": "Download image(s), by command or programmatically", | ||
"type": "module", | ||
@@ -38,2 +38,3 @@ "main": "dist/index.js", | ||
"homepage": "https://github.com/fityannugroho/img-dl#readme", | ||
"funding": "https://github.com/sponsors/fityannugroho", | ||
"engines": { | ||
@@ -59,4 +60,5 @@ "node": ">=18" | ||
"meow": "^12.1.1", | ||
"p-queue": "^7.4.1", | ||
"sanitize-filename": "^1.6.3" | ||
} | ||
} |
@@ -39,3 +39,3 @@ # img-dl | ||
``` | ||
Download an image from a URL | ||
Download image(s), by command or programmatically | ||
@@ -55,3 +55,5 @@ USAGE | ||
-h, --help Show this help message | ||
-H, --header=<header> The header to send with the request. Can be used multiple times | ||
-i, --increment Enable increment mode. Default: false | ||
--interval=<number> The interval between each batch of requests in milliseconds | ||
-n, --name=<filename> The filename. Default: original filename or timestamp | ||
@@ -61,2 +63,3 @@ --max-retry=<number> Set the maximum number of times to retry the request if it fails | ||
--start=<number> The start index for increment mode. Default: 0 | ||
--step=<number> The number of requests to make at the same time. Default: 5 | ||
-t, --timeout=<number> Set timeout for each request in milliseconds | ||
@@ -71,2 +74,3 @@ -v, --version Show the version number | ||
$ imgdl https://example.com/image-{i}.jpg --increment --start=1 --end=10 | ||
$ imgdl https://example.com/image.jpg --header="User-Agent: Mozilla/5.0" --header="Cookie: foo=bar" | ||
``` | ||
@@ -147,2 +151,4 @@ | ||
| `extension` | `string` | jpg | The file extension. If not specified, the original extension will be used. If the original extension is not available, 'jpg' will be used. | | ||
| `headers` | `Record<string, string \| string[] \| undefined>` | `undefined` | The headers to send with the request. | | ||
| `interval` | `number` | 100 | The interval between each batch of requests in milliseconds when downloading multiple images. | | ||
| `name` | `string` | image | The filename. If not specified, the original filename will be used. If the original filename is not available, 'image' will be used. <br>When downloading multiple images, `-index` will be appended to the end of the name (suffix). `index` will start from 1. For example: 'image-1' | | ||
@@ -152,2 +158,3 @@ | `maxRetry` | `number` | 2 | Set the maximum number of times to retry the request if it fails. | ||
| `onError` | `(error: Error, url: string) => void` | `undefined` | The callback function to be called when the image fails to download. Only available when downloading multiple images. | | ||
| `step` | `number` | 5 | The number of requests to make at the same time when downloading multiple images. | | ||
| `timeout` | `number` | `undefined` | Set timeout for each request in milliseconds. | ||
@@ -154,0 +161,0 @@ |
Sorry, the diff of this file is not supported yet
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
43787
10.77%175
13.64%177
4.12%5
25%+ Added
+ Added
+ Added
+ Added