get songInfo from youtube API

This commit is contained in:
Araxeus
2021-10-23 16:13:06 +03:00
parent f2e04f9170
commit 51364b63e7
2 changed files with 37 additions and 44 deletions

View File

@ -9,23 +9,27 @@ 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 = () => { function setup() {
const oldXHR = window.XMLHttpRequest; if (document.querySelector('#movie_player')) {
function newXHR() { injectListener();
const realXHR = new oldXHR(); return;
realXHR.addEventListener( }
"readystatechange",
() => { const observer = new MutationObserver(() => {
if (realXHR.readyState === 4 && realXHR.status === 200 if (document.querySelector('#movie_player')) {
&& realXHR.responseURL.includes("/player")) { observer.disconnect();
// if the request contains the song info, send the response to ipcMain injectListener();
ipcRenderer.send("song-info-request", realXHR.responseText); }
} })
},
false observer.observe(document.documentElement, { childList: true, subtree: true });
); }
return realXHR;
} function injectListener() {
window.XMLHttpRequest = newXHR; document.querySelector('video').addEventListener('loadedmetadata', () => {
const data = document.querySelector('#movie_player').getPlayerResponse();
ipcRenderer.send("song-info-request", JSON.stringify(data));
});
}; };
module.exports = injectListener;
module.exports = setup;

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'
); );
}; };
@ -29,16 +25,10 @@ const getImage = async (src) => {
// To find the paused status, we check if the title contains `-` // To find the paused status, we check if the title contains `-`
const getPausedStatus = async (win) => { const getPausedStatus = async (win) => {
const title = await win.webContents.executeJavaScript("document.title"); const title = await win.webContents.executeJavaScript("document.title");
console.log('doc title = ',title)
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 +49,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,19 +100,19 @@ 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) { function cleanupName(artist) {
if (!artist) { if (!artist) return artist;
return artist; const lowerCaseArtist = artist.toLowerCase();
}
for (const suffix of suffixesToRemove) { for (const suffix of suffixesToRemove) {
if (artist.endsWith(suffix)) { if (lowerCaseArtist.endsWith(suffix)) {
return artist.slice(0, -suffix.length); return artist.slice(0, -suffix.length);
} }
} }