// 引入modules const fs = require('fs'); const path = require('path'); const dbUtils = require(global.useMysqlPool ? '../utils/dbPoolUtils' : '../utils/dbUtils'); const sleepUtils = require('../utils/sleepUtils'); // 数据库连接池 dbUtils.create({ database: "neteaseMusic", // 指定数据库 connectionLimit: global.connectionLimit || 10, // 设置数据库连接池数量 }); global.dbUtils = dbUtils; console.log("global.useMysqlPool:", !!global.useMysqlPool); // 两次请求之间停顿时间 global.sleepTime = 300; // 引入utils const songInfoUtils = require('./src/getInfo/songInfoUtils'); const artistInfoUtils = require('./src/getInfo/artistInfoUtils'); const albumInfoUtils = require('./src/getInfo/albumInfoUtils'); const lyricInfoUtils = require('./src/getInfo/lyricInfoUtils'); const commentUtils = require('./src/getInfo/commentUtils'); /** * 测试 */ async function test() { console.log("neteaseMusic test..."); // 不是所有歌手都有个人主页 例如 https://music.163.com/#/artist?id=1079075 // let res = await albumInfoUtils.fetch({ albumId: "9156", debug: true }); // let res = await artistInfoUtils.fetch({ artistId: "12023508" }); // let res = await songInfoUtils.fetch({ songId: "437608327" }); // let res = await albumInfoUtils.getFromDatabase({ albumId: "9156" }); // let res = await artistInfoUtils.getFromDatabase({ artistId: "12023508" }); // let res = await songInfoUtils.getFromDatabase({ songId: "437608327" }); console.log(res); } /** * 主函数 */ async function main() { console.log("neteaseMusic Start fetch ..."); while (true) { // // 删除脏数据 // var affectedRows1 = await dbUtils.query(`DELETE FROM song_artist_relation WHERE song_id = 0 OR artist_id = 0`, []); // var affectedRows2 = await dbUtils.query(`DELETE FROM song_album_relation WHERE song_id = 0 OR album_id = 0`, []); // console.log(`删除脏数据 affectedRows:`, affectedRows1.affectedRows, affectedRows2.affectedRows); await songInfoUtils.fetchAll(); await albumInfoUtils.fetchAll({}); await artistInfoUtils.fetchAll(); await lyricInfoUtils.fetchAll(); await sleepUtils.sleep(2000); } } /** * 数据更新 (重新爬取) */ async function update() { console.log("neteaseMusic Start update ..."); while (true) { await albumInfoUtils.fetchAll({ isUpdate: true }); await sleepUtils.sleep(2000); } } /** * 统计数据库中数据 */ async function watch() { let sql = ` SELECT song_count, song_waiting_1 + song_waiting_2 as song_waiting, album_count, album_waiting, artist_count, artist_waiting, lyric_count, lyric_waiting, song_album_count, song_artist_count FROM ( SELECT count(*) AS song_count FROM song ) t_song, ( SELECT count( DISTINCT song_id ) as song_waiting_1 FROM song_artist_relation WHERE song_id NOT IN ( SELECT DISTINCT song_id FROM song ) ) t_song_waiting_song_artist, ( SELECT count( DISTINCT song_id ) as song_waiting_2 FROM song_album_relation WHERE song_id NOT IN ( SELECT DISTINCT song_id FROM song ) ) t_song_waiting_song_album, ( SELECT count(*) AS album_count FROM album ) t_album, ( SELECT count( DISTINCT album_id ) as album_waiting FROM song_album_relation WHERE album_id NOT IN ( SELECT DISTINCT album_id FROM album ) ) as t_album_waiting_song_album, ( SELECT count(*) AS artist_count FROM artist ) t_artist, ( SELECT count( DISTINCT artist_id ) as artist_waiting FROM song_artist_relation WHERE artist_id NOT IN ( SELECT DISTINCT artist_id FROM artist ) ) as t_album_waiting_song_artist, ( SELECT count(*) AS lyric_count FROM lyric ) t_lyric, ( SELECT count( DISTINCT song_id ) as lyric_waiting FROM song WHERE song_id NOT IN ( SELECT DISTINCT song_id FROM lyric ) ) as t_lyric_waiting_song, ( SELECT count(*) AS song_album_count FROM song_album_relation ) t_song_album, ( SELECT count(*) AS song_artist_count FROM song_artist_relation ) t_song_artist `; let result = await dbUtils.query(sql, []); let songCount = result[0].song_count; let songWaiting = result[0].song_waiting; let albumCount = result[0].album_count; let albumWaiting = result[0].album_waiting; let artistCount = result[0].artist_count; let artistWaiting = result[0].artist_waiting; let lyricCount = result[0].lyric_count; let lyricWaiting = result[0].lyric_waiting; let songAlbumCount = result[0].song_album_count; let songArtistCount = result[0].song_artist_count; let statisticsString = [ `song: ${songCount}/${songCount + songWaiting}`, `album: ${albumCount}/${albumCount + albumWaiting}`, `artist: ${artistCount}/${artistCount + artistWaiting}`, `lyric: ${lyricCount}/${lyricCount + lyricWaiting}`, `songAlbum: ${songAlbumCount}`, `songArtist: ${songArtistCount}` ].join(', '); console.log(statisticsString); } /** * 退出程序 */ global.checkIsExit = async function () { if (fs.readFileSync('stop.txt') != "1") return; console.log(); console.log(`收到退出指令,准备退出...`); await sleepUtils.sleep(500); await dbUtils.close(); console.log(`数据库连接池已关闭`); await sleepUtils.sleep(100); process.exit(0); } module.exports = { main: main, update: update, watch: watch, test: test, }