2021-04-27 10:07:17 +01:00
|
|
|
/* globals Quasar, Vue, _, VueQrcode, windowMixin, LNbits, LOCALE */
|
|
|
|
|
|
|
|
Vue.component(VueQrcode.name, VueQrcode)
|
|
|
|
|
|
|
|
const pica = window.pica()
|
|
|
|
|
2021-04-28 12:04:47 +01:00
|
|
|
|
2021-04-27 10:07:17 +01:00
|
|
|
|
|
|
|
new Vue({
|
|
|
|
el: '#vue',
|
|
|
|
mixins: [windowMixin],
|
|
|
|
data() {
|
|
|
|
return {
|
2021-04-28 12:04:47 +01:00
|
|
|
isPwd: true,
|
|
|
|
tokenFetched: true,
|
2021-04-29 00:40:58 +01:00
|
|
|
devices: [],
|
2021-04-28 12:04:47 +01:00
|
|
|
jukebox: {},
|
|
|
|
playlists: [],
|
|
|
|
step: 1,
|
|
|
|
locationcbPath: "",
|
2021-04-29 00:40:58 +01:00
|
|
|
locationcb: "",
|
2021-04-28 12:04:47 +01:00
|
|
|
jukeboxDialog: {
|
2021-04-27 10:07:17 +01:00
|
|
|
show: false,
|
2021-04-28 12:04:47 +01:00
|
|
|
data: {}
|
|
|
|
},
|
|
|
|
spotifyDialog: false
|
2021-04-27 10:07:17 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
printItems() {
|
|
|
|
return this.jukebox.items.filter(({enabled}) => enabled)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2021-04-28 12:04:47 +01:00
|
|
|
closeFormDialog() {
|
|
|
|
this.jukeboxDialog.data = {}
|
|
|
|
this.jukeboxDialog.show = false
|
|
|
|
this.step = 1
|
2021-04-27 10:07:17 +01:00
|
|
|
},
|
2021-04-28 12:04:47 +01:00
|
|
|
submitSpotify() {
|
|
|
|
|
|
|
|
self = this
|
|
|
|
console.log(self.jukeboxDialog.data)
|
|
|
|
self.requestAuthorization()
|
|
|
|
this.$q.notify({
|
|
|
|
spinner: true,
|
|
|
|
message: 'Fetching token',
|
|
|
|
timeout: 4000
|
2021-04-27 10:07:17 +01:00
|
|
|
})
|
2021-04-28 12:04:47 +01:00
|
|
|
LNbits.api.request(
|
|
|
|
'POST',
|
|
|
|
'/jukebox/api/v1/jukebox/',
|
|
|
|
self.g.user.wallets[0].adminkey,
|
|
|
|
self.jukeboxDialog.data
|
|
|
|
)
|
2021-04-27 10:07:17 +01:00
|
|
|
.then(response => {
|
2021-04-28 12:04:47 +01:00
|
|
|
if(response.data){
|
|
|
|
var timerId = setInterval(function(){
|
2021-04-29 00:40:58 +01:00
|
|
|
console.log(response.data)
|
2021-04-28 12:04:47 +01:00
|
|
|
if(!self.jukeboxDialog.data.sp_user){
|
|
|
|
clearInterval(timerId);
|
|
|
|
}
|
2021-04-29 00:40:58 +01:00
|
|
|
self.jukeboxDialog.data.sp_id = response.data.id
|
2021-04-28 12:04:47 +01:00
|
|
|
LNbits.api
|
2021-04-29 00:40:58 +01:00
|
|
|
.request('GET', '/jukebox/api/v1/jukebox/spotify/' + self.jukeboxDialog.data.sp_id, self.g.user.wallets[0].inkey)
|
2021-04-28 12:04:47 +01:00
|
|
|
.then(response => {
|
2021-04-29 00:40:58 +01:00
|
|
|
if(response.data.sp_access_token){
|
|
|
|
self.jukeboxDialog.data.sp_access_token = response.data.sp_access_token
|
2021-04-28 12:04:47 +01:00
|
|
|
self.step = 3
|
2021-04-29 00:40:58 +01:00
|
|
|
self.fetchAccessToken()
|
|
|
|
|
|
|
|
clearInterval(timerId)
|
|
|
|
|
|
|
|
// self.refreshPlaylists(response.data.sp_token)
|
|
|
|
// self.$q.notify({
|
|
|
|
// message: 'Success! App is now linked!',
|
|
|
|
// timeout: 3000
|
|
|
|
// })
|
2021-04-28 12:04:47 +01:00
|
|
|
//set devices, playlists
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
LNbits.utils.notifyApiError(err)
|
|
|
|
})
|
|
|
|
}, 3000)
|
|
|
|
}
|
2021-04-27 10:07:17 +01:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2021-04-28 12:04:47 +01:00
|
|
|
LNbits.utils.notifyApiError(err)
|
2021-04-27 10:07:17 +01:00
|
|
|
})
|
|
|
|
},
|
2021-04-28 12:04:47 +01:00
|
|
|
requestAuthorization(){
|
|
|
|
self = this
|
2021-04-29 00:40:58 +01:00
|
|
|
var url = 'https://accounts.spotify.com/authorize'
|
|
|
|
url += '?client_id=' + self.jukeboxDialog.data.sp_user
|
|
|
|
url += '&response_type=code'
|
2021-04-29 08:47:58 +01:00
|
|
|
url += '&redirect_uri=' + encodeURI(self.locationcbPath) + self.jukeboxDialog.data.sp_user
|
2021-04-29 00:40:58 +01:00
|
|
|
url += "&show_dialog=true"
|
|
|
|
url += '&scope=user-read-private user-read-email user-modify-playback-state user-read-playback-position user-library-read streaming user-read-playback-state user-read-recently-played playlist-read-private'
|
2021-04-28 12:04:47 +01:00
|
|
|
console.log(url)
|
|
|
|
window.open(url)
|
|
|
|
},
|
|
|
|
openNewDialog() {
|
|
|
|
this.jukeboxDialog.show = true
|
|
|
|
this.jukeboxDialog.data = {}
|
|
|
|
},
|
|
|
|
openUpdateDialog(itemId) {
|
|
|
|
this.jukeboxDialog.show = true
|
|
|
|
let item = this.jukebox.items.find(item => item.id === itemId)
|
|
|
|
this.jukeboxDialog.data = item
|
|
|
|
},
|
2021-04-29 00:40:58 +01:00
|
|
|
createJukebox(){
|
|
|
|
self = this
|
2021-04-27 10:07:17 +01:00
|
|
|
|
2021-04-29 00:40:58 +01:00
|
|
|
LNbits.api.request(
|
|
|
|
'PUT',
|
|
|
|
'/jukebox/api/v1/jukebox/' + this.jukeboxDialog.data.sp_id,
|
|
|
|
self.g.user.wallets[0].adminkey,
|
|
|
|
self.jukeboxDialog.data
|
|
|
|
)
|
|
|
|
.then(response => {
|
|
|
|
console.log(response.data)
|
|
|
|
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
LNbits.utils.notifyApiError(err)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
playlistApi(method, url, body){
|
|
|
|
self = this
|
2021-04-28 12:04:47 +01:00
|
|
|
let xhr = new XMLHttpRequest()
|
|
|
|
xhr.open(method, url, true)
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/json')
|
2021-04-29 00:40:58 +01:00
|
|
|
xhr.setRequestHeader('Authorization', 'Bearer ' + this.jukeboxDialog.data.sp_access_token)
|
2021-04-28 12:04:47 +01:00
|
|
|
xhr.send(body)
|
2021-04-29 00:40:58 +01:00
|
|
|
xhr.onload = function() {
|
|
|
|
let responseObj = JSON.parse(xhr.response)
|
2021-04-29 08:47:58 +01:00
|
|
|
self.playlists = []
|
2021-04-29 00:40:58 +01:00
|
|
|
var i;
|
|
|
|
for (i = 0; i < responseObj.items.length; i++) {
|
|
|
|
self.playlists.push(responseObj.items[i].name + "-" + responseObj.items[i].id)
|
|
|
|
}
|
|
|
|
}
|
2021-04-28 12:04:47 +01:00
|
|
|
},
|
|
|
|
refreshPlaylists(){
|
2021-04-29 00:40:58 +01:00
|
|
|
self = this
|
|
|
|
self.playlistApi( "GET", "https://api.spotify.com/v1/me/playlists", null)
|
2021-04-27 10:07:17 +01:00
|
|
|
},
|
2021-04-29 00:40:58 +01:00
|
|
|
deviceApi(method, url, body){
|
|
|
|
self = this
|
|
|
|
let xhr = new XMLHttpRequest()
|
|
|
|
xhr.open(method, url, true)
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/json')
|
|
|
|
xhr.setRequestHeader('Authorization', 'Bearer ' + this.jukeboxDialog.data.sp_access_token)
|
|
|
|
xhr.send(body)
|
|
|
|
xhr.onload = function() {
|
2021-04-29 08:47:58 +01:00
|
|
|
let responseObj = JSON.parse(xhr.response)
|
|
|
|
console.log(responseObj.devices[0])
|
|
|
|
self.devices = []
|
2021-04-29 00:40:58 +01:00
|
|
|
var i;
|
2021-04-29 08:47:58 +01:00
|
|
|
for (i = 0; i < responseObj.devices.length; i++) {
|
|
|
|
self.devices.push(responseObj.devices[i].name + "-" + responseObj.devices[i].id)
|
|
|
|
console.log(responseObj.devices[i].name)
|
2021-04-29 00:40:58 +01:00
|
|
|
}
|
2021-04-29 08:47:58 +01:00
|
|
|
|
2021-04-27 10:07:17 +01:00
|
|
|
}
|
|
|
|
},
|
2021-04-29 00:40:58 +01:00
|
|
|
refreshDevices(){
|
|
|
|
self = this
|
|
|
|
self.deviceApi( "GET", "https://api.spotify.com/v1/me/player/devices", null)
|
|
|
|
},
|
|
|
|
fetchAccessToken( ){
|
|
|
|
self = this
|
|
|
|
let body = "grant_type=authorization_code"
|
|
|
|
body += "&code=" + self.jukeboxDialog.data.sp_access_token
|
2021-04-29 08:47:58 +01:00
|
|
|
body += '&redirect_uri=' + encodeURI(self.locationcbPath) + self.jukeboxDialog.data.sp_user
|
2021-04-29 00:40:58 +01:00
|
|
|
this.callAuthorizationApi(body)
|
|
|
|
},
|
2021-04-28 12:04:47 +01:00
|
|
|
refreshAccessToken(){
|
2021-04-29 00:40:58 +01:00
|
|
|
self = this
|
2021-04-28 12:04:47 +01:00
|
|
|
let body = "grant_type=refresh_token"
|
2021-04-29 00:40:58 +01:00
|
|
|
body += "&refresh_token=" + self.jukeboxDialog.data.sp_refresh_token
|
2021-04-28 12:04:47 +01:00
|
|
|
body += "&client_id=" + self.jukeboxDialog.data.sp_user
|
2021-04-29 00:40:58 +01:00
|
|
|
this.callAuthorizationApi(body)
|
2021-04-28 12:04:47 +01:00
|
|
|
},
|
|
|
|
callAuthorizationApi(body){
|
2021-04-29 00:40:58 +01:00
|
|
|
self = this
|
2021-04-28 12:04:47 +01:00
|
|
|
let xhr = new XMLHttpRequest()
|
2021-04-29 00:40:58 +01:00
|
|
|
xhr.open("POST", "https://accounts.spotify.com/api/token", true)
|
2021-04-28 12:04:47 +01:00
|
|
|
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
|
2021-04-29 00:40:58 +01:00
|
|
|
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(this.jukeboxDialog.data.sp_user + ":" + this.jukeboxDialog.data.sp_secret))
|
2021-04-28 12:04:47 +01:00
|
|
|
xhr.send(body)
|
2021-04-29 00:40:58 +01:00
|
|
|
console.log(('Authorization', 'Basic ' + btoa(this.jukeboxDialog.data.sp_user + ":" + this.jukeboxDialog.data.sp_secret)))
|
|
|
|
xhr.onload = function() {
|
|
|
|
let responseObj = JSON.parse(xhr.response)
|
|
|
|
alert(responseObj.access_token)
|
|
|
|
alert(responseObj.refresh_token)
|
|
|
|
self.jukeboxDialog.data.sp_access_token = responseObj.access_token
|
|
|
|
self.jukeboxDialog.data.sp_refresh_token = responseObj.refresh_token
|
|
|
|
console.log(self.jukeboxDialog.data)
|
|
|
|
self.refreshPlaylists()
|
|
|
|
self.refreshDevices()
|
|
|
|
}
|
2021-04-27 10:07:17 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
created() {
|
|
|
|
this.selectedWallet = this.g.user.wallets[0]
|
2021-04-28 12:04:47 +01:00
|
|
|
this.locationcbPath = String([
|
|
|
|
window.location.protocol,
|
|
|
|
'//',
|
|
|
|
window.location.host,
|
|
|
|
'/jukebox/api/v1/jukebox/spotify/cb/'
|
|
|
|
].join(''))
|
2021-04-29 00:40:58 +01:00
|
|
|
console.log(this.locationcbPath)
|
|
|
|
this.locationcb = this.locationcbPath
|
2021-04-27 10:07:17 +01:00
|
|
|
}
|
|
|
|
})
|