1
0
Code Issues Pull Requests Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
2022-10-01 18:53:19 +08:00
parent 64834ae7f9
commit 074cbc124f
7 changed files with 168 additions and 39 deletions

View File

@@ -11,14 +11,6 @@ async function fetch({ 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}`;
@@ -31,7 +23,6 @@ async function fetch({ albumId }) {
console.error(errors);
return;
}
// console.log(html);
if (html.includes(`<p class="note s-fc3">很抱歉,你要查找的网页找不到</p>`)) {
// TODO 最后统一来处理这里 demo: artistId == 30084536
@@ -44,10 +35,40 @@ async function fetch({ albumId }) {
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) {
if (html.includes(`<p class="intr"><b>发行公司:`)) {
try {
company = /<p class="intr"><b>发行公司:<\/b>\n(.*?)\n<\/p>/.exec(html)[1];
} catch (e) {
// 解析出错
await dbUtils.query('INSERT INTO log (`id`, `name`, `msg`) VALUES (?, ?, ?)', [albumId, 'album_fetch', `company 正则失败\n${e.message}`]);
return;
}
}
// 专辑详细简介
let fullDescription = null;
if (html.includes(`<div id="album-desc-more" class="f-hide">`)) {
// 比较长 有点击展开按钮
try {
fullDescription = /<div id="album-desc-more" class="f-hide">([\S\s]*?)<\/div>/.exec(html)[1];
fullDescription = fullDescription.replace(/<p class="f-brk">\n/g, '').replace(/<\/p>\n/g, '').trim();
} catch (e) {
// 解析出错
await dbUtils.query('INSERT INTO log (`id`, `name`, `msg`) VALUES (?, ?, ?)', [albumId, 'album_fetch', `fullDescription 1 正则失败\n${e.message}`]);
return;
}
} else if (html.includes(`<div id="album-desc-dot" class="f-brk">`)) {
// 比较短 无点击展开按钮
try {
fullDescription = /<div id="album-desc-dot" class="f-brk">([\S\s]*?)<\/div>/.exec(html)[1];
fullDescription = fullDescription.replace(/<p>/g, '').replace(/<\/p>/g, '').trim();
} catch (e) {
// 解析出错
await dbUtils.query('INSERT INTO log (`id`, `name`, `msg`) VALUES (?, ?, ?)', [albumId, 'album_fetch', `fullDescription 2 正则失败\n${e.message}`]);
return;
}
}
let image = /<meta property="og:image" content="http:\/\/p.\.music\.126\.net\/(.*?)" \/>/.exec(html)[1];
@@ -60,6 +81,7 @@ async function fetch({ albumId }) {
title: albumInfoDict.title,
image: image,
description: albumInfoDict.description,
full_description: fullDescription,
pubDate: albumInfoDict.pubDate,
company: company,
songIds: songIds,
@@ -69,9 +91,11 @@ async function fetch({ albumId }) {
album_id: albumInfo.albumId,
title: albumInfo.title,
description: albumInfo.description,
full_description: albumInfo.fullDescription,
image: albumInfo.image,
pub_date: albumInfo.pubDate,
company: albumInfo.company,
version: 4
});
songIds.forEach(function (songId) {
if (isNaN(Number(songId)) || Number(songId) === 0 || isNaN(Number(albumId)) || Number(songId) === 0)
@@ -84,6 +108,67 @@ async function fetch({ albumId }) {
return albumInfo;
}
/*
v1 to v3
升级v3完毕后应该查不出记录才对
SELECT
*
FROM
album
WHERE
version = 3 and full_description is null and description like '%专辑《%》,简介:%'
*/
async function update({ 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 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;
}
if (html.includes(`<p class="note s-fc3">很抱歉,你要查找的网页找不到</p>`)) {
return;
}
// 专辑详细简介
let fullDescription = null;
if (html.includes(`<div id="album-desc-more" class="f-hide">`)) {
try {
fullDescription = /<div id="album-desc-more" class="f-hide">([\S\s]*?)<\/div>/.exec(html)[1];
fullDescription = fullDescription.replace(/<p class="f-brk">\n/g, '').replace(/<\/p>\n/g, '').trim();
} catch (e) {
// 解析出错
await dbUtils.query('INSERT INTO log (`id`, `name`, `msg`) VALUES (?, ?, ?)', [albumId, 'album_fetch', `fullDescription 3 正则失败\n${e.message}`]);
return;
}
} else if (html.includes(`<div id="album-desc-dot" class="f-brk">`)) {
try {
fullDescription = /<div id="album-desc-dot" class="f-brk">([\S\s]*?)<\/div>/.exec(html)[1];
fullDescription = fullDescription.replace(/<p>/g, '').replace(/<\/p>/g, '').trim();
} catch (e) {
// 解析出错
await dbUtils.query('INSERT INTO log (`id`, `name`, `msg`) VALUES (?, ?, ?)', [albumId, 'album_fetch', `fullDescription 4 正则失败\n${e.message}`]);
return;
}
}
await dbUtils.query('UPDATE album SET full_description = ?, version = 3 WHERE album_id = ?', [fullDescription, albumId]);
return;
}
module.exports = {
fetch: fetch,
update: update,
}

View File

@@ -32,7 +32,6 @@ async function fetch({ artistId }) {
console.error(errors);
return;
}
// console.log(html);
if (html.includes(`<p class="note s-fc3">很抱歉,你要查找的网页找不到</p>`)) {
// TODO 最后统一来处理这里 demo: artistId == 30084536

View File

@@ -37,7 +37,6 @@ async function fetch({ songId }) {
console.error(errors);
return;
}
// console.log(html);
if (html.includes(`<p class="note s-fc3">很抱歉,你要查找的网页找不到</p>`)) {
// TODO 最后统一来处理这里 demo: artistId == 30084536

View File

@@ -15,7 +15,6 @@ async function fetch({ userId }) {
var html = await requestUtils.getApiResult(url);
fs.writeFileSync(path.join(__dirname, "../../temp", ` user-${userId}.html`), html);
}
// console.log(html);
}