1
0
Code Issues Pull Requests Projects Releases Wiki Activity GitHub Gitee

fetchAll从index.js提到各个utils中

This commit is contained in:
2022-10-01 22:00:29 +08:00
parent 5d2bfccb4b
commit 3dcb71b5a3
6 changed files with 196 additions and 110 deletions

View File

@@ -2,11 +2,32 @@ const fs = require('fs');
const path = require('path');
const requestUtils = require('../../../utils/requestUtils');
const sleepUtils = require('../../../utils/sleepUtils');
const dbUtils = global.dbUtils;
// 从数据库中查出还缺少的歌词,并进行爬取
async function fetchAll() {
console.log("start fetching lyrics ...");
var songIds = await dbUtils.query(`
SELECT DISTINCT song_id FROM song WHERE song_id NOT IN ( SELECT DISTINCT song_id FROM lyric )
`, []);
songIds = songIds.map(song => song.song_id);
for (let i = 0; i < songIds.length; i++) {
await global.checkIsExit();
const songId = songIds[i];
console.log(`${i}/${songIds.length} | lyric: ${songId} | ${await global.statistics()}`);
try {
await fetch({ songId: songId });
} catch (err) {
console.error(err);
}
await sleepUtils.sleep(global.sleepTime);
}
}
// 获取歌词详情
async function fetch({ songId }) {
async function fetch({ songId, debug = false }) {
var url = `https://music.163.com/api/song/lyric?id=${songId}&lv=1`;
try {
@@ -41,4 +62,5 @@ async function fetch({ songId }) {
module.exports = {
fetch: fetch,
fetchAll: fetchAll,
}