@secret-momo/utils 1.1.2 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/download/aria2.d.ts +14 -0
- package/lib/download/aria2.js +1 -0
- package/lib/download/index.d.ts +25 -7
- package/lib/download/index.js +1 -1
- package/lib/download/types.d.ts +6 -0
- package/lib/index.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { DownloadOptions } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* 使用 aria2c 下载视频资源到指定目录。
|
|
4
|
+
*
|
|
5
|
+
* 下载前会检查本机是否安装 `aria2c`;若不存在会抛出异常,并提示通过
|
|
6
|
+
* `brew install aria2` 安装。下载时使用 `aria2c -x8 -s8 -k1M` 参数,
|
|
7
|
+
* 并根据 URL 或响应 `Content-Type` 推断视频扩展名。
|
|
8
|
+
*
|
|
9
|
+
* @param options 下载配置
|
|
10
|
+
* @param options.url 视频资源 URL
|
|
11
|
+
* @param options.dir 资源保存目录,不存在时会自动创建
|
|
12
|
+
* @returns 下载成功后的文件路径,下载过程失败时返回 `null`
|
|
13
|
+
*/
|
|
14
|
+
export declare function aria2DownloadVideo({ url, dir }: DownloadOptions): Promise<string | null>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{mkdirSync as e}from"node:fs";import{join as n,basename as u}from"node:path";import{execFile as c}from"node:child_process";import{promisify as m}from"node:util";import{dateTimeToNow as s}from"../date-time.js";import{fetch as d}from"../fetch.js";import{logger as f}from"../pino.js";import{pickVideoExtension as l}from"./extension.js";const a=m(c);async function p(){try{await a("aria2c",["--version"])}catch(r){throw r.code==="ENOENT"?new Error("\u672A\u627E\u5230 aria2c \u547D\u4EE4\u884C\u5DE5\u5177\uFF0C\u8BF7\u5148\u4F7F\u7528 brew install aria2 \u5B89\u88C5\u3002",{cause:r}):r}}async function w(r){try{return(await d(r,{method:"HEAD"})).headers.get("content-type")}catch{return null}}async function y({url:r,dir:t}){await p();try{e(t,{recursive:!0});const o=l(r,await w(r)),i=n(t,`${s("YYYYMMDD_HHmmss")}.${o}`);return await a("aria2c",["-x8","-s8","-k1M","-d",t,"-o",u(i),r]),i}catch(o){return f.child({name:"aria2DownloadVideo"}).error(o),null}}export{y as aria2DownloadVideo};
|
package/lib/download/index.d.ts
CHANGED
|
@@ -1,9 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { aria2DownloadVideo } from './aria2';
|
|
2
|
+
import type { DownloadOptions } from './types';
|
|
3
|
+
export { aria2DownloadVideo };
|
|
4
|
+
/**
|
|
5
|
+
* 下载视频资源到指定目录。
|
|
6
|
+
*
|
|
7
|
+
* 会根据 URL 或响应 `Content-Type` 推断视频扩展名,并用当前时间生成文件名。
|
|
8
|
+
* 下载成功时返回保存后的文件路径;下载失败时记录日志并返回 `null`。
|
|
9
|
+
*
|
|
10
|
+
* @param options 下载配置
|
|
11
|
+
* @param options.url 视频资源 URL
|
|
12
|
+
* @param options.dir 资源保存目录,不存在时会自动创建
|
|
13
|
+
* @returns 下载成功后的文件路径,失败时返回 `null`
|
|
14
|
+
*/
|
|
7
15
|
export declare function downloadVideo({ url, dir }: DownloadOptions): Promise<string | null>;
|
|
16
|
+
/**
|
|
17
|
+
* 下载图片资源到指定目录。
|
|
18
|
+
*
|
|
19
|
+
* 会根据 URL 或响应 `Content-Type` 推断图片扩展名,并用当前时间生成文件名。
|
|
20
|
+
* 下载成功时返回保存后的文件路径;下载失败时记录日志并返回 `null`。
|
|
21
|
+
*
|
|
22
|
+
* @param options 下载配置
|
|
23
|
+
* @param options.url 图片资源 URL
|
|
24
|
+
* @param options.dir 资源保存目录,不存在时会自动创建
|
|
25
|
+
* @returns 下载成功后的文件路径,失败时返回 `null`
|
|
26
|
+
*/
|
|
8
27
|
export declare function downloadImage({ url, dir }: DownloadOptions): Promise<string | null>;
|
|
9
|
-
export {};
|
package/lib/download/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{mkdirSync as i,createWriteStream as a}from"node:fs";import{join as m}from"node:path";import{Readable as d}from"node:stream";import{pipeline as c}from"node:stream/promises";import{fetch as s}from"../fetch.js";import{dateTimeToNow as p}from"../date-time.js";import{pickImageExtension as l,pickVideoExtension as w}from"./extension.js";import{logger as f}from"../pino.js";async function y({url:r,dir:e}){try{i(e,{recursive:!0});const o=await s(r);if(!o.body)throw new Error("Response has no body");const n=w(r,o.headers.get("content-type")),t=m(e,`${p("YYYYMMDD_HHmmss")}.${n}`);return await c(d.fromWeb(o.body),a(t)),t}catch(o){return f.child({name:"downloadVideo"}).error(o),null}}async function u({url:r,dir:e}){try{i(e,{recursive:!0});const o=await s(r);if(!o.body)throw new Error("Response has no body");const n=l(r,o.headers.get("content-type")),t=m(e,`${p("YYYYMMDD_HHmmss")}.${n}`);return await c(d.fromWeb(o.body),a(t)),t}catch(o){return f.child({name:"downloadImage"}).error(o),null}}export{u as downloadImage,y as downloadVideo};
|
|
1
|
+
import{mkdirSync as i,createWriteStream as a}from"node:fs";import{join as m}from"node:path";import{Readable as d}from"node:stream";import{pipeline as c}from"node:stream/promises";import{fetch as s}from"../fetch.js";import{dateTimeToNow as p}from"../date-time.js";import{pickImageExtension as l,pickVideoExtension as w}from"./extension.js";import{logger as f}from"../pino.js";import{aria2DownloadVideo as k}from"./aria2.js";async function y({url:r,dir:e}){try{i(e,{recursive:!0});const o=await s(r);if(!o.body)throw new Error("Response has no body");const n=w(r,o.headers.get("content-type")),t=m(e,`${p("YYYYMMDD_HHmmss")}.${n}`);return await c(d.fromWeb(o.body),a(t)),t}catch(o){return f.child({name:"downloadVideo"}).error(o),null}}async function u({url:r,dir:e}){try{i(e,{recursive:!0});const o=await s(r);if(!o.body)throw new Error("Response has no body");const n=l(r,o.headers.get("content-type")),t=m(e,`${p("YYYYMMDD_HHmmss")}.${n}`);return await c(d.fromWeb(o.body),a(t)),t}catch(o){return f.child({name:"downloadImage"}).error(o),null}}export{k as aria2DownloadVideo,u as downloadImage,y as downloadVideo};
|
package/lib/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{downloadImage as r,downloadVideo as t}from"./download/index.js";import{dateTimeToNow as f,dateToNow as a,timestampToDate as d,yesterdayDate as p}from"./date-time.js";import{getNodeName as i}from"./env.js";import{fetch as
|
|
1
|
+
import{downloadImage as r,downloadVideo as t}from"./download/index.js";import{dateTimeToNow as f,dateToNow as a,timestampToDate as d,yesterdayDate as p}from"./date-time.js";import{getNodeName as i}from"./env.js";import{fetch as n,fetchHtml as g,fetchJson as s,fetchText as c,mobileUserAgent as u,userAgent as w}from"./fetch.js";import{fileSize as h}from"./file.js";import{fixed2 as y,fixed4 as D,toPercentage as v}from"./num.js";import{logger as I}from"./pino.js";import{sleep as U,sleep_random as V}from"./sleep.js";import{genId as z,uuidv4 as H}from"./uuid.js";import{getRedirectUrl as Q,removeQueryParams as R}from"./url.js";import{aria2DownloadVideo as _}from"./download/aria2.js";import{default as k}from"pino";export{_ as aria2DownloadVideo,f as dateTimeToNow,a as dateToNow,r as downloadImage,t as downloadVideo,n as fetch,g as fetchHtml,s as fetchJson,c as fetchText,h as fileSize,y as fixed2,D as fixed4,z as genId,i as getNodeName,Q as getRedirectUrl,I as logger,u as mobileUserAgent,k as pino,R as removeQueryParams,U as sleep,V as sleep_random,d as timestampToDate,v as toPercentage,w as userAgent,H as uuidv4,p as yesterdayDate};
|