mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-17 21:22:05 +00:00
get songInfo from youtube API
This commit is contained in:
@ -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'
|
||||
);
|
||||
};
|
||||
|
||||
@ -29,16 +25,10 @@ const getImage = async (src) => {
|
||||
// To find the paused status, we check if the title contains `-`
|
||||
const getPausedStatus = async (win) => {
|
||||
const title = await win.webContents.executeJavaScript("document.title");
|
||||
console.log('doc title = ',title)
|
||||
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 +49,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,19 +100,19 @@ 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;
|
||||
}
|
||||
if (!artist) return artist;
|
||||
const lowerCaseArtist = artist.toLowerCase();
|
||||
for (const suffix of suffixesToRemove) {
|
||||
if (artist.endsWith(suffix)) {
|
||||
if (lowerCaseArtist.endsWith(suffix)) {
|
||||
return artist.slice(0, -suffix.length);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user