2021-04-28 12:04:47 +01:00
|
|
|
from quart import g, jsonify, request
|
2021-04-27 10:07:17 +01:00
|
|
|
from http import HTTPStatus
|
2021-05-05 19:20:53 +01:00
|
|
|
import base64
|
2021-06-07 23:29:40 +01:00
|
|
|
from lnbits.core.crud import get_wallet
|
|
|
|
from lnbits.core.services import create_invoice, check_invoice_status
|
2021-06-13 12:58:46 +01:00
|
|
|
import json
|
2021-04-27 10:07:17 +01:00
|
|
|
|
|
|
|
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
2021-04-28 12:04:47 +01:00
|
|
|
import httpx
|
2021-04-27 10:07:17 +01:00
|
|
|
from . import jukebox_ext
|
|
|
|
from .crud import (
|
2021-04-28 12:04:47 +01:00
|
|
|
create_jukebox,
|
|
|
|
update_jukebox,
|
2021-04-27 10:07:17 +01:00
|
|
|
get_jukebox,
|
|
|
|
get_jukeboxs,
|
|
|
|
delete_jukebox,
|
2021-06-01 16:50:06 +01:00
|
|
|
create_jukebox_payment,
|
|
|
|
get_jukebox_payment,
|
|
|
|
update_jukebox_payment,
|
2021-04-27 10:07:17 +01:00
|
|
|
)
|
2021-06-01 16:50:06 +01:00
|
|
|
from lnbits.core.services import create_invoice, check_invoice_status
|
2021-04-27 10:07:17 +01:00
|
|
|
|
2021-06-07 12:03:13 +01:00
|
|
|
|
2021-04-27 10:07:17 +01:00
|
|
|
@jukebox_ext.route("/api/v1/jukebox", methods=["GET"])
|
2021-06-13 12:58:46 +01:00
|
|
|
@api_check_wallet_key("admin")
|
2021-04-27 10:07:17 +01:00
|
|
|
async def api_get_jukeboxs():
|
2021-04-29 22:38:57 +01:00
|
|
|
try:
|
|
|
|
return (
|
|
|
|
jsonify(
|
2021-05-03 23:22:40 +01:00
|
|
|
[{**jukebox._asdict()} for jukebox in await get_jukeboxs(g.wallet.user)]
|
2021-04-29 22:38:57 +01:00
|
|
|
),
|
|
|
|
HTTPStatus.OK,
|
|
|
|
)
|
|
|
|
except:
|
|
|
|
return "", HTTPStatus.NO_CONTENT
|
2021-04-28 12:04:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
##################SPOTIFY AUTH#####################
|
|
|
|
|
|
|
|
|
2021-05-03 23:22:40 +01:00
|
|
|
@jukebox_ext.route("/api/v1/jukebox/spotify/cb/<juke_id>", methods=["GET"])
|
|
|
|
async def api_check_credentials_callbac(juke_id):
|
2021-04-29 00:40:58 +01:00
|
|
|
sp_code = ""
|
|
|
|
sp_access_token = ""
|
|
|
|
sp_refresh_token = ""
|
2021-06-11 15:42:50 +01:00
|
|
|
try:
|
|
|
|
jukebox = await get_jukebox(juke_id)
|
|
|
|
except:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "No Jukebox"}),
|
|
|
|
HTTPStatus.FORBIDDEN,
|
|
|
|
)
|
2021-04-29 08:47:58 +01:00
|
|
|
if request.args.get("code"):
|
|
|
|
sp_code = request.args.get("code")
|
2021-04-29 00:40:58 +01:00
|
|
|
jukebox = await update_jukebox(
|
2021-05-03 23:22:40 +01:00
|
|
|
juke_id=juke_id, sp_secret=jukebox.sp_secret, sp_access_token=sp_code
|
2021-04-29 00:40:58 +01:00
|
|
|
)
|
2021-04-29 08:47:58 +01:00
|
|
|
if request.args.get("access_token"):
|
|
|
|
sp_access_token = request.args.get("access_token")
|
|
|
|
sp_refresh_token = request.args.get("refresh_token")
|
2021-04-29 00:40:58 +01:00
|
|
|
jukebox = await update_jukebox(
|
2021-05-03 23:22:40 +01:00
|
|
|
juke_id=juke_id,
|
2021-04-29 08:47:58 +01:00
|
|
|
sp_secret=jukebox.sp_secret,
|
|
|
|
sp_access_token=sp_access_token,
|
|
|
|
sp_refresh_token=sp_refresh_token,
|
2021-04-29 00:40:58 +01:00
|
|
|
)
|
2021-04-28 12:04:47 +01:00
|
|
|
return "<h1>Success!</h1><h2>You can close this window</h2>"
|
2021-04-27 10:07:17 +01:00
|
|
|
|
2021-04-29 08:47:58 +01:00
|
|
|
|
2021-06-13 12:58:46 +01:00
|
|
|
@jukebox_ext.route("/api/v1/jukebox/<juke_id>", methods=["GET"])
|
|
|
|
@api_check_wallet_key("admin")
|
|
|
|
async def api_check_credentials_check(juke_id):
|
|
|
|
jukebox = await get_jukebox(juke_id)
|
2021-04-28 12:04:47 +01:00
|
|
|
return jsonify(jukebox._asdict()), HTTPStatus.CREATED
|
2021-04-27 10:07:17 +01:00
|
|
|
|
2021-04-28 12:04:47 +01:00
|
|
|
|
|
|
|
@jukebox_ext.route("/api/v1/jukebox/", methods=["POST"])
|
2021-05-03 23:22:40 +01:00
|
|
|
@jukebox_ext.route("/api/v1/jukebox/<juke_id>", methods=["PUT"])
|
2021-04-27 10:07:17 +01:00
|
|
|
@api_check_wallet_key("admin")
|
|
|
|
@api_validate_post_request(
|
|
|
|
schema={
|
2021-04-29 22:38:57 +01:00
|
|
|
"user": {"type": "string", "empty": False, "required": True},
|
2021-04-28 12:04:47 +01:00
|
|
|
"title": {"type": "string", "empty": False, "required": True},
|
|
|
|
"wallet": {"type": "string", "empty": False, "required": True},
|
|
|
|
"sp_user": {"type": "string", "empty": False, "required": True},
|
|
|
|
"sp_secret": {"type": "string", "required": True},
|
2021-04-29 00:40:58 +01:00
|
|
|
"sp_access_token": {"type": "string", "required": False},
|
|
|
|
"sp_refresh_token": {"type": "string", "required": False},
|
2021-04-28 12:04:47 +01:00
|
|
|
"sp_device": {"type": "string", "required": False},
|
|
|
|
"sp_playlists": {"type": "string", "required": False},
|
2021-06-13 12:58:46 +01:00
|
|
|
"price": {"type": "string", "required": False},
|
2021-04-27 10:07:17 +01:00
|
|
|
}
|
|
|
|
)
|
2021-05-03 23:22:40 +01:00
|
|
|
async def api_create_update_jukebox(juke_id=None):
|
|
|
|
if juke_id:
|
2021-06-07 12:03:13 +01:00
|
|
|
jukebox = await update_jukebox(juke_id=juke_id, inkey=g.wallet.inkey, **g.data)
|
2021-04-29 22:38:57 +01:00
|
|
|
else:
|
2021-06-07 12:03:13 +01:00
|
|
|
jukebox = await create_jukebox(inkey=g.wallet.inkey, **g.data)
|
|
|
|
|
2021-04-27 10:07:17 +01:00
|
|
|
return jsonify(jukebox._asdict()), HTTPStatus.CREATED
|
|
|
|
|
|
|
|
|
|
|
|
@jukebox_ext.route("/api/v1/jukebox/<juke_id>", methods=["DELETE"])
|
|
|
|
@api_check_wallet_key("admin")
|
|
|
|
async def api_delete_item(juke_id):
|
2021-04-29 22:38:57 +01:00
|
|
|
await delete_jukebox(juke_id)
|
|
|
|
try:
|
|
|
|
return (
|
|
|
|
jsonify(
|
2021-05-03 23:22:40 +01:00
|
|
|
[{**jukebox._asdict()} for jukebox in await get_jukeboxs(g.wallet.user)]
|
2021-04-29 22:38:57 +01:00
|
|
|
),
|
|
|
|
HTTPStatus.OK,
|
|
|
|
)
|
|
|
|
except:
|
|
|
|
return "", HTTPStatus.NO_CONTENT
|
2021-05-05 15:58:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
################JUKEBOX ENDPOINTS##################
|
|
|
|
|
2021-05-10 20:29:26 +01:00
|
|
|
######GET ACCESS TOKEN######
|
2021-05-05 15:58:33 +01:00
|
|
|
|
2021-05-12 15:47:21 +01:00
|
|
|
|
2021-06-07 14:24:04 +01:00
|
|
|
@jukebox_ext.route(
|
|
|
|
"/api/v1/jukebox/jb/playlist/<juke_id>/<sp_playlist>", methods=["GET"]
|
|
|
|
)
|
2021-06-11 15:42:50 +01:00
|
|
|
async def api_get_jukebox_song(juke_id, sp_playlist, retry=False):
|
|
|
|
try:
|
|
|
|
jukebox = await get_jukebox(juke_id)
|
|
|
|
except:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "No Jukebox"}),
|
|
|
|
HTTPStatus.FORBIDDEN,
|
|
|
|
)
|
2021-05-05 23:03:22 +01:00
|
|
|
tracks = []
|
2021-05-05 19:20:53 +01:00
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
try:
|
|
|
|
r = await client.get(
|
2021-05-05 23:03:22 +01:00
|
|
|
"https://api.spotify.com/v1/playlists/" + sp_playlist + "/tracks",
|
2021-05-05 19:20:53 +01:00
|
|
|
timeout=40,
|
|
|
|
headers={"Authorization": "Bearer " + jukebox.sp_access_token},
|
|
|
|
)
|
2021-05-05 23:03:22 +01:00
|
|
|
if "items" not in r.json():
|
2021-06-07 23:29:40 +01:00
|
|
|
if r.status_code == 401:
|
2021-06-07 14:24:04 +01:00
|
|
|
token = await api_get_token(juke_id)
|
2021-05-05 23:03:22 +01:00
|
|
|
if token == False:
|
|
|
|
return False
|
2021-06-11 15:42:50 +01:00
|
|
|
elif retry:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "Failed to get auth"}),
|
|
|
|
HTTPStatus.FORBIDDEN,
|
|
|
|
)
|
2021-05-05 23:03:22 +01:00
|
|
|
else:
|
2021-06-11 15:42:50 +01:00
|
|
|
return await api_get_jukebox_song(
|
|
|
|
juke_id, sp_playlist, retry=True
|
|
|
|
)
|
2021-05-12 15:47:21 +01:00
|
|
|
return r, HTTPStatus.OK
|
2021-05-05 23:03:22 +01:00
|
|
|
for item in r.json()["items"]:
|
|
|
|
tracks.append(
|
|
|
|
{
|
2021-06-07 12:03:13 +01:00
|
|
|
"id": item["track"]["id"],
|
|
|
|
"name": item["track"]["name"],
|
|
|
|
"album": item["track"]["album"]["name"],
|
|
|
|
"artist": item["track"]["artists"][0]["name"],
|
|
|
|
"image": item["track"]["album"]["images"][0]["url"],
|
2021-05-05 23:03:22 +01:00
|
|
|
}
|
|
|
|
)
|
2021-05-05 19:20:53 +01:00
|
|
|
except AssertionError:
|
|
|
|
something = None
|
2021-05-12 15:47:21 +01:00
|
|
|
return jsonify([track for track in tracks])
|
|
|
|
|
2021-06-07 12:03:13 +01:00
|
|
|
|
2021-06-07 14:24:04 +01:00
|
|
|
async def api_get_token(juke_id):
|
2021-06-11 15:42:50 +01:00
|
|
|
try:
|
|
|
|
jukebox = await get_jukebox(juke_id)
|
|
|
|
except:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "No Jukebox"}),
|
|
|
|
HTTPStatus.FORBIDDEN,
|
|
|
|
)
|
2021-06-07 12:03:13 +01:00
|
|
|
|
2021-05-05 19:20:53 +01:00
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
try:
|
|
|
|
r = await client.post(
|
|
|
|
"https://accounts.spotify.com/api/token",
|
|
|
|
timeout=40,
|
|
|
|
params={
|
|
|
|
"grant_type": "refresh_token",
|
|
|
|
"refresh_token": jukebox.sp_refresh_token,
|
|
|
|
"client_id": jukebox.sp_user,
|
|
|
|
},
|
|
|
|
headers={
|
2021-05-05 23:03:22 +01:00
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
"Authorization": "Basic "
|
2021-05-05 19:20:53 +01:00
|
|
|
+ base64.b64encode(
|
2021-05-05 23:03:22 +01:00
|
|
|
str(jukebox.sp_user + ":" + jukebox.sp_secret).encode("ascii")
|
2021-05-05 19:20:53 +01:00
|
|
|
).decode("ascii"),
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
},
|
|
|
|
)
|
2021-05-05 23:03:22 +01:00
|
|
|
if "access_token" not in r.json():
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
await update_jukebox(
|
2021-06-07 14:24:04 +01:00
|
|
|
juke_id=juke_id, sp_access_token=r.json()["access_token"]
|
2021-05-05 23:03:22 +01:00
|
|
|
)
|
2021-05-05 19:20:53 +01:00
|
|
|
except AssertionError:
|
|
|
|
something = None
|
2021-05-05 23:03:22 +01:00
|
|
|
return True
|
2021-05-06 10:02:18 +01:00
|
|
|
|
|
|
|
|
2021-06-11 15:42:50 +01:00
|
|
|
######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,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-06-01 21:19:41 +01:00
|
|
|
######GET INVOICE STUFF
|
2021-05-06 10:02:18 +01:00
|
|
|
|
2021-06-07 12:03:13 +01:00
|
|
|
|
2021-06-07 14:24:04 +01:00
|
|
|
@jukebox_ext.route("/api/v1/jukebox/jb/invoice/<juke_id>/<song_id>", methods=["GET"])
|
|
|
|
async def api_get_jukebox_invoice(juke_id, song_id):
|
2021-06-11 15:42:50 +01:00
|
|
|
try:
|
|
|
|
jukebox = await get_jukebox(juke_id)
|
|
|
|
except:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "No Jukebox"}),
|
|
|
|
HTTPStatus.FORBIDDEN,
|
|
|
|
)
|
2021-06-13 12:58:46 +01:00
|
|
|
try:
|
|
|
|
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 not deviceConnected:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "No device connected"}),
|
|
|
|
HTTPStatus.NOT_FOUND,
|
|
|
|
)
|
|
|
|
except:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "No device connected"}),
|
|
|
|
HTTPStatus.NOT_FOUND,
|
|
|
|
)
|
2021-06-01 23:40:50 +01:00
|
|
|
|
2021-06-07 12:03:13 +01:00
|
|
|
invoice = await create_invoice(
|
|
|
|
wallet_id=jukebox.wallet,
|
|
|
|
amount=jukebox.price,
|
|
|
|
memo=jukebox.title,
|
|
|
|
extra={"tag": "jukebox"},
|
|
|
|
)
|
|
|
|
|
2021-06-07 15:42:02 +01:00
|
|
|
jukebox_payment = await create_jukebox_payment(song_id, invoice[0], juke_id)
|
2021-06-07 12:03:13 +01:00
|
|
|
|
2021-06-01 16:50:06 +01:00
|
|
|
return jsonify(invoice, jukebox_payment)
|
2021-05-12 15:47:21 +01:00
|
|
|
|
|
|
|
|
2021-06-07 23:29:40 +01:00
|
|
|
@jukebox_ext.route(
|
|
|
|
"/api/v1/jukebox/jb/checkinvoice/<pay_hash>/<juke_id>", methods=["GET"]
|
|
|
|
)
|
|
|
|
async def api_get_jukebox_invoice_check(pay_hash, juke_id):
|
2021-06-11 15:42:50 +01:00
|
|
|
try:
|
|
|
|
jukebox = await get_jukebox(juke_id)
|
|
|
|
except:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "No Jukebox"}),
|
|
|
|
HTTPStatus.FORBIDDEN,
|
|
|
|
)
|
2021-06-07 23:29:40 +01:00
|
|
|
try:
|
|
|
|
status = await check_invoice_status(jukebox.wallet, pay_hash)
|
|
|
|
is_paid = not status.pending
|
|
|
|
except Exception as exc:
|
|
|
|
return jsonify({"paid": False}), HTTPStatus.OK
|
|
|
|
if is_paid:
|
|
|
|
wallet = await get_wallet(jukebox.wallet)
|
|
|
|
payment = await wallet.get_payment(pay_hash)
|
|
|
|
await payment.set_pending(False)
|
|
|
|
await update_jukebox_payment(pay_hash, paid=True)
|
|
|
|
return jsonify({"paid": True}), HTTPStatus.OK
|
|
|
|
return jsonify({"paid": False}), HTTPStatus.OK
|
2021-06-07 14:24:04 +01:00
|
|
|
|
|
|
|
|
2021-06-07 23:29:40 +01:00
|
|
|
@jukebox_ext.route(
|
|
|
|
"/api/v1/jukebox/jb/invoicep/<song_id>/<juke_id>/<pay_hash>", methods=["GET"]
|
|
|
|
)
|
2021-06-11 15:42:50 +01:00
|
|
|
async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash, retry=False):
|
|
|
|
try:
|
|
|
|
jukebox = await get_jukebox(juke_id)
|
|
|
|
except:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "No Jukebox"}),
|
|
|
|
HTTPStatus.FORBIDDEN,
|
|
|
|
)
|
2021-06-07 23:29:40 +01:00
|
|
|
jukebox_payment = await get_jukebox_payment(pay_hash)
|
|
|
|
if jukebox_payment.paid:
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
r = await client.get(
|
|
|
|
"https://api.spotify.com/v1/me/player/currently-playing?market=ES",
|
|
|
|
timeout=40,
|
|
|
|
headers={"Authorization": "Bearer " + jukebox.sp_access_token},
|
|
|
|
)
|
2021-06-11 15:42:50 +01:00
|
|
|
rDevice = await client.get(
|
2021-06-11 20:27:13 +01:00
|
|
|
"https://api.spotify.com/v1/me/player",
|
2021-06-11 15:42:50 +01:00
|
|
|
timeout=40,
|
|
|
|
headers={"Authorization": "Bearer " + jukebox.sp_access_token},
|
|
|
|
)
|
2021-06-11 20:27:13 +01:00
|
|
|
isPlaying = False
|
|
|
|
if rDevice.status_code == 200:
|
|
|
|
isPlaying = rDevice.json()["is_playing"]
|
2021-06-11 15:42:50 +01:00
|
|
|
|
2021-06-11 20:27:13 +01:00
|
|
|
if r.status_code == 204 or isPlaying == False:
|
2021-06-07 23:29:40 +01:00
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
uri = ["spotify:track:" + song_id]
|
|
|
|
r = await client.put(
|
|
|
|
"https://api.spotify.com/v1/me/player/play?device_id="
|
|
|
|
+ jukebox.sp_device.split("-")[1],
|
|
|
|
json={"uris": uri},
|
|
|
|
timeout=40,
|
|
|
|
headers={"Authorization": "Bearer " + jukebox.sp_access_token},
|
|
|
|
)
|
|
|
|
if r.status_code == 204:
|
|
|
|
return jsonify(jukebox_payment), HTTPStatus.OK
|
|
|
|
elif r.status_code == 401 or r.status_code == 403:
|
|
|
|
token = await api_get_token(juke_id)
|
|
|
|
if token == False:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "Invoice not paid"}),
|
|
|
|
HTTPStatus.FORBIDDEN,
|
|
|
|
)
|
2021-06-11 15:42:50 +01:00
|
|
|
elif retry:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "Failed to get auth"}),
|
|
|
|
HTTPStatus.FORBIDDEN,
|
|
|
|
)
|
2021-06-07 23:29:40 +01:00
|
|
|
else:
|
|
|
|
return api_get_jukebox_invoice_paid(
|
2021-06-11 15:42:50 +01:00
|
|
|
song_id, juke_id, pay_hash, retry=True
|
2021-06-07 23:29:40 +01:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "Invoice not paid"}),
|
|
|
|
HTTPStatus.FORBIDDEN,
|
|
|
|
)
|
2021-06-11 20:27:13 +01:00
|
|
|
elif r.status_code == 200:
|
2021-06-07 14:24:04 +01:00
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
r = await client.post(
|
|
|
|
"https://api.spotify.com/v1/me/player/queue?uri=spotify%3Atrack%3A"
|
2021-06-07 23:29:40 +01:00
|
|
|
+ song_id
|
2021-06-07 14:24:04 +01:00
|
|
|
+ "&device_id="
|
|
|
|
+ jukebox.sp_device.split("-")[1],
|
|
|
|
timeout=40,
|
|
|
|
headers={"Authorization": "Bearer " + jukebox.sp_access_token},
|
|
|
|
)
|
2021-06-07 23:29:40 +01:00
|
|
|
if r.status_code == 204:
|
|
|
|
return jsonify(jukebox_payment), HTTPStatus.OK
|
|
|
|
|
|
|
|
elif r.status_code == 401 or r.status_code == 403:
|
|
|
|
token = await api_get_token(juke_id)
|
|
|
|
if token == False:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "Invoice not paid"}),
|
|
|
|
HTTPStatus.OK,
|
|
|
|
)
|
2021-06-11 15:42:50 +01:00
|
|
|
elif retry:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "Failed to get auth"}),
|
|
|
|
HTTPStatus.FORBIDDEN,
|
|
|
|
)
|
2021-06-07 23:29:40 +01:00
|
|
|
else:
|
|
|
|
return await api_get_jukebox_invoice_paid(
|
|
|
|
song_id, juke_id, pay_hash
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "Invoice not paid"}),
|
|
|
|
HTTPStatus.OK,
|
|
|
|
)
|
|
|
|
elif r.status_code == 401 or r.status_code == 403:
|
|
|
|
token = await api_get_token(juke_id)
|
|
|
|
if token == False:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "Invoice not paid"}),
|
|
|
|
HTTPStatus.OK,
|
|
|
|
)
|
2021-06-11 15:42:50 +01:00
|
|
|
elif retry:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "Failed to get auth"}),
|
|
|
|
HTTPStatus.FORBIDDEN,
|
|
|
|
)
|
2021-06-07 23:29:40 +01:00
|
|
|
else:
|
|
|
|
return await api_get_jukebox_invoice_paid(
|
|
|
|
song_id, juke_id, pay_hash
|
|
|
|
)
|
2021-06-11 20:27:13 +01:00
|
|
|
return jsonify({"error": "Invoice not paid"}), HTTPStatus.OK
|
2021-06-07 12:03:13 +01:00
|
|
|
|
|
|
|
|
2021-06-01 21:19:41 +01:00
|
|
|
############################GET TRACKS
|
|
|
|
|
2021-06-07 12:03:13 +01:00
|
|
|
|
2021-06-07 14:24:04 +01:00
|
|
|
@jukebox_ext.route("/api/v1/jukebox/jb/currently/<juke_id>", methods=["GET"])
|
2021-06-11 15:42:50 +01:00
|
|
|
async def api_get_jukebox_currently(juke_id, retry=False):
|
|
|
|
try:
|
|
|
|
jukebox = await get_jukebox(juke_id)
|
|
|
|
except:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "No Jukebox"}),
|
|
|
|
HTTPStatus.FORBIDDEN,
|
|
|
|
)
|
2021-06-01 21:19:41 +01:00
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
try:
|
|
|
|
r = await client.get(
|
|
|
|
"https://api.spotify.com/v1/me/player/currently-playing?market=ES",
|
|
|
|
timeout=40,
|
|
|
|
headers={"Authorization": "Bearer " + jukebox.sp_access_token},
|
|
|
|
)
|
2021-06-07 23:29:40 +01:00
|
|
|
if r.status_code == 204:
|
|
|
|
return jsonify({"error": "Nothing"}), HTTPStatus.OK
|
|
|
|
elif r.status_code == 200:
|
2021-06-11 15:42:50 +01:00
|
|
|
try:
|
|
|
|
response = r.json()
|
2021-06-11 20:27:13 +01:00
|
|
|
|
2021-06-11 15:42:50 +01:00
|
|
|
track = {
|
|
|
|
"id": response["item"]["id"],
|
|
|
|
"name": response["item"]["name"],
|
|
|
|
"album": response["item"]["album"]["name"],
|
|
|
|
"artist": response["item"]["artists"][0]["name"],
|
|
|
|
"image": response["item"]["album"]["images"][0]["url"],
|
|
|
|
}
|
2021-06-11 20:27:13 +01:00
|
|
|
return jsonify(track), HTTPStatus.OK
|
2021-06-11 15:42:50 +01:00
|
|
|
except:
|
|
|
|
return jsonify("Something went wrong"), HTTPStatus.NOT_FOUND
|
|
|
|
|
2021-06-07 23:29:40 +01:00
|
|
|
elif r.status_code == 401:
|
|
|
|
token = await api_get_token(juke_id)
|
|
|
|
if token == False:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "Invoice not paid"}),
|
|
|
|
HTTPStatus.FORBIDDEN,
|
|
|
|
)
|
2021-06-11 15:42:50 +01:00
|
|
|
elif retry:
|
|
|
|
return (
|
|
|
|
jsonify({"error": "Failed to get auth"}),
|
|
|
|
HTTPStatus.FORBIDDEN,
|
|
|
|
)
|
2021-06-07 23:29:40 +01:00
|
|
|
else:
|
2021-06-11 15:42:50 +01:00
|
|
|
return await api_get_jukebox_currently(juke_id, retry=True)
|
2021-06-07 23:29:40 +01:00
|
|
|
else:
|
|
|
|
return jsonify("Something went wrong"), HTTPStatus.NOT_FOUND
|
2021-06-01 21:19:41 +01:00
|
|
|
except AssertionError:
|
2021-06-07 23:29:40 +01:00
|
|
|
return jsonify("Something went wrong"), HTTPStatus.NOT_FOUND
|