UNPKG

netease-music-downloader

Version:
130 lines (129 loc) 5.94 kB
"use strict"; 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 }); exports.downloadSongLyrics = downloadSongLyrics; exports.downloadAlbumLyrics = downloadAlbumLyrics; const netease_1 = require("../services/netease"); const file_1 = require("../utils/file"); const fs = __importStar(require("fs")); /** * Download lyrics for a single song */ async function downloadSongLyrics(id) { try { // Extract ID from URL if needed if (id.includes('music.163.com')) { const match = id.match(/id=(\d+)/); if (!match) { console.error('无效的歌曲URL Invalid song URL'); process.exit(1); } id = match[1]; } // Get song info const song = await (0, netease_1.getSongInfo)(id); const songName = song.name; const artistName = song.artists?.[0]?.name || '未知歌手 Unknown Artist'; console.log(`\n歌曲信息 Song info: ${artistName}-${songName}`); // Download lyrics const lyrics = await (0, netease_1.getLyrics)(id); if (!lyrics) { console.log('该歌曲无歌词 No lyrics available for this song'); return; } // Save lyrics const sanitizedSongName = (0, file_1.sanitizeFileName)(songName); const sanitizedArtistName = (0, file_1.sanitizeFileName)(artistName); const fileName = `${sanitizedArtistName}-${sanitizedSongName}.lrc`; const lrcPath = (0, file_1.getDownloadPath)('single', fileName); fs.writeFileSync(lrcPath, lyrics, 'utf8'); console.log('歌词下载完成 Lyrics downloaded'); console.log('保存路径 Save path:', lrcPath); console.log('网页链接 Web URL:', `https://music.163.com/#/song?id=${id}`); } catch (error) { console.error('下载歌词失败 Failed to download lyrics:', error instanceof Error ? error.message : 'Unknown error'); process.exit(1); } } /** * Download lyrics for all songs in an album */ async function downloadAlbumLyrics(albumId) { try { // Extract ID from URL if needed if (albumId.includes('music.163.com')) { const match = albumId.match(/id=(\d+)/); if (!match) { console.error('无效的专辑URL Invalid album URL'); process.exit(1); } albumId = match[1]; } // Get album info const albumInfo = await (0, netease_1.getAlbumInfo)(albumId); const { songs, albumName, artistName } = albumInfo; console.log(`\n专辑信息 Album info: ${albumName} - ${artistName}`); console.log(`共 Total: ${songs.length} 首歌曲 songs\n`); const sanitizedAlbumName = (0, file_1.sanitizeFileName)(albumName); const sanitizedArtistName = (0, file_1.sanitizeFileName)(artistName); const albumDirName = `${sanitizedArtistName}-${sanitizedAlbumName}`; // Download lyrics for each song for (let i = 0; i < songs.length; i++) { const song = songs[i]; const songName = song.name; const songArtistName = song.artists?.[0]?.name || '未知歌手 Unknown Artist'; const displayName = `${songArtistName}-${songName}`; console.log(`\n[${i + 1}/${songs.length}] 正在获取歌词 Getting lyrics: ${displayName}`); const lyrics = await (0, netease_1.getLyrics)(song.id); if (!lyrics) { console.log(`[${i + 1}/${songs.length}] 该歌曲无歌词 No lyrics available for this song`); continue; } const sanitizedSongName = (0, file_1.sanitizeFileName)(songName); const sanitizedSongArtistName = (0, file_1.sanitizeFileName)(songArtistName); const fileName = `${String(i + 1).padStart(2, '0')}.${sanitizedSongArtistName}-${sanitizedSongName}.lrc`; const lrcPath = (0, file_1.getDownloadPath)('album', fileName, albumDirName); fs.writeFileSync(lrcPath, lyrics, 'utf8'); console.log(`[${i + 1}/${songs.length}] 歌词下载完成 Lyrics downloaded`); console.log(`[${i + 1}/${songs.length}] 网页链接 Web URL: https://music.163.com/#/song?id=${song.id}`); } console.log('\n专辑歌词下载完成!Album lyrics download completed!'); } catch (error) { console.error('下载专辑歌词失败 Failed to download album lyrics:', error instanceof Error ? error.message : 'Unknown error'); process.exit(1); } }