diff --git a/index.js b/index.js index e5e8ed3..edaa27f 100644 --- a/index.js +++ b/index.js @@ -1,2 +1,5 @@ -const dbUtils = require('./utils/dbUtils'); -const dbPoolUtils = require('./utils/dbPoolUtils'); \ No newline at end of file +// const dbUtils = require('./utils/dbUtils'); +// const dbPoolUtils = require('./utils/dbPoolUtils'); + +const neteaseMusic = require('./netease_music/index'); +neteaseMusic.main(); \ No newline at end of file diff --git a/netease-music/index.js b/netease-music/index.js deleted file mode 100644 index e69de29..0000000 diff --git a/netease_music/index.js b/netease_music/index.js new file mode 100644 index 0000000..89c124e --- /dev/null +++ b/netease_music/index.js @@ -0,0 +1,260 @@ +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 }) { + await sleepUtils.sleep(500); + 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}`); + // return; + // } + + 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 = /\