From ba395bac47cf2dcf7f74d9cd059d926415c6f412 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Sat, 1 Oct 2022 20:35:42 +0800 Subject: [PATCH] update --- index.js | 1 + netease_music/index.js | 6 ++--- netease_music/src/getInfo/albumInfoUtils.js | 17 ++++++++++++- netease_music/src/getInfo/artistInfoUtils.js | 26 ++++++++++++-------- netease_music/src/getInfo/songInfoUtils.js | 20 +++++++++++++-- test.js | 1 + update.js | 1 + 7 files changed, 56 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index e280a06..f176bbf 100644 --- a/index.js +++ b/index.js @@ -1,2 +1,3 @@ +global.useMysqlPool = true; 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 index c621ad0..eba2841 100644 --- a/netease_music/index.js +++ b/netease_music/index.js @@ -1,7 +1,6 @@ const fs = require('fs'); const path = require('path'); -// const dbUtils = require('../utils/dbUtils'); -const dbUtils = require('../utils/dbPoolUtils'); +const dbUtils = require(global.useMysqlPool ? '../utils/dbPoolUtils' : '../utils/dbUtils'); const requestUtils = require('../utils/requestUtils'); const sleepUtils = require('../utils/sleepUtils'); @@ -10,12 +9,13 @@ dbUtils.create({ database: "neteaseMusic", // 指定数据库 connectionLimit: 8, // 设置数据库连接池数量 }); - global.dbUtils = dbUtils; + const songInfoUtils = require('./src/getInfo/songInfoUtils'); const artistInfoUtils = require('./src/getInfo/artistInfoUtils'); const albumInfoUtils = require('./src/getInfo/albumInfoUtils'); +console.log("global.useMysqlPool:", !!global.useMysqlPool); // 退出检查 async function checkIsExit() { diff --git a/netease_music/src/getInfo/albumInfoUtils.js b/netease_music/src/getInfo/albumInfoUtils.js index 79d0901..b57e96b 100644 --- a/netease_music/src/getInfo/albumInfoUtils.js +++ b/netease_music/src/getInfo/albumInfoUtils.js @@ -2,10 +2,24 @@ const fs = require('fs'); const path = require('path'); const requestUtils = require('../../../utils/requestUtils'); -const sleepUtils = require('../../../utils/sleepUtils'); const dbUtils = global.dbUtils; +// 从数据库中查询 +async function getFromDatabase({ albumId }) { + // 查询出专辑 + let infoResultSet = await dbUtils.query('SELECT * FROM album WHERE album_id = ?', [albumId]); + if (infoResultSet.length == 0) return {}; + + // 查出专辑与歌曲对应关系 + let relationResultSet = await dbUtils.query('SELECT * FROM song_album_relation WHERE album_id = ?', [albumId]); + + // 拼装 + let albumInfo = JSON.parse(JSON.stringify(infoResultSet[0])); + albumInfo.songIds = relationResultSet.map(song => song.song_id); + return albumInfo; +} + // 获取专辑详情 async function fetch({ albumId }) { let result = await dbUtils.query('SELECT count(*) as count FROM album WHERE album_id = ?', [albumId]); @@ -170,6 +184,7 @@ async function update({ albumId }) { module.exports = { + getFromDatabase: getFromDatabase, fetch: fetch, update: update, } \ No newline at end of file diff --git a/netease_music/src/getInfo/artistInfoUtils.js b/netease_music/src/getInfo/artistInfoUtils.js index 078fe88..0afecab 100644 --- a/netease_music/src/getInfo/artistInfoUtils.js +++ b/netease_music/src/getInfo/artistInfoUtils.js @@ -2,25 +2,30 @@ const fs = require('fs'); const path = require('path'); const requestUtils = require('../../../utils/requestUtils'); -const sleepUtils = require('../../../utils/sleepUtils'); const dbUtils = global.dbUtils; +// 从数据库中查询 +async function getFromDatabase({ artistId }) { + // 查询出专辑 + let infoResultSet = await dbUtils.query('SELECT * FROM artist WHERE artist_id = ?', [artistId]); + if (infoResultSet.length == 0) return {}; + + // 查出专辑与歌曲对应关系 + let relationResultSet = await dbUtils.query('SELECT * FROM song_artist_relation WHERE artist_id = ?', [artistId]); + + // 拼装 + let artistInfo = JSON.parse(JSON.stringify(infoResultSet[0])); + artistInfo.songIds = relationResultSet.map(song => song.song_id); + return artistInfo; +} + // 获取音乐人详情 async function fetch({ artistId }) { let result = await dbUtils.query('SELECT count(*) as count FROM artist WHERE artist_id = ?', [artistId]); if (result[0].count > 0) { console.log(`数据库中已有数据,跳过 artistId: ${artistId}`); return; - - // // let artistResult = await dbUtils.query('SELECT * FROM artist LEFT JOIN song_artist_relation ON artist.artist_id = song_artist_relation.artist_id WHERE artist.artist_id = ?', [artistId]); - // let artistResult = await dbUtils.query('SELECT * FROM artist WHERE artist_id = ?', [artistId]); - // artistResult = JSON.parse(JSON.stringify(artistResult)); - // let songArtistResult = await dbUtils.query('SELECT * FROM song_artist_relation WHERE artist_id = ?', [artistId]); - // songArtistResult = JSON.parse(JSON.stringify(songArtistResult)); - // artistResult.songIds = songArtistResult.map(song => song.song_id); - // // console.log(artistResult); - // return artistResult; } let url = `https://music.163.com/artist?id=${artistId}`; @@ -78,5 +83,6 @@ async function fetch({ artistId }) { } module.exports = { + getFromDatabase: getFromDatabase, fetch: fetch, } \ No newline at end of file diff --git a/netease_music/src/getInfo/songInfoUtils.js b/netease_music/src/getInfo/songInfoUtils.js index 0048647..7b68ddf 100644 --- a/netease_music/src/getInfo/songInfoUtils.js +++ b/netease_music/src/getInfo/songInfoUtils.js @@ -2,10 +2,26 @@ const fs = require('fs'); const path = require('path'); const requestUtils = require('../../../utils/requestUtils'); -const sleepUtils = require('../../../utils/sleepUtils'); const dbUtils = global.dbUtils; +// 从数据库中查询 +async function getFromDatabase({ songId }) { + // 查询出专辑 + let infoResultSet = await dbUtils.query('SELECT * FROM song WHERE song_id = ?', [songId]); + if (infoResultSet.length == 0) return {}; + + // 查出专辑与歌曲对应关系 + let albumRelationResultSet = await dbUtils.query('SELECT * FROM song_album_relation WHERE song_id = ?', [songId]); + let artistRelationResultSet = await dbUtils.query('SELECT * FROM song_artist_relation WHERE song_id = ?', [songId]); + + // 拼装 + let songInfo = JSON.parse(JSON.stringify(infoResultSet[0])); + songInfo.albumIds = albumRelationResultSet.map(album => album.album_id); + songInfo.artistIds = artistRelationResultSet.map(artist => artist.artist_id); + return songInfo; +} + // 获取音乐详情 async function fetch({ songId }) { let result = await dbUtils.query('SELECT count(*) as count FROM song WHERE song_id = ?', [songId]); @@ -61,7 +77,6 @@ async function fetch({ songId }) { // 歌曲不在专辑中 } - const reg = //g; let artistIds = []; let matched = null; @@ -102,5 +117,6 @@ async function fetch({ songId }) { } module.exports = { + getFromDatabase: getFromDatabase, fetch: fetch, } \ No newline at end of file diff --git a/test.js b/test.js index 0305a83..61b0249 100644 --- a/test.js +++ b/test.js @@ -1,2 +1,3 @@ +global.useMysqlPool = false; const neteaseMusic = require('./netease_music/index'); neteaseMusic.test(); \ No newline at end of file diff --git a/update.js b/update.js index 65360b2..57b87c6 100644 --- a/update.js +++ b/update.js @@ -1,2 +1,3 @@ +global.useMysqlPool = true; const neteaseMusic = require('./netease_music/index'); neteaseMusic.update(); \ No newline at end of file