Download API

The DownloadManager class manages the download queue, supporting progress display and batch downloads.

Import

import { 
const download: DownloadManager
download
} from 'music-getter';

download is a singleton instance of DownloadManager that can be used directly.

Methods

add

Add a download task to the queue.

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;
}
ParameterTypeDescription
urlstring | (() => Promise<string>)Download URL, can be a string or an async function returning a string
outputstringOutput file path

startAll

Execute all download tasks.

declare class 
class DownloadManager
DownloadManager
{
DownloadManager.startAll(): Promise<void>
startAll
():
interface Promise<T>

Represents the completion of an asynchronous operation

Promise
<void>;
}

Real-time progress bars are displayed during the download process:

Downloading 1/3:
  File: Daylight-David Kushner.mp3
 ████████████████████ 100% | 3500000/3500000 bytes | ETA: 0s

Downloading 2/3:
  File: Blue and White Porcelain-Jay Chou.mp3
 ████████████████░░░░ 80% | 2800000/3500000 bytes | ETA: 2s

All downloads finished!

clear

Clear the download queue.

declare class 
class DownloadManager
DownloadManager
{
DownloadManager.clear(): void
clear
(): void;
}

getCount

Get the number of tasks in the queue.

declare class 
class DownloadManager
DownloadManager
{
DownloadManager.getCount(): number
getCount
(): number;
}

Complete Examples

Download a Single Song

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: JSON

An 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): any

Converts 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
();

Batch Download a Playlist

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: JSON

An 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): any

Converts 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[]): void

The console.log() static method outputs a message to the console.

MDN Reference

log
(`Preparing to download ${
const download: DownloadManager
download
.
DownloadManager.getCount(): number
getCount
()} songs`);
await
const download: DownloadManager
download
.
DownloadManager.startAll(): Promise<void>
startAll
();

Using Dynamic URLs

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: JSON

An 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): any

Converts 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'));
// Use an async function to dynamically get the URL
const download: DownloadManager
download
.
DownloadManager.add(url: string | (() => Promise<string>), output: string): void
add
(async () => {
const
const urlData: any
urlData
=
var JSON: JSON

An 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): any

Converts 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
();

Notes

  • Download tasks are executed sequentially in the order they were added
  • Illegal characters in filenames are automatically replaced with underscores
  • Output directories are created automatically if they don't exist
  • Progress bars are only displayed when the server returns a Content-Length header