thedesk/app/js/ui/spotify.js

221 lines
8.4 KiB
JavaScript
Raw Normal View History

2019-05-19 17:39:30 +10:00
function spotifyConnect() {
2018-03-15 06:42:48 +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";
2019-05-19 17:39:30 +10:00
const {
shell
} = require('electron');
var electron = require("electron");
var remote = electron.remote;
var platform = remote.process.platform;
if (platform == "win32") {
shell.openExternal(auth);
var ipc = electron.ipcRenderer;
ipc.send('quit', 'go');
} else {
auth = auth + "&state=code";
$("#spotify-code-show").removeClass("hide");
shell.openExternal(auth);
}
2018-03-15 06:42:48 +11:00
2018-04-01 07:00:27 +10:00
}
2019-05-19 17:39:30 +10:00
function spotifyAuth() {
var code = $("#spotify-code").val();
2018-04-01 07:00:27 +10:00
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() {
2018-03-15 06:42:48 +11:00
localStorage.removeItem("spotify");
localStorage.removeItem("spotify-refresh");
checkSpotify();
}
2019-05-19 17:39:30 +10:00
function checkSpotify() {
if (localStorage.getItem("spotify")) {
2018-03-15 06:42:48 +11:00
$("#spotify-enable").addClass("disabled");
$("#spotify-disable").removeClass("disabled");
2019-05-19 17:39:30 +10:00
} else {
2018-03-15 06:42:48 +11:00
$("#spotify-enable").removeClass("disabled");
$("#spotify-disable").addClass("disabled");
}
2019-05-19 17:39:30 +10:00
var content = localStorage.getItem("np-temp");
if (!content || content == "" || content == "null") {
var content = "#NowPlaying {song} / {album} / {artist}\n{url} #SpotifyWithTheDesk";
2018-04-01 07:00:27 +10:00
}
2018-03-18 02:00:53 +11:00
$("#np-temp").val(content);
2019-05-19 17:39:30 +10:00
var flag = localStorage.getItem("artwork");
if (flag) {
2018-06-12 01:44:28 +10:00
$("#awk_yes").prop("checked", true);
2019-05-19 17:39:30 +10:00
} else {
2018-06-12 01:44:28 +10:00
$("#awk_no").prop("checked", true);
}
}
2019-05-19 17:39:30 +10:00
function spotifyFlagSave() {
2018-06-12 01:44:28 +10:00
var awk = $("[name=awk]:checked").val();
2019-05-19 17:39:30 +10:00
if (awk == "yes") {
localStorage.setItem("artwork", "yes");
2019-01-26 14:24:26 +11:00
Materialize.toast(lang.lang_spotify_img, 3000);
2019-05-19 17:39:30 +10:00
} else {
2018-06-12 01:44:28 +10:00
localStorage.removeItem("artwork");
2019-01-26 14:24:26 +11:00
Materialize.toast(lang.lang_spotify_imgno, 3000);
2018-06-12 01:44:28 +10:00
}
2018-03-15 06:42:48 +11:00
}
2019-05-19 17:39:30 +10:00
function nowplaying(mode) {
if (mode == "spotify") {
var start = "https://thedesk.top/now-playing?at=" + localStorage.getItem("spotify") + "&rt=" + localStorage.getItem("spotify-refresh");
2018-04-10 02:22:08 +10:00
var at = localStorage.getItem("spotify");
2019-05-19 17:39:30 +10:00
if (at) {
fetch(start, {
method: 'GET',
headers: {
'content-type': 'application/json'
}
}).then(function (response) {
return response.json();
}).catch(function (error) {
todo(error);
console.error(error);
}).then(function (json) {
console.table(json);
if (json.length < 1) {
return false;
}
var item = json.item;
var img = item.album.images[0].url;
var electron = require("electron");
var ipc = electron.ipcRenderer;
var flag = localStorage.getItem("artwork");
if (flag) {
ipc.send('bmp-image', [img, 0]);
}
var content = localStorage.getItem("np-temp");
if (!content || content == "" || content == "null") {
var content = "#NowPlaying {song} / {album} / {artist}\n{url}";
}
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);
});
} else {
2019-01-26 14:24:26 +11:00
alert(lang.lang_spotify_acct);
2018-03-15 06:42:48 +11:00
}
2019-05-19 17:39:30 +10:00
} else if (mode == "itunes") {
2018-04-10 02:22:08 +10:00
var electron = require("electron");
2019-05-19 17:39:30 +10:00
var ipc = electron.ipcRenderer;
if (ipc.listenerCount('itunes-np') > 0) {
return false;
}
2019-05-19 17:39:30 +10:00
ipc.send('itunes', "");
ipc.once('itunes-np', function (event, arg) {
2019-04-14 02:32:16 +10:00
if (arg.cmd) {
2019-05-19 17:39:30 +10:00
console.error(arg);
return;
2019-04-14 02:32:16 +10:00
}
2019-05-19 16:17:05 +10:00
console.table(arg);
2019-05-19 17:39:30 +10:00
var content = localStorage.getItem("np-temp");
if (!content || content == "" || content == "null") {
var content = "#NowPlaying {song} / {album} / {artist}\n{url}";
2018-04-10 02:22:08 +10:00
}
2019-05-19 17:39:30 +10:00
var flag = localStorage.getItem("artwork");
var remote = electron.remote;
var platform = remote.process.platform;
if (platform == "win32") {
if (flag && arg.path) {
media(arg.path, "image/png", "new");
2019-03-20 22:08:33 +11:00
}
2019-05-19 17:39:30 +10:00
} else if (platform == "darwin") {
if (flag && arg.existsArtwork) {
media(arg.artworks[0].data, "image/png", "new");
2019-03-20 22:08:33 +11:00
}
}
2018-04-10 02:22:08 +10:00
var regExp = new RegExp("{song}", "g");
content = content.replace(regExp, arg.name);
var regExp = new RegExp("{album}", "g");
2018-06-12 02:41:11 +10:00
content = content.replace(regExp, arg.album);
2018-04-10 02:22:08 +10:00
var regExp = new RegExp("{artist}", "g");
content = content.replace(regExp, arg.artist);
var regExp = new RegExp("{url}", "g");
content = content.replace(regExp, "");
2018-05-26 22:48:13 +10:00
var regExp = new RegExp("{composer}", "g");
content = content.replace(regExp, arg.composer);
var regExp = new RegExp("{hz}", "g");
2019-05-19 17:39:30 +10:00
content = content.replace(regExp, arg.sampleRate / 1000 + "kHz");
2018-07-22 23:03:46 +10:00
var regExp = new RegExp("{lyricist}", "g");
content = content.replace(regExp, "");
var regExp = new RegExp("{bpm}", "g");
content = content.replace(regExp, "");
2018-06-12 02:41:11 +10:00
var regExp = new RegExp("{bitRate}", "g");
2019-05-19 17:39:30 +10:00
content = content.replace(regExp, arg.bitRate + "kbps");
2018-06-12 02:41:11 +10:00
var regExp = new RegExp("{genre}", "g");
content = content.replace(regExp, arg.genre);
2018-04-10 02:22:08 +10:00
$("#textarea").val(content);
2019-05-19 17:39:30 +10:00
})
2018-03-18 02:00:53 +11:00
}
2019-05-19 17:39:30 +10:00
2018-03-15 06:42:48 +11:00
}
2019-05-19 17:39:30 +10:00
function spotifySave() {
var temp = $("#np-temp").val();
2018-03-15 06:42:48 +11:00
localStorage.setItem("np-temp", temp);
2019-01-26 14:24:26 +11:00
Materialize.toast(lang.lang_spotify_np, 3000);
2018-03-15 06:42:48 +11:00
}
2019-05-19 17:39:30 +10:00
function npprovider() {
2019-01-27 01:27:35 +11:00
var provd = $("[name=npp]:checked").val();
2019-05-19 17:39:30 +10:00
if (!provd) {
2019-01-27 01:27:35 +11:00
if (localStorage.getItem("np_provider")) {
2019-05-19 17:39:30 +10:00
$("[value=" + localStorage.getItem("np_provider") + "]").prop("checked", true);
} else {
2019-01-27 01:27:35 +11:00
$("[value=AIMP]").prop("checked", true);
localStorage.setItem("np_provider", "AIMP");
}
2019-05-19 17:39:30 +10:00
} else {
2019-01-27 01:27:35 +11:00
if (provd != localStorage.getItem("np_provider")) {
2019-05-19 17:39:30 +10:00
Materialize.toast(lang.lang_setting_npprovide.replace("{{set}}", provd), 3000);
2019-01-27 01:27:35 +11:00
}
localStorage.setItem("np_provider", provd);
var electron = require("electron");
var ipc = electron.ipcRenderer;
2019-05-19 17:39:30 +10:00
ipc.send('itunes', ["set", provd]);
2019-01-27 01:27:35 +11:00
}
}
2019-05-19 17:39:30 +10:00
if (location.search) {
2018-03-15 06:42:48 +11:00
var m = location.search.match(/\?mode=([a-zA-Z-0-9]+)\&code=(.+)/);
2019-05-19 17:39:30 +10:00
var mode = m[1];
var codex = m[2];
if (mode == "spotify") {
var coder = codex.split(":");
2018-03-15 06:42:48 +11:00
localStorage.setItem("spotify", coder[0]);
localStorage.setItem("spotify-refresh", coder[1]);
2019-05-19 17:39:30 +10:00
} else {
2018-03-15 06:42:48 +11:00
}
2019-05-19 17:39:30 +10:00
2018-05-20 16:17:10 +10:00
}
2019-05-19 17:39:30 +10:00
$("#npbtn").click(function () {
2018-05-20 16:17:10 +10:00
nowplaying('spotify');
});
2019-05-19 17:39:30 +10:00
$("#npbtn").bind('contextmenu', function () {
2018-05-20 16:17:10 +10:00
nowplaying('itunes');
return false;
2019-04-11 02:52:01 +10:00
});