From 78ad57f99fc7fb7f6ab2de8ff8ae0b662f2207cb Mon Sep 17 00:00:00 2001 From: Ben Arc Date: Fri, 11 Jun 2021 20:27:13 +0100 Subject: [PATCH] Fixed device search --- .../jukebox/templates/jukebox/jukebox.html | 8 +++++--- lnbits/extensions/jukebox/views.py | 12 ++++++++---- lnbits/extensions/jukebox/views_api.py | 16 +++++++++------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lnbits/extensions/jukebox/templates/jukebox/jukebox.html b/lnbits/extensions/jukebox/templates/jukebox/jukebox.html index b8e65bbc1..5089daa18 100644 --- a/lnbits/extensions/jukebox/templates/jukebox/jukebox.html +++ b/lnbits/extensions/jukebox/templates/jukebox/jukebox.html @@ -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 } }) diff --git a/lnbits/extensions/jukebox/views.py b/lnbits/extensions/jukebox/views.py index 39bab6a21..ac7a7a150 100644 --- a/lnbits/extensions/jukebox/views.py +++ b/lnbits/extensions/jukebox/views.py @@ -22,13 +22,17 @@ async def index(): @jukebox_ext.route("/") -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(","), diff --git a/lnbits/extensions/jukebox/views_api.py b/lnbits/extensions/jukebox/views_api.py index 047a14959..ac5147254 100644 --- a/lnbits/extensions/jukebox/views_api.py +++ b/lnbits/extensions/jukebox/views_api.py @@ -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