add yt lyrics

This commit is contained in:
vapormusic 2021-12-24 13:20:57 +07:00
parent 9fb4585e52
commit 0486fb8fa6
3 changed files with 137 additions and 62 deletions

View file

@ -37,7 +37,8 @@
"source-map-support": "^0.5.21",
"v8-compile-cache": "^2.3.0",
"ws": "^8.3.0",
"xml2js": "^0.4.23"
"xml2js": "^0.4.23",
"youtube-search-without-api-key": "^1.0.7"
},
"devDependencies": {
"electron": "https://github.com/castlabs/electron-releases.git",

View file

@ -7,6 +7,7 @@ const windowStateKeeper = require("electron-window-state");
const os = require('os');
const Store = require("electron-store");
const store = new Store();
const yt = require('youtube-search-without-api-key');
const schema = {
"general": {
@ -183,6 +184,12 @@ const CiderBase = {
win.close();
})
ipcMain.handle('getYTLyrics', async (event, track, artist) => {
var u = track + " " + artist + " official video";
const videos = await yt.search(u);
return videos
})
ipcMain.handle('getStoreValue', (event, key, defaultValue) => {
return (defaultValue ? app.cfg.get(key, true) : app.cfg.get(key));
});

View file

@ -393,7 +393,8 @@ const app = new Vue({
setTimeout(() => {
this.getBrowsePage();
this.$forceUpdate()}, 500)
this.$forceUpdate()
}, 500)
},
invokeDrawer(panel) {
if (this.drawer.panel == panel && this.drawer.open) {
@ -939,13 +940,15 @@ const app = new Vue({
app.appleCurator = a
}
else {
this.getPlaylistContinuous(a)}
this.getPlaylistContinuous(a)
}
}
} finally {
if (kind == "appleCurator") {
app.appleCurator = a
} else {
this.getPlaylistContinuous(a)}
this.getPlaylistContinuous(a)
}
}
;
},
@ -1373,7 +1376,11 @@ const app = new Vue({
this.page = "search"
},
loadLyrics() {
this.loadMXM();
const musicType = (MusicKit.getInstance().nowPlayingItem != null) ? MusicKit.getInstance().nowPlayingItem["type"] ?? '' : '';
console.log("mt",musicType)
if (musicType === "musicVideo") {
this.loadYTLyrics();} else {
this.loadMXM();}
},
loadAMLyrics() {
const songID = (this.mk.nowPlayingItem != null) ? this.mk.nowPlayingItem["_songId"] ?? -1 : -1;
@ -1392,6 +1399,66 @@ const app = new Vue({
self.getLibrarySongsFull(true)
})
},
async loadYTLyrics() {
const track = (this.mk.nowPlayingItem != null) ? this.mk.nowPlayingItem.title ?? '' : '';
const artist = (this.mk.nowPlayingItem != null) ? this.mk.nowPlayingItem.artistName ?? '' : '';
const time = (this.mk.nowPlayingItem != null) ? (Math.round((this.mk.nowPlayingItem.attributes["durationInMillis"] ?? -1000) / 1000) ?? -1) : -1;
ipcRenderer.invoke('getYTLyrics', track, artist).then((result) => {
var response = result;
if (result.length > 0) {
var rawtime = this.toMS(result[0].duration_raw)
var ytid = result[0]['id']['videoId'];
if (Math.abs(parseInt(rawtime) - time) > 5) {
loadYT(ytid, "en")
} else { app.loadMXM() }
} else { app.loadMXM() }
function loadYT(id, lang) {
var req = new XMLHttpRequest();
var url = `https://www.youtube.com/watch?&v=${id}`;
req.open('GET', url, true);
req.onerror = function (e) {
this.loadMXM();
}
req.onload = function () {
// console.log(this.responseText);
res = this.responseText;
var captionurl1 = res.substring(res.indexOf(`{"playerCaptionsRenderer":{"baseUrl":"`) + (`{"playerCaptionsRenderer":{"baseUrl":"`).length);
var captionurl = captionurl1.substring(0, captionurl1.indexOf(`"`));
if (captionurl.includes("timedtext")) {
var json = JSON.parse(`{"url": "${captionurl}"}`);
var newurl = json.url + `&lang=${lang}&format=ttml`
var req2 = new XMLHttpRequest();
req2.open('GET', newurl, true);
req2.onerror = function (e) {
app.loadMXM();
}
req2.onload = function () {
try {
const ttmlLyrics = this.responseText;
if (ttmlLyrics){
this.lyricsMediaItem = ttmlLyrics
this.parseTTML()}
} catch (e) {
app.loadMXM();
}
}
req2.send();
} else {
app.loadMXM();
}
}
req.send();
}
})
},
loadMXM() {
let attempt = 0;
const track = encodeURIComponent((this.mk.nowPlayingItem != null) ? this.mk.nowPlayingItem.title ?? '' : '');