thedesk/app/js/ui/spotify.js

129 lines
4.7 KiB
JavaScript
Raw Normal View History

2018-03-15 06:42:48 +11:00
function spotifyConnect(){
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";
const {
shell
} = require('electron');
2018-04-01 07:00:27 +10:00
var electron = require("electron");
var remote=electron.remote;
var platform=remote.process.platform;
if(platform=="win32"){
shell.openExternal(auth);
2018-03-15 06:42:48 +11:00
var ipc = electron.ipcRenderer;
2018-04-01 07:00:27 +10:00
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
}
function spotifyAuth(){
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
}
function spotifyDisconnect(){
localStorage.removeItem("spotify");
localStorage.removeItem("spotify-refresh");
checkSpotify();
}
function checkSpotify(){
if(localStorage.getItem("spotify")){
$("#spotify-enable").addClass("disabled");
$("#spotify-disable").removeClass("disabled");
}else{
$("#spotify-enable").removeClass("disabled");
$("#spotify-disable").addClass("disabled");
}
2018-03-18 02:00:53 +11:00
var content=localStorage.getItem("np-temp");
2018-04-01 07:00:27 +10:00
if(!content || content==""){
var content="#NowPlaying {song} / {album} / {artist}\n{url} #SpotifyWithTheDesk";
}
2018-03-18 02:00:53 +11:00
$("#np-temp").val(content);
2018-03-15 06:42:48 +11:00
}
2018-04-10 02:22:08 +10:00
function nowplaying(mode){
if(mode=="spotify"){
var start = "https://thedesk.top/now-playing?at="+localStorage.getItem("spotify")+"&rt="+localStorage.getItem("spotify-refresh");
var at = localStorage.getItem("spotify");
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.log(json);
var item=json.item;
var content=localStorage.getItem("np-temp");
if(!content || content==""){
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);
$("#textarea").val(content);
});
}else{
alert("アカウント連携設定をして下さい。");
2018-03-15 06:42:48 +11:00
}
2018-04-10 02:22:08 +10:00
}else if(mode=="itunes"){
var electron = require("electron");
var ipc = electron.ipcRenderer;
ipc.send('itunes', "");
ipc.on('itunesRes', function (event, arg) {
var content=localStorage.getItem("np-temp");
if(!content || content==""){
var content="#NowPlaying {song} / {album} / {artist}\n{url}";
}
var str_array=arg.artist.split('');//1文字ずつ配列に入れる
var utf8Array=Encoding.convert(str_array, 'SJIS', 'AUTO');//UTF-8に変換
console.log(utf8Array);
var convert=Encoding.codeToString( utf8Array );
console.log(convert);
var regExp = new RegExp("{song}", "g");
content = content.replace(regExp, arg.name);
var regExp = new RegExp("{album}", "g");
content = content.replace(regExp, arg.album);
var regExp = new RegExp("{artist}", "g");
content = content.replace(regExp, arg.artist);
var regExp = new RegExp("{url}", "g");
content = content.replace(regExp, "");
$("#textarea").val(content);
})
2018-03-18 02:00:53 +11:00
}
2018-04-10 02:22:08 +10:00
2018-03-15 06:42:48 +11:00
}
function spotifySave(){
var temp=$("#np-temp").val();
localStorage.setItem("np-temp", temp);
Materialize.toast("NowPlaying文章を更新しました。", 3000);
}
if(location.search){
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]);
}else{
}
}