const fs = require('fs'); const path = require('path'); const dbUtils = require('../utils/dbUtils'); const requestUtils = require('../utils/requestUtils'); const sleepUtils = require('../utils/sleepUtils'); async function main() { console.log("neteaseMusic Starting..."); // 指定数据库 dbUtils.create("neteaseMusic"); // getMusicInfo({ songId: "1855221507" }); // getMusicInfo({ songId: "1855221517" }); // getMusicInfo({ songId: "1861632812" }); // getArtistInfo({ artistId: "1079074" }); // getArtistInfo({ artistId: "1079075" }); // getAlbumInfo({ albumId: "74268047" }); // getAlbumInfo({ albumId: "129327797" }); // 不是所有歌手都有个人主页 例如 https://music.163.com/#/artist?id=1079075 // getUserInfo({ userId: "37365202" }); // getUserInfo({ userId: "29879272" }); await startGetMusic({ songId: "1966061035" }); } async function startGetMusic({ songId }) { var songInfo = await getMusicInfo({ songId: songId }); var albumInfo = await getAlbumInfo({ albumId: songInfo.albumId }); if (albumInfo) { for (var songId of albumInfo.songIds) { await startGetMusic({ songId: songId }); } } for (var artistId of songInfo.artistIds) { var artistInfo = await getArtistInfo({ artistId: artistId }); if (artistInfo) { for (var songId of artistInfo.songIds) { await startGetMusic({ songId: songId }); } } } } // 获取音乐详情 async function getMusicInfo({ songId }) { console.log(`开始处理 song: ${songId}`); let result = await dbUtils.query('SELECT count(*) as count FROM song WHERE song_id = ?', [songId]); if (result[0].count > 0) { console.log(`数据库中已有数据,跳过 songId: ${songId}`); let songResult = await dbUtils.query('SELECT * FROM song WHERE song_id = ?', [songId]); songResult = JSON.parse(JSON.stringify(songResult)); let songArtistResult = await dbUtils.query('SELECT * FROM song_artist_relation WHERE song_id = ?', [songId]); songArtistResult = JSON.parse(JSON.stringify(songArtistResult)); songResult.artistIds = songArtistResult.map(song => song.artist_id); let songAlbumResult = await dbUtils.query('SELECT * FROM song_album_relation WHERE song_id = ?', [songId]); songAlbumResult = JSON.parse(JSON.stringify(songAlbumResult)); songResult.albumId = songAlbumResult.map(song => song.album_id)[0]; // console.log(songResult); return songResult; } await sleepUtils.sleep(500); let url = `https://music.163.com/song?id=${songId}`; try { throw new Error(`Error`); var html = fs.readFileSync(path.join(__dirname, "../temp", `song-${songId}.html`), 'utf8'); } catch (errors) { var html = await requestUtils.getApiResult(url); fs.writeFileSync(path.join(__dirname, "../temp", `song-${songId}.html`), html); } // console.log(html); // 正则匹配 let regExResult = /\