1
0
Code Issues Pull Requests Projects Releases Wiki Activity GitHub Gitee
tools/netease_music/index.js

128 lines
4.9 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const dbUtils = require('../utils/dbUtils');
const requestUtils = require('../utils/requestUtils');
const sleepUtils = require('../utils/sleepUtils');
const songInfoUtils = require('./src/getInfo/songInfoUtils');
const artistInfoUtils = require('./src/getInfo/artistInfoUtils');
const albumInfoUtils = require('./src/getInfo/albumInfoUtils');
async function main() {
console.log("neteaseMusic Starting...");
// 指定数据库
dbUtils.create("neteaseMusic");
// getMusicInfo({ songId: "1855221507" });
// getArtistInfo({ artistId: "1079074" });
// getAlbumInfo({ albumId: "74268047" });
// 不是所有歌手都有个人主页 例如 https://music.163.com/#/artist?id=1079075
// getUserInfo({ userId: "37365202" });
while (true) {
// 删除脏数据
var affectRows1 = await dbUtils.query(`DELETE FROM song_artist_relation WHERE song_id = 0 OR artist_id = 0`, []);
var affectRows2 = await dbUtils.query(`DELETE FROM song_album_relation WHERE song_id = 0 OR album_id = 0`, []);
console.log(affectRows1.affectedRows, affectRows2.affectedRows);
await startGetMusic(1);
await sleepUtils.sleep(2000);
}
}
async function startGetMusic(sleepTime) {
// 从数据库中查出还缺少的歌曲,并进行爬取
console.log("start fetching songs ...");
let songIds = await dbUtils.query(`
SELECT DISTINCT song_id FROM song_artist_relation WHERE song_id NOT IN ( SELECT DISTINCT song_id FROM song )
UNION
SELECT DISTINCT song_id FROM song_album_relation WHERE song_id NOT IN ( SELECT DISTINCT song_id FROM song )
`, []);
songIds = songIds.map(item => item.song_id);
for (let i = 0; i < songIds.length; i++) {
const songId = songIds[i];
console.log(`${i}/${songIds.length} | song: ${songId} | ${await statistics()}`);
try {
await songInfoUtils.get({ songId: songId });
} catch (err) {
console.error(err);
}
await sleepUtils.sleep(sleepTime);
if (fs.readFileSync('stop.txt') == "1") {
throw new Error(`Stopped`);
}
}
// 从数据库中查出还缺少的专辑,并进行爬取
console.log("start fetching albums ...")
let albumIds = await dbUtils.query(`
SELECT DISTINCT album_id FROM song_album_relation WHERE album_id NOT IN ( SELECT DISTINCT album_id FROM album )
`, []);
albumIds = albumIds.map(item => item.album_id);
for (let i = 0; i < albumIds.length; i++) {
const albumId = albumIds[i];
console.log(`${i}/${albumIds.length} | album: ${albumId} | ${await statistics()}`);
try {
await albumInfoUtils.get({ albumId: albumId });
} catch (err) {
console.error(err);
}
await sleepUtils.sleep(sleepTime);
if (fs.readFileSync('stop.txt') == "1") {
throw new Error(`Stopped`);
}
}
// 从数据库中查出还缺少的歌手,并进行爬取
console.log("start fetching albums ...")
let artistIds = await dbUtils.query(`
SELECT DISTINCT artist_id FROM song_artist_relation WHERE artist_id NOT IN ( SELECT DISTINCT artist_id FROM artist )
`, []);
artistIds = artistIds.map(item => item.artist_id);
for (let i = 0; i < artistIds.length; i++) {
const artistId = artistIds[i];
console.log(`${i}/${artistIds.length} | artist: ${artistId} | ${await statistics()}`);
try {
await artistInfoUtils.get({ artistId: artistId });
} catch (err) {
console.error(err);
}
await sleepUtils.sleep(sleepTime);
if (fs.readFileSync('stop.txt') == "1") {
throw new Error(`Stopped`);
}
}
}
async function statistics() {
let sql = `
SELECT
song_count,
album_count,
artist_count,
song_album_count,
song_artist_count
FROM
( SELECT count(*) AS song_count FROM song ) t1,
( SELECT count(*) AS album_count FROM album ) t2,
( SELECT count(*) AS artist_count FROM artist ) t3,
( SELECT count(*) AS song_album_count FROM song_album_relation ) t4,
( SELECT count(*) AS song_artist_count FROM song_artist_relation ) t5`;
let result = await dbUtils.query(sql, []);
let songCount = result[0].song_count;
let albumCount = result[0].album_count;
let artistCount = result[0].artist_count;
let songAlbumCount = result[0].song_album_count;
let songArtistCount = result[0].song_artist_count;
return `song: ${songCount}, album: ${albumCount}, artist: ${artistCount} | songAlbum: ${songAlbumCount}, songArtist: ${songArtistCount}`;
}
module.exports = {
main: main,
}