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

View File

@@ -22,13 +22,17 @@ async def index():
@jukebox_ext.route("/<juke_id>") @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) jukebox = await get_jukebox(juke_id)
if not jukebox: if not jukebox:
return "error" return "error"
device = await api_get_jukebox_device_check(juke_id) deviceCheck = await api_get_jukebox_device_check(juke_id)
devices = json.loads(device[0].text) devices = json.loads(deviceCheck[0].text)
if len(devices["devices"]) > 0: deviceConnected = False
for device in devices["devices"]:
if device["id"] == jukebox.sp_device.split("-")[1]:
deviceConnected = True
if deviceConnected:
return await render_template( return await render_template(
"jukebox/jukebox.html", "jukebox/jukebox.html",
playlists=jukebox.sp_playlists.split(","), 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}, headers={"Authorization": "Bearer " + jukebox.sp_access_token},
) )
rDevice = await client.get( rDevice = await client.get(
"https://api.spotify.com/v1/me/player/devices", "https://api.spotify.com/v1/me/player",
timeout=40, timeout=40,
headers={"Authorization": "Bearer " + jukebox.sp_access_token}, 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: async with httpx.AsyncClient() as client:
uri = ["spotify:track:" + song_id] uri = ["spotify:track:" + song_id]
r = await client.put( 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"}), jsonify({"error": "Invoice not paid"}),
HTTPStatus.FORBIDDEN, HTTPStatus.FORBIDDEN,
) )
elif r.status_code == 200 and rDevice.json()["devices"] > 0: elif r.status_code == 200:
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
r = await client.post( r = await client.post(
"https://api.spotify.com/v1/me/player/queue?uri=spotify%3Atrack%3A" "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( return await api_get_jukebox_invoice_paid(
song_id, juke_id, pay_hash song_id, juke_id, pay_hash
) )
return jsonify({"error": "Failed to play"}), HTTPStatus.FORBIDDEN return jsonify({"error": "Invoice not paid"}), HTTPStatus.OK
############################GET TRACKS ############################GET TRACKS
@@ -444,7 +446,7 @@ async def api_get_jukebox_currently(juke_id, retry=False):
elif r.status_code == 200: elif r.status_code == 200:
try: try:
response = r.json() response = r.json()
response["item"]
track = { track = {
"id": response["item"]["id"], "id": response["item"]["id"],
"name": response["item"]["name"], "name": response["item"]["name"],
@@ -452,7 +454,7 @@ async def api_get_jukebox_currently(juke_id, retry=False):
"artist": response["item"]["artists"][0]["name"], "artist": response["item"]["artists"][0]["name"],
"image": response["item"]["album"]["images"][0]["url"], "image": response["item"]["album"]["images"][0]["url"],
} }
return track, HTTPStatus.OK return jsonify(track), HTTPStatus.OK
except: except:
return jsonify("Something went wrong"), HTTPStatus.NOT_FOUND return jsonify("Something went wrong"), HTTPStatus.NOT_FOUND