thedesk/app/js/ui/spotify.js

186 lines
6.1 KiB
JavaScript
Raw Normal View History

2019-05-19 17:39:30 +10:00
function spotifyConnect() {
2020-01-13 00:49:52 +11:00
var auth = "https://accounts.spotify.com/authorize?client_id=0f18e54abe0b4aedb4591e353d3aff69&redirect_uri=https://thedesk.top/spotify-connect&response_type=code&scope=user-read-currently-playing";
var platform = localStorage.getItem("platform");
if (platform == "win32") {
postMessage(["openUrl", auth], "*");
postMessage(["sendSinmpleIpc", "quit"], "*");
2019-11-04 03:10:06 +11:00
} else {
2020-01-13 00:49:52 +11:00
auth = auth + "&state=code";
$("#spotify-code-show").removeClass("hide");
postMessage(["openUrl", auth], "*");
2019-11-04 03:10:06 +11:00
}
2018-04-01 07:00:27 +10:00
}
2019-05-19 17:39:30 +10:00
function spotifyAuth() {
2020-01-13 00:49:52 +11:00
var code = $("#spotify-code").val();
localStorage.setItem("spotify", "code");
localStorage.setItem("spotify-refresh", code);
$("#spotify-code-show").addClass("hide");
$("#spotify-enable").addClass("disabled");
$("#spotify-disable").removeClass("disabled");
2018-03-15 06:42:48 +11:00
}
2019-05-19 17:39:30 +10:00
function spotifyDisconnect() {
2020-01-13 00:49:52 +11:00
localStorage.removeItem("spotify");
localStorage.removeItem("spotify-refresh");
checkSpotify();
2018-03-15 06:42:48 +11:00
}
2019-05-19 17:39:30 +10:00
function checkSpotify() {
2020-01-13 00:49:52 +11:00
if (localStorage.getItem("spotify")) {
$("#spotify-enable").addClass("disabled");
$("#spotify-disable").removeClass("disabled");
2019-11-04 03:10:06 +11:00
} else {
2020-01-13 00:49:52 +11:00
$("#spotify-enable").removeClass("disabled");
$("#spotify-disable").addClass("disabled");
2019-11-04 03:10:06 +11:00
}
2020-01-13 00:49:52 +11:00
var content = localStorage.getItem("np-temp");
if (!content || content == "" || content == "null") {
var content = "#NowPlaying {song} / {album} / {artist}\n{url} #SpotifyWithTheDesk";
2019-11-04 03:10:06 +11:00
}
2020-01-13 00:49:52 +11:00
$("#np-temp").val(content);
var flag = localStorage.getItem("artwork");
2019-11-04 03:10:06 +11:00
if (flag) {
2020-01-13 00:49:52 +11:00
$("#awk_yes").prop("checked", true);
2019-11-04 03:10:06 +11:00
} else {
2020-01-13 00:49:52 +11:00
$("#awk_no").prop("checked", true);
2019-11-04 03:10:06 +11:00
}
2018-06-12 01:44:28 +10:00
}
2019-05-19 17:39:30 +10:00
function spotifyFlagSave() {
2020-01-13 00:49:52 +11:00
var awk = $("[name=awk]:checked").val();
if (awk == "yes") {
localStorage.setItem("artwork", "yes");
M.toast({ html: lang.lang_spotify_img, displayLength: 3000 });
2019-11-04 03:10:06 +11:00
} else {
2020-01-13 00:49:52 +11:00
localStorage.removeItem("artwork");
M.toast({ html: lang.lang_spotify_imgno, displayLength: 3000 });
2019-11-04 03:10:06 +11:00
}
2018-03-15 06:42:48 +11:00
}
2019-05-19 17:39:30 +10:00
function nowplaying(mode) {
2020-01-13 00:49:52 +11:00
if (mode == "spotify") {
var start = "https://thedesk.top/now-playing?at=" + localStorage.getItem("spotify") + "&rt=" + localStorage.getItem("spotify-refresh");
var at = localStorage.getItem("spotify");
2019-11-04 03:10:06 +11:00
if (at) {
fetch(start, {
2020-01-13 00:49:52 +11:00
method: "GET",
2019-11-04 03:10:06 +11:00
headers: {
2020-01-13 00:49:52 +11:00
"content-type": "application/json"
2019-11-04 03:10:06 +11:00
}
})
.then(function(response) {
if (!response.ok) {
response.text().then(function(text) {
2020-01-13 00:49:52 +11:00
setLog(response.url, response.status, text);
});
2019-11-04 03:10:06 +11:00
}
2020-01-13 00:49:52 +11:00
return response.json();
2019-11-04 03:10:06 +11:00
})
.catch(function(error) {
2020-01-13 00:49:52 +11:00
todo(error);
setLog(start, "JSON", error);
console.error(error);
2019-11-04 03:10:06 +11:00
})
.then(function(json) {
2020-01-13 00:49:52 +11:00
console.table(json);
2019-11-04 03:10:06 +11:00
if (json.length < 1) {
2020-01-13 00:49:52 +11:00
return false;
2019-11-04 03:10:06 +11:00
}
2020-01-13 00:49:52 +11:00
var item = json.item;
var img = item.album.images[0].url;
var flag = localStorage.getItem("artwork");
2019-11-04 03:10:06 +11:00
if (flag) {
2020-01-13 00:49:52 +11:00
postMessage(["bmpImage", [img, 0]], "*");
2019-11-04 03:10:06 +11:00
}
2020-01-13 00:49:52 +11:00
var content = localStorage.getItem("np-temp");
if (!content || content == "" || content == "null") {
var content = "#NowPlaying {song} / {album} / {artist}\n{url}";
2019-11-04 03:10:06 +11:00
}
2020-01-13 00:49:52 +11:00
var regExp = new RegExp("{song}", "g");
content = content.replace(regExp, item.name);
var regExp = new RegExp("{album}", "g");
content = content.replace(regExp, item.album.name);
var regExp = new RegExp("{artist}", "g");
content = content.replace(regExp, item.artists[0].name);
var regExp = new RegExp("{url}", "g");
content = content.replace(regExp, item.external_urls.spotify);
var regExp = new RegExp("{composer}", "g");
content = content.replace(regExp, "");
var regExp = new RegExp("{hz}", "g");
content = content.replace(regExp, "");
var regExp = new RegExp("{bitRate}", "g");
content = content.replace(regExp, "");
var regExp = new RegExp("{lyricist}", "g");
content = content.replace(regExp, "");
var regExp = new RegExp("{bpm}", "g");
content = content.replace(regExp, "");
var regExp = new RegExp("{genre}", "g");
content = content.replace(regExp, "");
$("#textarea").val(content);
});
2019-11-04 03:10:06 +11:00
} else {
Swal.fire({
2020-01-13 00:49:52 +11:00
type: "info",
2019-11-04 03:10:06 +11:00
title: lang.lang_spotify_acct
2020-01-13 00:49:52 +11:00
});
2019-10-31 02:30:26 +11:00
}
2020-01-13 00:49:52 +11:00
} else if (mode == "itunes") {
postMessage(["itunes", ""], "*");
2019-11-04 03:10:06 +11:00
}
}
function npCore(arg) {
2020-01-13 00:49:52 +11:00
console.table(arg);
var content = localStorage.getItem("np-temp");
if (!content || content == "" || content == "null") {
var content = "#NowPlaying {song} / {album} / {artist}\n{url}";
2019-11-04 03:10:06 +11:00
}
2020-01-13 00:49:52 +11:00
var flag = localStorage.getItem("artwork");
var platform = localStorage.getItem("platform");
if (platform == "win32") {
2019-11-04 03:10:06 +11:00
if (flag && arg.path) {
2020-01-13 00:49:52 +11:00
media(arg.path, "image/png", "new");
2019-11-04 03:10:06 +11:00
}
2020-01-13 00:49:52 +11:00
} else if (platform == "darwin") {
2019-11-04 03:10:06 +11:00
if (flag && arg.artwork) {
2020-01-13 00:49:52 +11:00
media(arg.artwork, "image/png", "new");
2019-11-04 03:10:06 +11:00
}
}
2020-01-13 00:49:52 +11:00
var regExp = new RegExp("{song}", "g");
content = content.replace(regExp, arg.name);
var regExp = new RegExp("{album}", "g");
2019-11-04 03:10:06 +11:00
if (arg.album) {
if (arg.album.name) {
2020-01-13 00:49:52 +11:00
content = content.replace(regExp, arg.album.name);
2019-11-04 03:10:06 +11:00
}
}
2020-01-13 00:49:52 +11:00
var regExp = new RegExp("{artist}", "g");
content = content.replace(regExp, arg.artist);
var regExp = new RegExp("{url}", "g");
content = content.replace(regExp, "");
var regExp = new RegExp("{composer}", "g");
content = content.replace(regExp, arg.composer);
var regExp = new RegExp("{hz}", "g");
content = content.replace(regExp, arg.sampleRate / 1000 + "kHz");
var regExp = new RegExp("{lyricist}", "g");
content = content.replace(regExp, "");
var regExp = new RegExp("{bpm}", "g");
content = content.replace(regExp, "");
var regExp = new RegExp("{bitRate}", "g");
content = content.replace(regExp, arg.bitRate + "kbps");
var regExp = new RegExp("{genre}", "g");
content = content.replace(regExp, arg.genre);
$("#textarea").val(content);
2018-03-15 06:42:48 +11:00
}
2019-05-19 17:39:30 +10:00
function spotifySave() {
2020-01-13 00:49:52 +11:00
var temp = $("#np-temp").val();
localStorage.setItem("np-temp", temp);
M.toast({ html: lang.lang_spotify_np, displayLength: 3000 });
2018-03-15 06:42:48 +11:00
}
2019-05-19 17:39:30 +10:00
if (location.search) {
2020-01-13 00:49:52 +11:00
var m = location.search.match(/\?mode=([a-zA-Z-0-9]+)\&code=(.+)/);
var mode = m[1];
var codex = m[2];
if (mode == "spotify") {
var coder = codex.split(":");
localStorage.setItem("spotify", coder[0]);
localStorage.setItem("spotify-refresh", coder[1]);
2019-11-04 03:10:06 +11:00
} else {
}
}