Fixed device search

This commit is contained in:
Ben Arc 2021-06-11 20:27:13 +01:00
parent 10a5db403a
commit 78ad57f99f
3 changed files with 22 additions and 14 deletions

View file

@ -163,7 +163,7 @@
.then(function (response1) {
console.log(response1)
if (response1.data[2] == song_id) {
setTimeout(function(){ self.getCurrent() }, 500)
self.$q.notify({
color: 'green',
message:
@ -182,13 +182,14 @@
response1 = []
})
}
}, 4000)
}, 5000)
})
.catch(err => {
LNbits.utils.notifyApiError(err)
})
self.getCurrent()
},
checkInvoice(juke_id, paymentHash) {
@ -215,6 +216,7 @@
'/jukebox/api/v1/jukebox/jb/currently/{{juke_id}}')
.then(function (res) {
if (res.data.id) {
console.log(res.data)
self.currentlyPlaying = res.data
}
})

View file

@ -22,13 +22,17 @@ async def index():
@jukebox_ext.route("/<juke_id>")
async def print_qr_codes(juke_id):
async def connect_to_jukebox(juke_id):
jukebox = await get_jukebox(juke_id)
if not jukebox:
return "error"
device = await api_get_jukebox_device_check(juke_id)
devices = json.loads(device[0].text)
if len(devices["devices"]) > 0:
deviceCheck = await api_get_jukebox_device_check(juke_id)
devices = json.loads(deviceCheck[0].text)
deviceConnected = False
for device in devices["devices"]:
if device["id"] == jukebox.sp_device.split("-")[1]:
deviceConnected = True
if deviceConnected:
return await render_template(
"jukebox/jukebox.html",
playlists=jukebox.sp_playlists.split(","),

View file

@ -328,13 +328,15 @@ async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash, retry=False):
headers={"Authorization": "Bearer " + jukebox.sp_access_token},
)
rDevice = await client.get(
"https://api.spotify.com/v1/me/player/devices",
"https://api.spotify.com/v1/me/player",
timeout=40,
headers={"Authorization": "Bearer " + jukebox.sp_access_token},
)
]
isPlaying = False
if rDevice.status_code == 200:
isPlaying = rDevice.json()["is_playing"]
if r.status_code == 204 and rDevice.json()["devices"] > 0:
if r.status_code == 204 or isPlaying == False:
async with httpx.AsyncClient() as client:
uri = ["spotify:track:" + song_id]
r = await client.put(
@ -367,7 +369,7 @@ async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash, retry=False):
jsonify({"error": "Invoice not paid"}),
HTTPStatus.FORBIDDEN,
)
elif r.status_code == 200 and rDevice.json()["devices"] > 0:
elif r.status_code == 200:
async with httpx.AsyncClient() as client:
r = await client.post(
"https://api.spotify.com/v1/me/player/queue?uri=spotify%3Atrack%3A"
@ -417,7 +419,7 @@ async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash, retry=False):
return await api_get_jukebox_invoice_paid(
song_id, juke_id, pay_hash
)
return jsonify({"error": "Failed to play"}), HTTPStatus.FORBIDDEN
return jsonify({"error": "Invoice not paid"}), HTTPStatus.OK
############################GET TRACKS
@ -444,7 +446,7 @@ async def api_get_jukebox_currently(juke_id, retry=False):
elif r.status_code == 200:
try:
response = r.json()
response["item"]
track = {
"id": response["item"]["id"],
"name": response["item"]["name"],
@ -452,7 +454,7 @@ async def api_get_jukebox_currently(juke_id, retry=False):
"artist": response["item"]["artists"][0]["name"],
"image": response["item"]["album"]["images"][0]["url"],
}
return track, HTTPStatus.OK
return jsonify(track), HTTPStatus.OK
except:
return jsonify("Something went wrong"), HTTPStatus.NOT_FOUND