add netease music
This commit is contained in:
260
netease_music/index.js
Normal file
260
netease_music/index.js
Normal file
@@ -0,0 +1,260 @@
|
||||
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 main() {
|
||||
console.log("neteaseMusic Starting...");
|
||||
|
||||
// 指定数据库
|
||||
dbUtils.create("neteaseMusic");
|
||||
|
||||
// getMusicInfo({ songId: "1855221507" });
|
||||
// getMusicInfo({ songId: "1855221517" });
|
||||
// getMusicInfo({ songId: "1861632812" });
|
||||
|
||||
// getArtistInfo({ artistId: "1079074" });
|
||||
// getArtistInfo({ artistId: "1079075" });
|
||||
|
||||
// getAlbumInfo({ albumId: "74268047" });
|
||||
// getAlbumInfo({ albumId: "129327797" });
|
||||
|
||||
// 不是所有歌手都有个人主页 例如 https://music.163.com/#/artist?id=1079075
|
||||
// getUserInfo({ userId: "37365202" });
|
||||
// getUserInfo({ userId: "29879272" });
|
||||
|
||||
await startGetMusic({ songId: "1966061035" });
|
||||
}
|
||||
|
||||
async function startGetMusic({ songId }) {
|
||||
await sleepUtils.sleep(500);
|
||||
var songInfo = await getMusicInfo({ songId: songId });
|
||||
|
||||
var albumInfo = await getAlbumInfo({ albumId: songInfo.albumId });
|
||||
if (albumInfo) {
|
||||
for (var songId of albumInfo.songIds) {
|
||||
await startGetMusic({ songId: songId });
|
||||
}
|
||||
}
|
||||
|
||||
for (var artistId of songInfo.artistIds) {
|
||||
var artistInfo = await getArtistInfo({ artistId: artistId });
|
||||
if (artistInfo) {
|
||||
for (var songId of artistInfo.songIds) {
|
||||
await startGetMusic({ songId: songId });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取音乐详情
|
||||
async function getMusicInfo({ songId }) {
|
||||
console.log(`开始处理 song: ${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 url = `https://music.163.com/song?id=${songId}`;
|
||||
|
||||
try {
|
||||
throw new Error(`Error`);
|
||||
var html = fs.readFileSync(path.join(__dirname, "../temp", `song-${songId}.html`), 'utf8');
|
||||
} catch (errors) {
|
||||
var html = await requestUtils.getApiResult(url);
|
||||
fs.writeFileSync(path.join(__dirname, "../temp", `song-${songId}.html`), html);
|
||||
}
|
||||
// console.log(html);
|
||||
|
||||
// 正则匹配
|
||||
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 album = /<meta property="og:music:album" content="(.*?)"\/>/.exec(html)[1];
|
||||
let albumId = /<meta property="music:album" content="https:\/\/music\.163\.com\/album\?id=(.*?)"\/>/.exec(html)[1];
|
||||
let duration = /<meta property="music:duration" content="(.*?)"\/>/.exec(html)[1];
|
||||
|
||||
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,
|
||||
albumId: albumId,
|
||||
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,
|
||||
});
|
||||
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;
|
||||
}
|
||||
|
||||
// 获取音乐人详情
|
||||
async function getArtistInfo({ artistId }) {
|
||||
console.log(`开始处理 artist: ${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 url = `https://music.163.com/artist?id=${artistId}`;
|
||||
|
||||
try {
|
||||
throw new Error(`Error`);
|
||||
var html = fs.readFileSync(path.join(__dirname, "../temp", `artist-${artistId}.html`), 'utf8');
|
||||
} catch (errors) {
|
||||
var html = await requestUtils.getApiResult(url);
|
||||
fs.writeFileSync(path.join(__dirname, "../temp", `artist-${artistId}.html`), html);
|
||||
}
|
||||
// console.log(html);
|
||||
|
||||
// 正则匹配
|
||||
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) {
|
||||
dbUtils.query('INSERT IGNORE INTO song_artist_relation SET ?', {
|
||||
song_id: songId,
|
||||
artist_id: artistId,
|
||||
});
|
||||
});
|
||||
return artistInfo;
|
||||
}
|
||||
|
||||
// 获取专辑详情
|
||||
async function getAlbumInfo({ albumId }) {
|
||||
console.log(`开始处理 album: ${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 {
|
||||
throw new Error(`Error`);
|
||||
var html = fs.readFileSync(path.join(__dirname, "../temp", `album-${albumId}.html`), 'utf8');
|
||||
} catch (errors) {
|
||||
var html = await requestUtils.getApiResult(url);
|
||||
fs.writeFileSync(path.join(__dirname, "../temp", `album-${albumId}.html`), html);
|
||||
}
|
||||
// console.log(html);
|
||||
|
||||
// 正则匹配
|
||||
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) {
|
||||
dbUtils.query('INSERT IGNORE INTO song_album_relation SET ?', {
|
||||
song_id: songId,
|
||||
album_id: albumId,
|
||||
});
|
||||
});
|
||||
return albumInfo;
|
||||
}
|
||||
|
||||
// // 获取音乐人详情
|
||||
// async function getUserInfo({ userId }) {
|
||||
// let url = `https://music.163.com/user/home?id=${userId}`;
|
||||
|
||||
// try {
|
||||
// var html = fs.readFileSync(path.join(__dirname, "../temp", ` user-${userId}.html`), 'utf8');
|
||||
// } catch (errors) {
|
||||
// var html = await requestUtils.getApiResult(url);
|
||||
// fs.writeFileSync(path.join(__dirname, "../temp", ` user-${userId}.html`), html);
|
||||
// }
|
||||
// // console.log(html);
|
||||
|
||||
|
||||
// }
|
||||
|
||||
module.exports = {
|
||||
main: main,
|
||||
}
|
Reference in New Issue
Block a user