mirror of
https://github.com/lnbits/lnbits.git
synced 2025-09-20 04:21:20 +02:00
Added backend websocket and queue maker
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
import time
|
import time
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from quart import g, render_template, request, jsonify
|
from quart import g, render_template, request, jsonify, websocket
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
|
import trio
|
||||||
from lnbits.decorators import check_user_exists, validate_uuids
|
from lnbits.decorators import check_user_exists, validate_uuids
|
||||||
from lnbits.core.models import Payment
|
from lnbits.core.models import Payment
|
||||||
|
|
||||||
@@ -30,8 +30,39 @@ async def print_qr_codes(juke_id):
|
|||||||
playlists=jukebox.sp_playlists.split(","),
|
playlists=jukebox.sp_playlists.split(","),
|
||||||
juke_id=juke_id,
|
juke_id=juke_id,
|
||||||
price=jukebox.price,
|
price=jukebox.price,
|
||||||
inkey=jukebox.inkey
|
inkey=jukebox.inkey,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
##################WEBSOCKET ROUTES########################
|
||||||
|
|
||||||
|
|
||||||
|
connected_websockets = set()
|
||||||
|
|
||||||
|
|
||||||
|
def collect_websocket(func):
|
||||||
|
@wraps(func)
|
||||||
|
async def wrapper(*args, **kwargs):
|
||||||
|
global connected_websockets
|
||||||
|
send_channel, receive_channel = trio.open_memory_channel(0)
|
||||||
|
connected_websockets.add(send_channel)
|
||||||
|
try:
|
||||||
|
return await func(receive_channel, *args, **kwargs)
|
||||||
|
finally:
|
||||||
|
connected_websockets.remove(send_channel)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
@jukebox_ext.websocket("/ws")
|
||||||
|
@collect_websocket
|
||||||
|
async def wss(receive_channel):
|
||||||
|
while True:
|
||||||
|
data = await receive_channel.receive()
|
||||||
|
await websocket.send(data)
|
||||||
|
|
||||||
|
|
||||||
|
async def broadcast(message):
|
||||||
|
print(connected_websockets)
|
||||||
|
for queue in connected_websockets:
|
||||||
|
await queue.send(f"{message}")
|
||||||
|
@@ -10,6 +10,7 @@ import time
|
|||||||
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
||||||
import httpx
|
import httpx
|
||||||
from . import jukebox_ext
|
from . import jukebox_ext
|
||||||
|
from .views import broadcast
|
||||||
from .crud import (
|
from .crud import (
|
||||||
create_jukebox,
|
create_jukebox,
|
||||||
update_jukebox,
|
update_jukebox,
|
||||||
@@ -121,7 +122,7 @@ 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_son(juke_id, sp_playlist):
|
async def api_get_jukebox_song(juke_id, sp_playlist):
|
||||||
jukebox = await get_jukebox(juke_id)
|
jukebox = await get_jukebox(juke_id)
|
||||||
tracks = []
|
tracks = []
|
||||||
async with httpx.AsyncClient() as client:
|
async with httpx.AsyncClient() as client:
|
||||||
@@ -138,7 +139,7 @@ async def api_get_jukebox_son(juke_id, sp_playlist):
|
|||||||
|
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
return await api_get_jukebox_son(juke_id, sp_playlist)
|
return await api_get_jukebox_song(juke_id, sp_playlist)
|
||||||
return r, HTTPStatus.OK
|
return r, HTTPStatus.OK
|
||||||
for item in r.json()["items"]:
|
for item in r.json()["items"]:
|
||||||
tracks.append(
|
tracks.append(
|
||||||
@@ -155,9 +156,6 @@ async def api_get_jukebox_son(juke_id, sp_playlist):
|
|||||||
return jsonify([track for track in tracks])
|
return jsonify([track for track in tracks])
|
||||||
|
|
||||||
|
|
||||||
# return jsonify([track for track in tracks])
|
|
||||||
|
|
||||||
|
|
||||||
async def api_get_token(juke_id):
|
async def api_get_token(juke_id):
|
||||||
jukebox = await get_jukebox(juke_id)
|
jukebox = await get_jukebox(juke_id)
|
||||||
|
|
||||||
@@ -221,12 +219,10 @@ async def api_get_jukebox_invoice_paid(payment_hash, juke_id):
|
|||||||
jukebox_payment = await update_jukebox_payment(payment_hash, paid=True)
|
jukebox_payment = await update_jukebox_payment(payment_hash, paid=True)
|
||||||
else:
|
else:
|
||||||
return jsonify({"error": "Invoice not paid"})
|
return jsonify({"error": "Invoice not paid"})
|
||||||
|
queue = await add_to_song_queue(jukebox_payment.song_id, jukebox_payment.juke_id)
|
||||||
|
return queue
|
||||||
|
|
||||||
|
|
||||||
# if not is_paid:
|
|
||||||
# return jsonify({"status": False})
|
|
||||||
# return jsonify({"error": "Something went wrong"})
|
|
||||||
|
|
||||||
############################QUEUE SONG
|
############################QUEUE SONG
|
||||||
|
|
||||||
|
|
||||||
@@ -245,7 +241,7 @@ async def add_to_song_queue(song_id, juke_id):
|
|||||||
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"
|
||||||
+ jukebox_payment.song_id
|
+ queued[0]
|
||||||
+ "&device_id="
|
+ "&device_id="
|
||||||
+ jukebox.sp_device.split("-")[1],
|
+ jukebox.sp_device.split("-")[1],
|
||||||
timeout=40,
|
timeout=40,
|
||||||
@@ -256,36 +252,11 @@ async def add_to_song_queue(song_id, juke_id):
|
|||||||
queued = queued[1:]
|
queued = queued[1:]
|
||||||
jukebox = await update_jukebox(juke_id=juke_id, queue=queued)
|
jukebox = await update_jukebox(juke_id=juke_id, queue=queued)
|
||||||
queued = jukebox.queue
|
queued = jukebox.queue
|
||||||
|
broadcast(
|
||||||
|
json.dumps({"juke_id": juke_id, "queue": queued, "current": song})
|
||||||
|
)
|
||||||
jukebox = await update_jukebox(juke_id=juke_id, last_checked=time.time())
|
jukebox = await update_jukebox(juke_id=juke_id, last_checked=time.time())
|
||||||
|
return jsonify(jukebox), HTTPStatus.OK
|
||||||
# if current track playing isnt at the front of the queue, add it to queue
|
|
||||||
|
|
||||||
print(jukebox)
|
|
||||||
paid = await check_invoice_status(jukebox.wallet, payment_hash)
|
|
||||||
if paid:
|
|
||||||
jukebox_payment = await update_jukebox_payment(payment_hash, paid=True)
|
|
||||||
else:
|
|
||||||
return jsonify({"error": "Invoice not paid"})
|
|
||||||
async with httpx.AsyncClient() as client:
|
|
||||||
|
|
||||||
r = await client.post(
|
|
||||||
"https://api.spotify.com/v1/me/player/queue?uri=spotify%3Atrack%3A"
|
|
||||||
+ jukebox_payment.song_id
|
|
||||||
+ "&device_id="
|
|
||||||
+ jukebox.sp_device.split("-")[1],
|
|
||||||
timeout=40,
|
|
||||||
headers={"Authorization": "Bearer " + jukebox.sp_access_token},
|
|
||||||
)
|
|
||||||
print(r)
|
|
||||||
if r.json()["error"]["status"] == 401:
|
|
||||||
token = await api_get_token(juke_id)
|
|
||||||
if token == False:
|
|
||||||
return jsonify({"error": "Something went wrong"})
|
|
||||||
else:
|
|
||||||
return await api_get_jukebox_invoice_paid(juke_id, payment_hash)
|
|
||||||
if r.json()["error"]["status"] == 400:
|
|
||||||
return jsonify({"error": "Something went wrong"})
|
|
||||||
return jsonify(r), HTTPStatus.OK
|
|
||||||
|
|
||||||
|
|
||||||
############################GET TRACKS
|
############################GET TRACKS
|
||||||
|
Reference in New Issue
Block a user