1
0
Code Issues Pull Requests Projects Releases Wiki Activity GitHub Gitee
Files
tools/netease_music/src/getInfo/lyricInfoUtils.js

83 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const fs = require('fs');
const path = require('path');
const requestUtils = require('../../../utils/requestUtils');
const sleepUtils = require('../../../utils/sleepUtils');
const dataManager = require('../dataManager');
const dbUtils = global.dbUtils;
// 从数据库中查出还缺少的歌词,并进行爬取
async function fetchAll({ args = {} }) {
console.log("start fetching lyrics ...");
let songIds = await dataManager.lyric.getIdsToFetch(args);
console.log(`songIds was fetched, count: ${songIds.length}`);
for (let i = 0; i < songIds.length; i++) {
await global.checkIsExit();
const songId = songIds[i];
console.log(`${i + 1}/${songIds.length} | lyric: ${songId} | ${args.min || "?"}-${args.max || "?"}`);
try {
await fetch({ songId: songId });
} catch (err) {
console.error(err);
}
await sleepUtils.sleep(global.sleepTime);
}
}
// 获取歌词详情
async function fetch({ songId, debug = false }) {
let result = await dbUtils.query('SELECT count(*) as count FROM lyric WHERE song_id = ?', [songId]);
if (result[0].count > 0 && !debug) {
// 这里暂时跳过后期可能要考虑歌词version更新的问题
console.log(`数据库中已有数据,跳过 songId: ${songId}`);
// 从待爬取表中删除记录
await dataManager.wait_fetch.deleteCollection("lyric", [songId]);
return;
}
var url = `https://music.163.com/api/song/lyric?id=${songId}&lv=1`; // &kv=1&tv=-1
try {
// var json = fs.readFileSync(path.join(__dirname, "../../temp", `lyric-${songId}.json`), 'utf8');
var json = await requestUtils.getApiResult(url);
// fs.writeFileSync(path.join(__dirname, "../../temp", `lyric-${songId}.json`), json);
} catch (errors) {
console.error(errors);
return;
}
try {
var lyric = JSON.parse(json).lrc; // { version: xx, lyric: 'xxx' }
} catch (error) {
console.error(error);
return;
}
if (typeof lyric == "undefined") {
// 这首歌爬song的时候还在但是现在不在了
await dataManager.lyric.insert({
song_id: songId,
lyric: '',
version: -1,
});
return;
}
let lyricInfo = {
song_id: songId,
lyric: lyric.lyric,
version: lyric.version,
};
// console.log("lyricInfo", lyricInfo);
// 插入数据
await dataManager.lyric.insert(lyricInfo);
// 从待爬取表中删除记录
await dataManager.wait_fetch.deleteCollection("lyric", [songId]);
}
module.exports = {
fetch: fetch,
fetchAll: fetchAll,
}