added some checks to make sure the jukebox is connected

This commit is contained in:
Ben Arc 2021-06-11 15:42:50 +01:00
parent ff80f3d585
commit 7d615db8e2
4 changed files with 203 additions and 33 deletions

View File

@ -0,0 +1,35 @@
{% extends "public.html" %} {% block page %}
<div class="row q-col-gutter-md justify-center">
<div class="col-12 col-md-7 col-lg-6 q-gutter-y-md">
<q-card class="q-pa-lg">
<q-card-section class="q-pa-none">
<center>
<h3 class="q-my-none">Jukebox error</h3>
<br />
<q-icon
name="warning"
class="text-grey"
style="font-size: 20rem"
></q-icon>
<h5 class="q-my-none">Ask the host to turn on the device and launch spotify</h5>
<br />
</center>
</q-card-section>
</q-card>
</div>
{% endblock %} {% block scripts %}
<script>
new Vue({
el: '#vue',
mixins: [windowMixin],
data: function () {
return {}
}
})
</script>
{% endblock %}
</div>

View File

@ -170,7 +170,7 @@
'Success! "' + self.receive.name + '" will be played soon', 'Success! "' + self.receive.name + '" will be played soon',
timeout: 3000 timeout: 3000
}) })
self.getCurrent()
self.paid = false self.paid = false
response1 = [] response1 = []
} }
@ -183,10 +183,12 @@
}) })
} }
}, 4000) }, 4000)
}) })
.catch(err => { .catch(err => {
LNbits.utils.notifyApiError(err) LNbits.utils.notifyApiError(err)
}) })
self.getCurrent()
}, },
checkInvoice(juke_id, paymentHash) { checkInvoice(juke_id, paymentHash) {
@ -219,6 +221,7 @@
.catch(function (error) { .catch(function (error) {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
}) })
}, },
selectPlaylist() { selectPlaylist() {
self = this self = this

View File

@ -10,6 +10,7 @@ from functools import wraps
import json import json
from . import jukebox_ext from . import jukebox_ext
from .crud import get_jukebox from .crud import get_jukebox
from .views_api import api_get_jukebox_device_check
from urllib.parse import unquote from urllib.parse import unquote
@ -25,11 +26,15 @@ async def print_qr_codes(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)
return await render_template( devices = json.loads(device[0].text)
"jukebox/jukebox.html", if len(devices["devices"]) > 0:
playlists=jukebox.sp_playlists.split(","), return await render_template(
juke_id=juke_id, "jukebox/jukebox.html",
price=jukebox.price, playlists=jukebox.sp_playlists.split(","),
inkey=jukebox.inkey, juke_id=juke_id,
) price=jukebox.price,
inkey=jukebox.inkey,
)
else:
return await render_template("jukebox/error.html")

View File

@ -48,7 +48,13 @@ async def api_check_credentials_callbac(juke_id):
sp_code = "" sp_code = ""
sp_access_token = "" sp_access_token = ""
sp_refresh_token = "" sp_refresh_token = ""
jukebox = await get_jukebox(juke_id) try:
jukebox = await get_jukebox(juke_id)
except:
return (
jsonify({"error": "No Jukebox"}),
HTTPStatus.FORBIDDEN,
)
if request.args.get("code"): if request.args.get("code"):
sp_code = request.args.get("code") sp_code = request.args.get("code")
jukebox = await update_jukebox( jukebox = await update_jukebox(
@ -122,8 +128,14 @@ async def api_delete_item(juke_id):
@jukebox_ext.route( @jukebox_ext.route(
"/api/v1/jukebox/jb/playlist/<juke_id>/<sp_playlist>", methods=["GET"] "/api/v1/jukebox/jb/playlist/<juke_id>/<sp_playlist>", methods=["GET"]
) )
async def api_get_jukebox_song(juke_id, sp_playlist): async def api_get_jukebox_song(juke_id, sp_playlist, retry=False):
jukebox = await get_jukebox(juke_id) try:
jukebox = await get_jukebox(juke_id)
except:
return (
jsonify({"error": "No Jukebox"}),
HTTPStatus.FORBIDDEN,
)
tracks = [] tracks = []
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
try: try:
@ -137,8 +149,15 @@ async def api_get_jukebox_song(juke_id, sp_playlist):
token = await api_get_token(juke_id) token = await api_get_token(juke_id)
if token == False: if token == False:
return False return False
elif retry:
return (
jsonify({"error": "Failed to get auth"}),
HTTPStatus.FORBIDDEN,
)
else: else:
return await api_get_jukebox_song(juke_id, sp_playlist) return await api_get_jukebox_song(
juke_id, sp_playlist, retry=True
)
return r, HTTPStatus.OK return r, HTTPStatus.OK
for item in r.json()["items"]: for item in r.json()["items"]:
tracks.append( tracks.append(
@ -156,7 +175,13 @@ async def api_get_jukebox_song(juke_id, sp_playlist):
async def api_get_token(juke_id): async def api_get_token(juke_id):
jukebox = await get_jukebox(juke_id) try:
jukebox = await get_jukebox(juke_id)
except:
return (
jsonify({"error": "No Jukebox"}),
HTTPStatus.FORBIDDEN,
)
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
try: try:
@ -188,12 +213,63 @@ async def api_get_token(juke_id):
return True return True
######CHECK DEVICE
@jukebox_ext.route("/api/v1/jukebox/jb/<juke_id>", methods=["GET"])
async def api_get_jukebox_device_check(juke_id, retry=False):
try:
jukebox = await get_jukebox(juke_id)
except:
return (
jsonify({"error": "No Jukebox"}),
HTTPStatus.FORBIDDEN,
)
async with httpx.AsyncClient() as client:
rDevice = await client.get(
"https://api.spotify.com/v1/me/player/devices",
timeout=40,
headers={"Authorization": "Bearer " + jukebox.sp_access_token},
)
if rDevice.status_code == 204 or rDevice.status_code == 200:
return (
rDevice,
HTTPStatus.OK,
)
elif rDevice.status_code == 401 or rDevice.status_code == 403:
token = await api_get_token(juke_id)
if token == False:
return (
jsonify({"error": "No device connected"}),
HTTPStatus.FORBIDDEN,
)
elif retry:
return (
jsonify({"error": "Failed to get auth"}),
HTTPStatus.FORBIDDEN,
)
else:
return api_get_jukebox_device_check(juke_id, retry=True)
else:
return (
jsonify({"error": "No device connected"}),
HTTPStatus.FORBIDDEN,
)
######GET INVOICE STUFF ######GET INVOICE STUFF
@jukebox_ext.route("/api/v1/jukebox/jb/invoice/<juke_id>/<song_id>", methods=["GET"]) @jukebox_ext.route("/api/v1/jukebox/jb/invoice/<juke_id>/<song_id>", methods=["GET"])
async def api_get_jukebox_invoice(juke_id, song_id): async def api_get_jukebox_invoice(juke_id, song_id):
jukebox = await get_jukebox(juke_id) try:
jukebox = await get_jukebox(juke_id)
except:
return (
jsonify({"error": "No Jukebox"}),
HTTPStatus.FORBIDDEN,
)
invoice = await create_invoice( invoice = await create_invoice(
wallet_id=jukebox.wallet, wallet_id=jukebox.wallet,
@ -211,7 +287,13 @@ async def api_get_jukebox_invoice(juke_id, song_id):
"/api/v1/jukebox/jb/checkinvoice/<pay_hash>/<juke_id>", methods=["GET"] "/api/v1/jukebox/jb/checkinvoice/<pay_hash>/<juke_id>", methods=["GET"]
) )
async def api_get_jukebox_invoice_check(pay_hash, juke_id): async def api_get_jukebox_invoice_check(pay_hash, juke_id):
jukebox = await get_jukebox(juke_id) try:
jukebox = await get_jukebox(juke_id)
except:
return (
jsonify({"error": "No Jukebox"}),
HTTPStatus.FORBIDDEN,
)
try: try:
status = await check_invoice_status(jukebox.wallet, pay_hash) status = await check_invoice_status(jukebox.wallet, pay_hash)
is_paid = not status.pending is_paid = not status.pending
@ -229,8 +311,14 @@ async def api_get_jukebox_invoice_check(pay_hash, juke_id):
@jukebox_ext.route( @jukebox_ext.route(
"/api/v1/jukebox/jb/invoicep/<song_id>/<juke_id>/<pay_hash>", methods=["GET"] "/api/v1/jukebox/jb/invoicep/<song_id>/<juke_id>/<pay_hash>", methods=["GET"]
) )
async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash): async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash, retry=False):
jukebox = await get_jukebox(juke_id) try:
jukebox = await get_jukebox(juke_id)
except:
return (
jsonify({"error": "No Jukebox"}),
HTTPStatus.FORBIDDEN,
)
jukebox_payment = await get_jukebox_payment(pay_hash) jukebox_payment = await get_jukebox_payment(pay_hash)
if jukebox_payment.paid: if jukebox_payment.paid:
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
@ -239,7 +327,16 @@ async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash):
timeout=40, timeout=40,
headers={"Authorization": "Bearer " + jukebox.sp_access_token}, headers={"Authorization": "Bearer " + jukebox.sp_access_token},
) )
if r.status_code == 204: rDevice = await client.get(
"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 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(
@ -258,9 +355,14 @@ async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash):
jsonify({"error": "Invoice not paid"}), jsonify({"error": "Invoice not paid"}),
HTTPStatus.FORBIDDEN, HTTPStatus.FORBIDDEN,
) )
elif retry:
return (
jsonify({"error": "Failed to get auth"}),
HTTPStatus.FORBIDDEN,
)
else: else:
return api_get_jukebox_invoice_paid( return api_get_jukebox_invoice_paid(
song_id, juke_id, pay_hash song_id, juke_id, pay_hash, retry=True
) )
else: else:
return ( return (
@ -287,6 +389,11 @@ async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash):
jsonify({"error": "Invoice not paid"}), jsonify({"error": "Invoice not paid"}),
HTTPStatus.OK, HTTPStatus.OK,
) )
elif retry:
return (
jsonify({"error": "Failed to get auth"}),
HTTPStatus.FORBIDDEN,
)
else: else:
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
@ -303,6 +410,11 @@ async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash):
jsonify({"error": "Invoice not paid"}), jsonify({"error": "Invoice not paid"}),
HTTPStatus.OK, HTTPStatus.OK,
) )
elif retry:
return (
jsonify({"error": "Failed to get auth"}),
HTTPStatus.FORBIDDEN,
)
else: else:
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
@ -314,8 +426,14 @@ async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash):
@jukebox_ext.route("/api/v1/jukebox/jb/currently/<juke_id>", methods=["GET"]) @jukebox_ext.route("/api/v1/jukebox/jb/currently/<juke_id>", methods=["GET"])
async def api_get_jukebox_currently(juke_id): async def api_get_jukebox_currently(juke_id, retry=False):
jukebox = await get_jukebox(juke_id) try:
jukebox = await get_jukebox(juke_id)
except:
return (
jsonify({"error": "No Jukebox"}),
HTTPStatus.FORBIDDEN,
)
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
try: try:
r = await client.get( r = await client.get(
@ -326,16 +444,20 @@ async def api_get_jukebox_currently(juke_id):
if r.status_code == 204: if r.status_code == 204:
return jsonify({"error": "Nothing"}), HTTPStatus.OK return jsonify({"error": "Nothing"}), HTTPStatus.OK
elif r.status_code == 200: elif r.status_code == 200:
response = r.json() try:
response["item"] response = r.json()
track = { response["item"]
"id": response["item"]["id"], track = {
"name": response["item"]["name"], "id": response["item"]["id"],
"album": response["item"]["album"]["name"], "name": response["item"]["name"],
"artist": response["item"]["artists"][0]["name"], "album": response["item"]["album"]["name"],
"image": response["item"]["album"]["images"][0]["url"], "artist": response["item"]["artists"][0]["name"],
} "image": response["item"]["album"]["images"][0]["url"],
return track, HTTPStatus.OK }
return track, HTTPStatus.OK
except:
return jsonify("Something went wrong"), HTTPStatus.NOT_FOUND
elif r.status_code == 401: elif r.status_code == 401:
token = await api_get_token(juke_id) token = await api_get_token(juke_id)
if token == False: if token == False:
@ -343,8 +465,13 @@ async def api_get_jukebox_currently(juke_id):
jsonify({"error": "Invoice not paid"}), jsonify({"error": "Invoice not paid"}),
HTTPStatus.FORBIDDEN, HTTPStatus.FORBIDDEN,
) )
elif retry:
return (
jsonify({"error": "Failed to get auth"}),
HTTPStatus.FORBIDDEN,
)
else: else:
return await api_get_jukebox_currently(juke_id) return await api_get_jukebox_currently(juke_id, retry=True)
else: else:
return jsonify("Something went wrong"), HTTPStatus.NOT_FOUND return jsonify("Something went wrong"), HTTPStatus.NOT_FOUND
except AssertionError: except AssertionError: