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,106 @@
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({ songId }) {
let result = await dbUtils.query('SELECT count(*) as count FROM song WHERE song_id = ?', [songId]);
if (result[0].count > 0) {
console.log(`数据库中已有数据,跳过 songId: ${songId}`);
return;
// let songResult = await dbUtils.query('SELECT * FROM song WHERE song_id = ?', [songId]);
// songResult = JSON.parse(JSON.stringify(songResult));
// let songArtistResult = await dbUtils.query('SELECT * FROM song_artist_relation WHERE song_id = ?', [songId]);
// songArtistResult = JSON.parse(JSON.stringify(songArtistResult));
// songResult.artistIds = songArtistResult.map(song => song.artist_id);
// let songAlbumResult = await dbUtils.query('SELECT * FROM song_album_relation WHERE song_id = ?', [songId]);
// songAlbumResult = JSON.parse(JSON.stringify(songAlbumResult));
// songResult.albumId = songAlbumResult.map(song => song.album_id)[0];
// // console.log(songResult);
// return songResult;
}
let url = `https://music.163.com/song?id=${songId}`;
try {
// var html = fs.readFileSync(path.join(__dirname, "../temp", `song-${songId}.html`), 'utf8');
var html = await requestUtils.getApiResult(url);
// fs.writeFileSync(path.join(__dirname, "../temp", `song-${songId}.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 songInfoJSONString = regExResult[1];
let songInfoDict = JSON.parse(songInfoJSONString);
// console.log(songInfoDict);
let title = /<meta property="og:title" content="(.*?)" \/>/.exec(html)[1];
let image = /<meta property="og:image" content="http:\/\/p.\.music\.126\.net\/(.*?)" \/>/.exec(html)[1];
let artist = /<meta property="og:music:artist" content="(.*?)" \/>/.exec(html)[1];
let duration = /<meta property="music:duration" content="(.*?)"\/>/.exec(html)[1];
try {
var album = /<meta property="og:music:album" content="(.*?)"\/>/.exec(html)[1];
var albumId = /<meta property="music:album" content="https:\/\/music\.163\.com\/album\?id=(.*?)"\/>/.exec(html)[1];
} catch (err) {
// 歌曲不在专辑中
}
const reg = /<meta property="music:musician" content="https:\/\/music\.163\.com\/artist\?id=(.*?)"\/>/g;
let artistIds = [];
let matched = null;
while ((matched = reg.exec(html)) !== null) {
artistIds.push(matched[1]);
}
let songInfo = {
songId: songId,
title: title,
image: image,
pubDate: songInfoDict.pubDate,
artist: artist,
artistIds: artistIds,
album: album || null,
albumId: albumId || null,
duration: duration,
};
// console.log("songInfo", songInfo);
dbUtils.query('INSERT IGNORE INTO song SET ?', {
song_id: songInfo.songId,
title: songInfo.title,
image: songInfo.image,
pub_date: songInfo.pubDate,
});
if (albumId != null)
dbUtils.query('INSERT IGNORE INTO song_album_relation SET ?', {
song_id: songInfo.songId,
album_id: songInfo.albumId,
});
artistIds.forEach(function (artistId) {
dbUtils.query('INSERT IGNORE INTO song_artist_relation SET ?', {
song_id: songInfo.songId,
artist_id: artistId,
});
});
return songInfo;
}
module.exports = {
get: get,
}