mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-10 10:11:46 +00:00
Merge pull request #443 from Araxeus/songInfo-straight-from-youtube-api
get songInfo from youtube API
This commit is contained in:
@ -9,23 +9,11 @@ ipcRenderer.on("update-song-info", async (_, extractedSongInfo) => {
|
||||
global.songInfo.image = await getImage(global.songInfo.imageSrc);
|
||||
});
|
||||
|
||||
const injectListener = () => {
|
||||
const oldXHR = window.XMLHttpRequest;
|
||||
function newXHR() {
|
||||
const realXHR = new oldXHR();
|
||||
realXHR.addEventListener(
|
||||
"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 = () => {
|
||||
document.addEventListener('apiLoaded', e => {
|
||||
document.querySelector('video').addEventListener('loadeddata', () => {
|
||||
const data = e.detail.getPlayerResponse();
|
||||
ipcRenderer.send("song-info-request", JSON.stringify(data));
|
||||
});
|
||||
})
|
||||
};
|
||||
module.exports = injectListener;
|
||||
|
||||
@ -2,15 +2,11 @@ const { ipcMain, nativeImage } = require("electron");
|
||||
|
||||
const fetch = require("node-fetch");
|
||||
|
||||
// This selects the progress bar, used for current progress
|
||||
const progressSelector = "#progress-bar";
|
||||
|
||||
|
||||
// Grab the progress using the selector
|
||||
const getProgress = async (win) => {
|
||||
// Get current value of the progressbar element
|
||||
return win.webContents.executeJavaScript(
|
||||
'document.querySelector("' + progressSelector + '").value'
|
||||
'document.querySelector("#progress-bar").value'
|
||||
);
|
||||
};
|
||||
|
||||
@ -32,13 +28,6 @@ const getPausedStatus = async (win) => {
|
||||
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
|
||||
/**
|
||||
* @typedef {songInfo} SongInfo
|
||||
@ -59,14 +48,13 @@ const songInfo = {
|
||||
const handleData = async (responseText, win) => {
|
||||
let data = JSON.parse(responseText);
|
||||
songInfo.title = cleanupName(data?.videoDetails?.title);
|
||||
songInfo.artist =
|
||||
(await getArtist(win)) || cleanupName(data?.videoDetails?.author);
|
||||
songInfo.artist =cleanupName(data?.videoDetails?.author);
|
||||
songInfo.views = data?.videoDetails?.viewCount;
|
||||
songInfo.imageSrc = data?.videoDetails?.thumbnail?.thumbnails?.pop()?.url;
|
||||
songInfo.songDuration = data?.videoDetails?.lengthSeconds;
|
||||
songInfo.image = await getImage(songInfo.imageSrc);
|
||||
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));
|
||||
};
|
||||
@ -111,23 +99,23 @@ const registerProvider = (win) => {
|
||||
};
|
||||
|
||||
const suffixesToRemove = [
|
||||
" - Topic",
|
||||
"VEVO",
|
||||
" (Performance Video)",
|
||||
" (Official Music Video)",
|
||||
" (Official Video)",
|
||||
" (Clip officiel)",
|
||||
" - topic",
|
||||
"vevo",
|
||||
" (performance video)",
|
||||
" (official music video)",
|
||||
" (official video)",
|
||||
" (clip officiel)",
|
||||
];
|
||||
function cleanupName(artist) {
|
||||
if (!artist) {
|
||||
return artist;
|
||||
}
|
||||
|
||||
function cleanupName(name) {
|
||||
if (!name) return name;
|
||||
const lowCaseName = name.toLowerCase();
|
||||
for (const suffix of suffixesToRemove) {
|
||||
if (artist.endsWith(suffix)) {
|
||||
return artist.slice(0, -suffix.length);
|
||||
if (lowCaseName.endsWith(suffix)) {
|
||||
return name.slice(0, -suffix.length);
|
||||
}
|
||||
}
|
||||
return artist;
|
||||
return name;
|
||||
}
|
||||
|
||||
module.exports = registerCallback;
|
||||
|
||||
Reference in New Issue
Block a user