Merge pull request #443 from Araxeus/songInfo-straight-from-youtube-api

get songInfo from youtube API
This commit is contained in:
th-ch
2021-10-24 13:42:59 +02:00
committed by GitHub
2 changed files with 23 additions and 47 deletions

View File

@ -9,23 +9,11 @@ ipcRenderer.on("update-song-info", async (_, extractedSongInfo) => {
global.songInfo.image = await getImage(global.songInfo.imageSrc); global.songInfo.image = await getImage(global.songInfo.imageSrc);
}); });
const injectListener = () => { module.exports = () => {
const oldXHR = window.XMLHttpRequest; document.addEventListener('apiLoaded', e => {
function newXHR() { document.querySelector('video').addEventListener('loadeddata', () => {
const realXHR = new oldXHR(); const data = e.detail.getPlayerResponse();
realXHR.addEventListener( ipcRenderer.send("song-info-request", JSON.stringify(data));
"readystatechange", });
() => { })
if (realXHR.readyState === 4 && realXHR.status === 200
&& realXHR.responseURL.includes("/player")) {
// if the request contains the song info, send the response to ipcMain
ipcRenderer.send("song-info-request", realXHR.responseText);
}
},
false
);
return realXHR;
}
window.XMLHttpRequest = newXHR;
}; };
module.exports = injectListener;

View File

@ -2,15 +2,11 @@ const { ipcMain, nativeImage } = require("electron");
const fetch = require("node-fetch"); const fetch = require("node-fetch");
// This selects the progress bar, used for current progress
const progressSelector = "#progress-bar";
// Grab the progress using the selector // Grab the progress using the selector
const getProgress = async (win) => { const getProgress = async (win) => {
// Get current value of the progressbar element // Get current value of the progressbar element
return win.webContents.executeJavaScript( return win.webContents.executeJavaScript(
'document.querySelector("' + progressSelector + '").value' 'document.querySelector("#progress-bar").value'
); );
}; };
@ -32,13 +28,6 @@ const getPausedStatus = async (win) => {
return !title.includes("-"); return !title.includes("-");
}; };
const getArtist = async (win) => {
return win.webContents.executeJavaScript(`
document.querySelector(".subtitle.ytmusic-player-bar .yt-formatted-string")
?.textContent
`);
}
// Fill songInfo with empty values // Fill songInfo with empty values
/** /**
* @typedef {songInfo} SongInfo * @typedef {songInfo} SongInfo
@ -59,14 +48,13 @@ const songInfo = {
const handleData = async (responseText, win) => { const handleData = async (responseText, win) => {
let data = JSON.parse(responseText); let data = JSON.parse(responseText);
songInfo.title = cleanupName(data?.videoDetails?.title); songInfo.title = cleanupName(data?.videoDetails?.title);
songInfo.artist = songInfo.artist =cleanupName(data?.videoDetails?.author);
(await getArtist(win)) || cleanupName(data?.videoDetails?.author);
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;
songInfo.image = await getImage(songInfo.imageSrc); songInfo.image = await getImage(songInfo.imageSrc);
songInfo.uploadDate = data?.microformat?.microformatDataRenderer?.uploadDate; songInfo.uploadDate = data?.microformat?.microformatDataRenderer?.uploadDate;
songInfo.url = data?.microformat?.microformatDataRenderer?.urlCanonical; songInfo.url = data?.microformat?.microformatDataRenderer?.urlCanonical?.split("&")[0];
win.webContents.send("update-song-info", JSON.stringify(songInfo)); win.webContents.send("update-song-info", JSON.stringify(songInfo));
}; };
@ -111,23 +99,23 @@ const registerProvider = (win) => {
}; };
const suffixesToRemove = [ const suffixesToRemove = [
" - Topic", " - topic",
"VEVO", "vevo",
" (Performance Video)", " (performance video)",
" (Official Music Video)", " (official music video)",
" (Official Video)", " (official video)",
" (Clip officiel)", " (clip officiel)",
]; ];
function cleanupName(artist) {
if (!artist) { function cleanupName(name) {
return artist; if (!name) return name;
} const lowCaseName = name.toLowerCase();
for (const suffix of suffixesToRemove) { for (const suffix of suffixesToRemove) {
if (artist.endsWith(suffix)) { if (lowCaseName.endsWith(suffix)) {
return artist.slice(0, -suffix.length); return name.slice(0, -suffix.length);
} }
} }
return artist; return name;
} }
module.exports = registerCallback; module.exports = registerCallback;