scrape artistName from playBar

This commit is contained in:
Araxeus
2021-04-15 15:59:41 +03:00
parent a2207a2cb3
commit f765fb63f0

View File

@ -28,6 +28,18 @@ const getPausedStatus = async (win) => {
return !title.includes("-"); return !title.includes("-");
}; };
const getArtist = async (win) => {
return await win.webContents.executeJavaScript(
`
var bar = document.getElementsByClassName('subtitle ytmusic-player-bar')[0];
var artistName = (bar.getElementsByClassName('yt-formatted-string')[0]) || (bar.getElementsByClassName('byline ytmusic-player-bar')[0]);
if (artistName) {
artistName.textContent;
}
`
)
}
// Fill songInfo with empty values // Fill songInfo with empty values
const songInfo = { const songInfo = {
title: "", title: "",
@ -45,10 +57,7 @@ const songInfo = {
const handleData = async (responseText, win) => { const handleData = async (responseText, win) => {
let data = JSON.parse(responseText); let data = JSON.parse(responseText);
songInfo.title = data?.videoDetails?.title; songInfo.title = data?.videoDetails?.title;
songInfo.artist = data?.videoDetails?.author; songInfo.artist = await getArtist(win) || data?.videoDetails?.author;
if (songInfo.artist.endsWith(" - Topic")) {
songInfo.artist = songInfo.artist.slice(0, -8);
}
songInfo.views = data?.videoDetails?.viewCount; songInfo.views = data?.videoDetails?.viewCount;
songInfo.imageSrc = data?.videoDetails?.thumbnail?.thumbnails?.pop()?.url; songInfo.imageSrc = data?.videoDetails?.thumbnail?.thumbnails?.pop()?.url;
songInfo.songDuration = data?.videoDetails?.lengthSeconds; songInfo.songDuration = data?.videoDetails?.lengthSeconds;