#Download API
DownloadManager 类管理下载队列,支持进度显示和批量下载。
#导入
import { const download: DownloadManager download } from 'music-getter';download 是一个 DownloadManager 的单例实例,可以直接使用。
#方法
#add
添加下载任务到队列。
declare class class DownloadManager DownloadManager {
DownloadManager.add(url: string | (() => Promise<string>), output: string): void add (url: string | (() => Promise<string>) url : string | (() => interface Promise<T>Represents the completion of an asynchronous operation
Promise <string>), output: string output : string): void;
}| 参数 | 类型 | 说明 |
|---|---|---|
url | string | (() => Promise<string>) | 下载 URL,可以是字符串或返回字符串的异步函数 |
output | string | 输出文件路径 |
#startAll
开始执行所有下载任务。
declare class class DownloadManager DownloadManager {
DownloadManager.startAll(): Promise<void> startAll (): interface Promise<T>Represents the completion of an asynchronous operation
Promise <void>;
}下载过程中会显示实时进度条:
Downloading 1/3:
File: Daylight-David Kushner.mp3
████████████████████ 100% | 3500000/3500000 bytes | ETA: 0s
Downloading 2/3:
File: 青花瓷-周杰伦.mp3
████████████████░░░░ 80% | 2800000/3500000 bytes | ETA: 2s
All downloads finished!#clear
清空下载队列。
declare class class DownloadManager DownloadManager {
DownloadManager.clear(): void clear (): void;
}#getCount
获取队列中的任务数量。
declare class class DownloadManager DownloadManager {
DownloadManager.getCount(): number getCount (): number;
}#完整示例
#下载单曲
import { class Meting Meting , const download: DownloadManager download } from 'music-getter';
const const meting: Meting meting = new new Meting(server?: string): Meting Meting ('netease');
const meting: Meting meting .Meting.format(enable: boolean): Meting format (true);
const const song: any song = var JSON: JSONAn intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON .JSON.parse(text: string, reviver?: (this: any, key: string, value: any) => any): anyConverts a JavaScript Object Notation (JSON) string into an object.
@paramtext A valid JSON string.@paramreviver A function that transforms the results. This function is called for each member of the object.
If a member contains nested objects, the nested objects are transformed before the parent object is.@throws{SyntaxError} If text is not valid JSON. parse (await const meting: Meting meting .Meting.song(id: string | number): Promise<string> song ('1372188635'));
const download: DownloadManager download .DownloadManager.add(url: string | (() => Promise<string>), output: string): void add (const song: any song [0].url_id, './Daylight.mp3');
await const download: DownloadManager download .DownloadManager.startAll(): Promise<void> startAll ();#批量下载歌单
import { class Meting Meting , const download: DownloadManager download } from 'music-getter';
const const meting: Meting meting = new new Meting(server?: string): Meting Meting ('netease');
const meting: Meting meting .Meting.format(enable: boolean): Meting format (true);
const const songs: any songs = var JSON: JSONAn intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON .JSON.parse(text: string, reviver?: (this: any, key: string, value: any) => any): anyConverts a JavaScript Object Notation (JSON) string into an object.
@paramtext A valid JSON string.@paramreviver A function that transforms the results. This function is called for each member of the object.
If a member contains nested objects, the nested objects are transformed before the parent object is.@throws{SyntaxError} If text is not valid JSON. parse (await const meting: Meting meting .Meting.playlist(id: string | number): Promise<string> playlist ('7697114803'));
for (const const song: any song of const songs: any songs ) {
const const filename: string filename = `${const song: any song .name}-${const song: any song .artist.join(',')}.mp3`;
const download: DownloadManager download .DownloadManager.add(url: string | (() => Promise<string>), output: string): void add (const song: any song .url_id, `./downloads/${const filename: string filename }`);
}
var console: Console console .Console.log(...data: any[]): voidThe console.log() static method outputs a message to the console.
log (`准备下载 ${const download: DownloadManager download .DownloadManager.getCount(): number getCount ()} 首歌曲`);
await const download: DownloadManager download .DownloadManager.startAll(): Promise<void> startAll ();#使用动态 URL
import { class Meting Meting , const download: DownloadManager download } from 'music-getter';
const const meting: Meting meting = new new Meting(server?: string): Meting Meting ('netease');
const meting: Meting meting .Meting.format(enable: boolean): Meting format (true);
const const song: any song = var JSON: JSONAn intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON .JSON.parse(text: string, reviver?: (this: any, key: string, value: any) => any): anyConverts a JavaScript Object Notation (JSON) string into an object.
@paramtext A valid JSON string.@paramreviver A function that transforms the results. This function is called for each member of the object.
If a member contains nested objects, the nested objects are transformed before the parent object is.@throws{SyntaxError} If text is not valid JSON. parse (await const meting: Meting meting .Meting.song(id: string | number): Promise<string> song ('1372188635'));
// 使用异步函数动态获取 URL
const download: DownloadManager download .DownloadManager.add(url: string | (() => Promise<string>), output: string): void add (async () => {
const const urlData: any urlData = var JSON: JSONAn intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON .JSON.parse(text: string, reviver?: (this: any, key: string, value: any) => any): anyConverts a JavaScript Object Notation (JSON) string into an object.
@paramtext A valid JSON string.@paramreviver A function that transforms the results. This function is called for each member of the object.
If a member contains nested objects, the nested objects are transformed before the parent object is.@throws{SyntaxError} If text is not valid JSON. parse (await const meting: Meting meting .Meting.url(id: string | number, bitrate?: number): Promise<string> url (const song: any song [0].id, 320));
return const urlData: any urlData [0].url;
}, './Daylight.mp3');
await const download: DownloadManager download .DownloadManager.startAll(): Promise<void> startAll ();#注意事项
- 下载任务会按添加顺序依次执行
- 文件名中的非法字符会自动替换为下划线
- 输出目录不存在时会自动创建
- 下载进度条仅在服务器返回
Content-Length头时显示
