netease-music-downloader
Version:
Download music from NetEase Cloud Music
132 lines (131 loc) • 5.55 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const download_1 = require("./commands/download");
const album_1 = require("./commands/album");
const lyrics_1 = require("./commands/lyrics");
const netease_1 = require("./services/netease");
const fs = __importStar(require("fs"));
commander_1.program
.name('netease-downloader')
.description('网易云音乐下载工具 NetEase Cloud Music Downloader')
.version('1.0.0')
.option('-p, --proxy <url>', '设置代理服务器 Set proxy server (e.g. http://127.0.0.1:7890)')
.option('-a, --auto-proxy', '当直连失败时自动寻找可用的中国代理服务器 Auto find available Chinese proxy server when direct connection fails')
.hook('preAction', async (thisCommand) => {
const options = thisCommand.opts();
if (options.proxy) {
(0, netease_1.setProxy)(options.proxy);
}
});
commander_1.program
.command('download')
.description('下载单个或多个音乐 Download single or multiple songs')
.argument('[ids...]', '音乐ID列表 List of music IDs')
.option('-f, --file <file>', '从文件读取ID列表 Read ID list from file')
.action(async (ids, options) => {
let musicIds = ids || [];
if (options.file) {
try {
const fileContent = fs.readFileSync(options.file, 'utf8');
const fileIds = fileContent.split('\n')
.map(line => line.trim())
.filter(line => line && !line.startsWith('#'));
musicIds = [...musicIds, ...fileIds];
}
catch (error) {
console.error('读取文件失败 Failed to read file:', error);
process.exit(1);
}
}
musicIds = [...new Set(musicIds)];
if (musicIds.length === 0) {
console.error('请提供音乐ID Please provide music ID(s)');
process.exit(1);
}
console.log(`准备下载 Preparing to download ${musicIds.length} 首歌曲 songs`);
for (const id of musicIds) {
await (0, download_1.downloadSong)(id, undefined, { autoProxy: commander_1.program.opts().autoProxy });
}
console.log('\n所有下载任务完成!All download tasks completed!');
});
commander_1.program
.command('album')
.description('下载整张专辑 Download full album')
.argument('<albumId>', '专辑ID或URL Album ID or URL')
.action(async (albumId) => {
await (0, album_1.downloadAlbum)(albumId, undefined, { autoProxy: commander_1.program.opts().autoProxy });
});
commander_1.program
.command('lyrics')
.description('下载单个或多个音乐的歌词 Download lyrics for single or multiple songs')
.argument('[ids...]', '音乐ID列表 List of music IDs')
.option('-f, --file <file>', '从文件读取ID列表 Read ID list from file')
.action(async (ids, options) => {
let musicIds = ids || [];
if (options.file) {
try {
const fileContent = fs.readFileSync(options.file, 'utf8');
const fileIds = fileContent.split('\n')
.map(line => line.trim())
.filter(line => line && !line.startsWith('#'));
musicIds = [...musicIds, ...fileIds];
}
catch (error) {
console.error('读取文件失败 Failed to read file:', error);
process.exit(1);
}
}
musicIds = [...new Set(musicIds)];
if (musicIds.length === 0) {
console.error('请提供音乐ID Please provide music ID(s)');
process.exit(1);
}
console.log(`准备下载 Preparing to download lyrics for ${musicIds.length} 首歌曲 songs`);
for (const id of musicIds) {
await (0, lyrics_1.downloadSongLyrics)(id);
}
console.log('\n所有歌词下载任务完成!All lyrics download tasks completed!');
});
commander_1.program
.command('album-lyrics')
.description('下载整张专辑的歌词 Download lyrics for full album')
.argument('<albumId>', '专辑ID或URL Album ID or URL')
.action(async (albumId) => {
await (0, lyrics_1.downloadAlbumLyrics)(albumId);
});
commander_1.program.parse();