89 lines
3.4 KiB
JavaScript
89 lines
3.4 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');
|
|
|
|
// 获取专辑详情
|
|
async function get({ albumId }) {
|
|
let result = await dbUtils.query('SELECT count(*) as count FROM album WHERE album_id = ?', [albumId]);
|
|
if (result[0].count > 0) {
|
|
console.log(`数据库中已有数据,跳过 albumId: ${albumId}`);
|
|
return;
|
|
|
|
// let albumResult = await dbUtils.query('SELECT * FROM album WHERE album_id = ?', [albumId]);
|
|
// albumResult = JSON.parse(JSON.stringify(albumResult));
|
|
// let songAlbumResult = await dbUtils.query('SELECT * FROM song_album_relation WHERE album_id = ?', [albumId]);
|
|
// songAlbumResult = JSON.parse(JSON.stringify(songAlbumResult));
|
|
// albumResult.songIds = songAlbumResult.map(song => song.song_id);
|
|
// // console.log(albumResult);
|
|
// return albumResult;
|
|
}
|
|
|
|
let url = `https://music.163.com/album?id=${albumId}`;
|
|
|
|
try {
|
|
// var html = fs.readFileSync(path.join(__dirname, "../temp", `album-${albumId}.html`), 'utf8');
|
|
var html = await requestUtils.getApiResult(url);
|
|
// fs.writeFileSync(path.join(__dirname, "../temp", `album-${albumId}.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 albumInfoJSONString = regExResult[1];
|
|
let albumInfoDict = JSON.parse(albumInfoJSONString);
|
|
// console.log(albumInfoDict);
|
|
|
|
let company = null;
|
|
try {
|
|
company = /<p class="intr"><b>发行公司:<\/b>\n(.*?)\n<\/p>/.exec(html)[1];
|
|
} catch (e) {
|
|
}
|
|
|
|
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 albumInfo = {
|
|
albumId: albumId,
|
|
title: albumInfoDict.title,
|
|
image: image,
|
|
description: albumInfoDict.description,
|
|
pubDate: albumInfoDict.pubDate,
|
|
company: company,
|
|
songIds: songIds,
|
|
};
|
|
// console.log("albumInfo", albumInfo);
|
|
dbUtils.query('INSERT IGNORE INTO album SET ?', {
|
|
album_id: albumInfo.albumId,
|
|
title: albumInfo.title,
|
|
description: albumInfo.description,
|
|
image: albumInfo.image,
|
|
pub_date: albumInfo.pubDate,
|
|
company: albumInfo.company,
|
|
});
|
|
songIds.forEach(function (songId) {
|
|
if (isNaN(Number(songId)) || Number(songId) === 0 || isNaN(Number(albumId)) || Number(songId) === 0)
|
|
return;
|
|
dbUtils.query('INSERT IGNORE INTO song_album_relation SET ?', {
|
|
song_id: songId,
|
|
album_id: albumId,
|
|
});
|
|
});
|
|
return albumInfo;
|
|
}
|
|
|
|
module.exports = {
|
|
get: get,
|
|
} |