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

代码拆分出Utils

This commit is contained in:
2022-10-01 11:41:20 +08:00
parent 7004fe8858
commit 37fe49c53c
5 changed files with 325 additions and 257 deletions

View File

@@ -0,0 +1,82 @@
const fs = require('fs');
const path = require('path');
const dbUtils = require('../../../utils/dbUtils');
const requestUtils = require('../../../utils/requestUtils');
const sleepUtils = require('../../../utils/sleepUtils');
// 获取音乐人详情
async function get({ 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}`;
try {
// var html = fs.readFileSync(path.join(__dirname, "../temp", `artist-${artistId}.html`), 'utf8');
var html = await requestUtils.getApiResult(url);
// fs.writeFileSync(path.join(__dirname, "../temp", `artist-${artistId}.html`), html);
} catch (errors) {
console.error(errors);
return;
}
// console.log(html);
if (html.includes(`<p class="note s-fc3">很抱歉,你要查找的网页找不到</p>`)) {
// TODO 最后统一来处理这里 demo: artistId == 30084536
return;
}
// 正则匹配
let regExResult = /\<script type\=\"application\/ld\+json\"\>([\S\s]*?)\<\/script\>/.exec(html);
let artistInfoJSONString = regExResult[1];
let artistInfoDict = JSON.parse(artistInfoJSONString);
// console.log(artistInfoDict);
let image = /<meta property="og:image" content="http:\/\/p.\.music\.126\.net\/(.*?)" \/>/.exec(html)[1];
let songListJSONString = /<textarea id="song-list-pre-data" style="display:none;">(.*?)<\/textarea>/.exec(html)[1];
let songList = JSON.parse(songListJSONString);
let songIds = songList.map(song => song.id);
let artistInfo = {
artistId: artistId,
title: artistInfoDict.title,
image: image,
description: artistInfoDict.description,
pubDate: artistInfoDict.pubDate,
songIds: songIds,
};
// console.log("artistInfo", artistInfo);
dbUtils.query('INSERT IGNORE INTO artist SET ?', {
artist_id: artistInfo.artistId,
title: artistInfo.title,
description: artistInfo.description,
image: artistInfo.image,
pub_date: artistInfo.pubDate,
});
songIds.forEach(function (songId) {
if (isNaN(Number(songId)) || Number(songId) === 0 || isNaN(Number(artistId)) || Number(artistId) === 0)
return;
dbUtils.query('INSERT IGNORE INTO song_artist_relation SET ?', {
song_id: songId,
artist_id: artistId,
});
});
return artistInfo;
}
module.exports = {
get: get,
}