Working on jukebox

This commit is contained in:
Ben Arc
2021-08-20 20:12:03 +01:00
parent 76cb93b276
commit c824155ea4
5 changed files with 62 additions and 81 deletions

View File

@@ -12,14 +12,14 @@ from .crud import get_jukebox
from .views_api import api_get_jukebox_device_check from .views_api import api_get_jukebox_device_check
@jukebox_ext.route("/") @jukebox_ext.get("/")
@validate_uuids(["usr"], required=True) @validate_uuids(["usr"], required=True)
@check_user_exists() @check_user_exists()
async def index(): async def index():
return await render_template("jukebox/index.html", user=g.user) return await render_template("jukebox/index.html", user=g.user)
@jukebox_ext.route("/<juke_id>") @jukebox_ext.get("/<juke_id>")
async def connect_to_jukebox(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:

View File

@@ -46,7 +46,7 @@ async def api_check_credentials_callbac(juke_id):
jukebox = await get_jukebox(juke_id) jukebox = await get_jukebox(juke_id)
except: except:
return ( return (
jsonable_encoder({"error": "No Jukebox"}), {"error": "No Jukebox"},
HTTPStatus.FORBIDDEN, HTTPStatus.FORBIDDEN,
) )
if request.args.get("code"): if request.args.get("code"):
@@ -207,13 +207,13 @@ async def api_get_token(juke_id):
######CHECK DEVICE ######CHECK DEVICE
@jukebox_ext.route("/api/v1/jukebox/jb/<juke_id>", methods=["GET"]) @jukebox_ext.get("/api/v1/jukebox/jb/<juke_id>")
async def api_get_jukebox_device_check(juke_id, retry=False): async def api_get_jukebox_device_check(juke_id, retry=False):
try: try:
jukebox = await get_jukebox(juke_id) jukebox = await get_jukebox(juke_id)
except: except:
return ( return (
jsonable_encoder({"error": "No Jukebox"}), {"error": "No Jukebox"},
HTTPStatus.FORBIDDEN, HTTPStatus.FORBIDDEN,
) )
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
@@ -252,13 +252,13 @@ async def api_get_jukebox_device_check(juke_id, retry=False):
######GET INVOICE STUFF ######GET INVOICE STUFF
@jukebox_ext.route("/api/v1/jukebox/jb/invoice/<juke_id>/<song_id>", methods=["GET"]) @jukebox_ext.get("/api/v1/jukebox/jb/invoice/<juke_id>/<song_id>")
async def api_get_jukebox_invoice(juke_id, song_id): async def api_get_jukebox_invoice(juke_id, song_id):
try: try:
jukebox = await get_jukebox(juke_id) jukebox = await get_jukebox(juke_id)
except: except:
return ( return (
jsonable_encoder({"error": "No Jukebox"}), {"error": "No Jukebox"},
HTTPStatus.FORBIDDEN, HTTPStatus.FORBIDDEN,
) )
try: try:
@@ -270,12 +270,12 @@ async def api_get_jukebox_invoice(juke_id, song_id):
deviceConnected = True deviceConnected = True
if not deviceConnected: if not deviceConnected:
return ( return (
jsonable_encoder({"error": "No device connected"}), {"error": "No device connected"},
HTTPStatus.NOT_FOUND, HTTPStatus.NOT_FOUND,
) )
except: except:
return ( return (
jsonable_encoder({"error": "No device connected"}), {"error": "No device connected"},
HTTPStatus.NOT_FOUND, HTTPStatus.NOT_FOUND,
) )
@@ -299,25 +299,25 @@ async def api_get_jukebox_invoice_check(pay_hash, juke_id):
jukebox = await get_jukebox(juke_id) jukebox = await get_jukebox(juke_id)
except: except:
return ( return (
jsonable_encoder({"error": "No Jukebox"}), {"error": "No Jukebox"},
HTTPStatus.FORBIDDEN, 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
except Exception as exc: except Exception as exc:
return jsonable_encoder({"paid": False}), HTTPStatus.OK return {"paid": False}, HTTPStatus.OK
if is_paid: if is_paid:
wallet = await get_wallet(jukebox.wallet) wallet = await get_wallet(jukebox.wallet)
payment = await wallet.get_payment(pay_hash) payment = await wallet.get_payment(pay_hash)
await payment.set_pending(False) await payment.set_pending(False)
await update_jukebox_payment(pay_hash, paid=True) await update_jukebox_payment(pay_hash, paid=True)
return jsonable_encoder({"paid": True}), HTTPStatus.OK return {"paid": True}, HTTPStatus.OK
return jsonable_encoder({"paid": False}), HTTPStatus.OK return {"paid": False}, HTTPStatus.OK
@jukebox_ext.route( @jukebox_ext.get(
"/api/v1/jukebox/jb/invoicep/<song_id>/<juke_id>/<pay_hash>", methods=["GET"] "/api/v1/jukebox/jb/invoicep/<song_id>/<juke_id>/<pay_hash>"
) )
async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash, retry=False): async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash, retry=False):
try: try:
@@ -356,17 +356,17 @@ 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},
) )
if r.status_code == 204: if r.status_code == 204:
return jsonify(jukebox_payment), HTTPStatus.OK return jukebox_payment, HTTPStatus.OK
elif r.status_code == 401 or r.status_code == 403: elif r.status_code == 401 or r.status_code == 403:
token = await api_get_token(juke_id) token = await api_get_token(juke_id)
if token == False: if token == False:
return ( return (
jsonify({"error": "Invoice not paid"}), {"error": "Invoice not paid"},
HTTPStatus.FORBIDDEN, HTTPStatus.FORBIDDEN,
) )
elif retry: elif retry:
return ( return (
jsonify({"error": "Failed to get auth"}), {"error": "Failed to get auth"},
HTTPStatus.FORBIDDEN, HTTPStatus.FORBIDDEN,
) )
else: else:
@@ -375,7 +375,7 @@ async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash, retry=False):
) )
else: else:
return ( return (
jsonify({"error": "Invoice not paid"}), {"error": "Invoice not paid"},
HTTPStatus.FORBIDDEN, HTTPStatus.FORBIDDEN,
) )
elif r.status_code == 200: elif r.status_code == 200:
@@ -389,18 +389,18 @@ 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},
) )
if r.status_code == 204: if r.status_code == 204:
return jsonify(jukebox_payment), HTTPStatus.OK return jukebox_payment, HTTPStatus.OK
elif r.status_code == 401 or r.status_code == 403: elif r.status_code == 401 or r.status_code == 403:
token = await api_get_token(juke_id) token = await api_get_token(juke_id)
if token == False: if token == False:
return ( return (
jsonify({"error": "Invoice not paid"}), {"error": "Invoice not paid"},
HTTPStatus.OK, HTTPStatus.OK,
) )
elif retry: elif retry:
return ( return (
jsonify({"error": "Failed to get auth"}), {"error": "Failed to get auth"},
HTTPStatus.FORBIDDEN, HTTPStatus.FORBIDDEN,
) )
else: else:
@@ -409,38 +409,38 @@ async def api_get_jukebox_invoice_paid(song_id, juke_id, pay_hash, retry=False):
) )
else: else:
return ( return (
jsonify({"error": "Invoice not paid"}), {"error": "Invoice not paid"},
HTTPStatus.OK, HTTPStatus.OK,
) )
elif r.status_code == 401 or r.status_code == 403: elif r.status_code == 401 or r.status_code == 403:
token = await api_get_token(juke_id) token = await api_get_token(juke_id)
if token == False: if token == False:
return ( return (
jsonify({"error": "Invoice not paid"}), {"error": "Invoice not paid"},
HTTPStatus.OK, HTTPStatus.OK,
) )
elif retry: elif retry:
return ( return (
jsonify({"error": "Failed to get auth"}), {"error": "Failed to get auth"},
HTTPStatus.FORBIDDEN, 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
) )
return jsonify({"error": "Invoice not paid"}), HTTPStatus.OK return {"error": "Invoice not paid"}, HTTPStatus.OK
############################GET TRACKS ############################GET TRACKS
@jukebox_ext.route("/api/v1/jukebox/jb/currently/<juke_id>", methods=["GET"]) @jukebox_ext.get("/api/v1/jukebox/jb/currently/<juke_id>")
async def api_get_jukebox_currently(juke_id, retry=False): async def api_get_jukebox_currently(juke_id, retry=False):
try: try:
jukebox = await get_jukebox(juke_id) jukebox = await get_jukebox(juke_id)
except: except:
return ( return (
jsonify({"error": "No Jukebox"}), {"error": "No Jukebox"},
HTTPStatus.FORBIDDEN, HTTPStatus.FORBIDDEN,
) )
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
@@ -451,7 +451,7 @@ async def api_get_jukebox_currently(juke_id, retry=False):
headers={"Authorization": "Bearer " + jukebox.sp_access_token}, headers={"Authorization": "Bearer " + jukebox.sp_access_token},
) )
if r.status_code == 204: if r.status_code == 204:
return jsonify({"error": "Nothing"}), HTTPStatus.OK return {"error": "Nothing"}, HTTPStatus.OK
elif r.status_code == 200: elif r.status_code == 200:
try: try:
response = r.json() response = r.json()
@@ -463,25 +463,25 @@ 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 jsonify(track), HTTPStatus.OK return track, HTTPStatus.OK
except: except:
return jsonify("Something went wrong"), HTTPStatus.NOT_FOUND return "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:
return ( return (
jsonify({"error": "Invoice not paid"}), {"error": "Invoice not paid"},
HTTPStatus.FORBIDDEN, HTTPStatus.FORBIDDEN,
) )
elif retry: elif retry:
return ( return (
jsonify({"error": "Failed to get auth"}), {"error": "Failed to get auth"},
HTTPStatus.FORBIDDEN, HTTPStatus.FORBIDDEN,
) )
else: else:
return await api_get_jukebox_currently(juke_id, retry=True) return await api_get_jukebox_currently(juke_id, retry=True)
else: else:
return jsonify("Something went wrong"), HTTPStatus.NOT_FOUND return "Something went wrong", HTTPStatus.NOT_FOUND
except AssertionError: except AssertionError:
return jsonify("Something went wrong"), HTTPStatus.NOT_FOUND return "Something went wrong", HTTPStatus.NOT_FOUND

View File

@@ -6,17 +6,9 @@ from lnbits.decorators import check_user_exists, validate_uuids
from . import watchonly_ext from . import watchonly_ext
@watchonly_ext.route("/") @watchonly_ext.get("/")
@validate_uuids(["usr"], required=True) @validate_uuids(["usr"], required=True)
@check_user_exists() @check_user_exists()
async def index(): async def index():
return await render_template("watchonly/index.html", user=g.user) return await render_template("watchonly/index.html", user=g.user)
@watchonly_ext.route("/<charge_id>")
async def display(charge_id):
link = get_payment(charge_id) or abort(
HTTPStatus.NOT_FOUND, "Charge link does not exist."
)
return await render_template("watchonly/display.html", link=link)

View File

@@ -37,71 +37,65 @@ async def api_wallets_retrieve():
return "" return ""
@watchonly_ext.route("/api/v1/wallet/<wallet_id>", methods=["GET"]) @watchonly_ext.get("/api/v1/wallet/<wallet_id>")
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
async def api_wallet_retrieve(wallet_id): async def api_wallet_retrieve(wallet_id):
wallet = await get_watch_wallet(wallet_id) wallet = await get_watch_wallet(wallet_id)
if not wallet: if not wallet:
return jsonify({"message": "wallet does not exist"}), HTTPStatus.NOT_FOUND return {"message": "wallet does not exist"}, HTTPStatus.NOT_FOUND
return jsonify(wallet._asdict()), HTTPStatus.OK return wallet._asdict(), HTTPStatus.OK
@watchonly_ext.route("/api/v1/wallet", methods=["POST"]) @watchonly_ext.post("/api/v1/wallet")
@api_check_wallet_key("admin") @api_check_wallet_key("admin")
@api_validate_post_request( async def api_wallet_create_or_update(masterPub: str, Title: str, wallet_id=None):
schema={
"masterpub": {"type": "string", "empty": False, "required": True},
"title": {"type": "string", "empty": False, "required": True},
}
)
async def api_wallet_create_or_update(wallet_id=None):
try: try:
wallet = await create_watch_wallet( wallet = await create_watch_wallet(
user=g.wallet.user, masterpub=g.data["masterpub"], title=g.data["title"] user=g.wallet.user, masterpub=masterPub, title=Title
) )
except Exception as e: except Exception as e:
return jsonify({"message": str(e)}), HTTPStatus.BAD_REQUEST return {"message": str(e)}, HTTPStatus.BAD_REQUEST
mempool = await get_mempool(g.wallet.user) mempool = await get_mempool(g.wallet.user)
if not mempool: if not mempool:
create_mempool(user=g.wallet.user) create_mempool(user=g.wallet.user)
return jsonify(wallet._asdict()), HTTPStatus.CREATED return wallet._asdict(), HTTPStatus.CREATED
@watchonly_ext.route("/api/v1/wallet/<wallet_id>", methods=["DELETE"]) @watchonly_ext.delete("/api/v1/wallet/<wallet_id>")
@api_check_wallet_key("admin") @api_check_wallet_key("admin")
async def api_wallet_delete(wallet_id): async def api_wallet_delete(wallet_id):
wallet = await get_watch_wallet(wallet_id) wallet = await get_watch_wallet(wallet_id)
if not wallet: if not wallet:
return jsonify({"message": "Wallet link does not exist."}), HTTPStatus.NOT_FOUND return {"message": "Wallet link does not exist."}, HTTPStatus.NOT_FOUND
await delete_watch_wallet(wallet_id) await delete_watch_wallet(wallet_id)
return jsonify({"deleted": "true"}), HTTPStatus.NO_CONTENT return {"deleted": "true"}, HTTPStatus.NO_CONTENT
#############################ADDRESSES########################## #############################ADDRESSES##########################
@watchonly_ext.route("/api/v1/address/<wallet_id>", methods=["GET"]) @watchonly_ext.get("/api/v1/address/<wallet_id>")
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
async def api_fresh_address(wallet_id): async def api_fresh_address(wallet_id):
await get_fresh_address(wallet_id) await get_fresh_address(wallet_id)
addresses = await get_addresses(wallet_id) addresses = await get_addresses(wallet_id)
return jsonify([address._asdict() for address in addresses]), HTTPStatus.OK return [address._asdict() for address in addresses], HTTPStatus.OK
@watchonly_ext.route("/api/v1/addresses/<wallet_id>", methods=["GET"]) @watchonly_ext.get("/api/v1/addresses/<wallet_id>")
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
async def api_get_addresses(wallet_id): async def api_get_addresses(wallet_id):
wallet = await get_watch_wallet(wallet_id) wallet = await get_watch_wallet(wallet_id)
if not wallet: if not wallet:
return jsonify({"message": "wallet does not exist"}), HTTPStatus.NOT_FOUND return {"message": "wallet does not exist"}, HTTPStatus.NOT_FOUND
addresses = await get_addresses(wallet_id) addresses = await get_addresses(wallet_id)
@@ -109,28 +103,23 @@ async def api_get_addresses(wallet_id):
await get_fresh_address(wallet_id) await get_fresh_address(wallet_id)
addresses = await get_addresses(wallet_id) addresses = await get_addresses(wallet_id)
return jsonify([address._asdict() for address in addresses]), HTTPStatus.OK return [address._asdict() for address in addresses], HTTPStatus.OK
#############################MEMPOOL########################## #############################MEMPOOL##########################
@watchonly_ext.route("/api/v1/mempool", methods=["PUT"]) @watchonly_ext.put("/api/v1/mempool")
@api_check_wallet_key("admin") @api_check_wallet_key("admin")
@api_validate_post_request( async def api_update_mempool(endpoint: str):
schema={ mempool = await update_mempool(user=g.wallet.user, **endpoint)
"endpoint": {"type": "string", "empty": False, "required": True}, return mempool._asdict(), HTTPStatus.OK
}
)
async def api_update_mempool():
mempool = await update_mempool(user=g.wallet.user, **g.data)
return jsonify(mempool._asdict()), HTTPStatus.OK
@watchonly_ext.route("/api/v1/mempool", methods=["GET"]) @watchonly_ext.get("/api/v1/mempool")
@api_check_wallet_key("admin") @api_check_wallet_key("admin")
async def api_get_mempool(): async def api_get_mempool():
mempool = await get_mempool(g.wallet.user) mempool = await get_mempool(g.wallet.user)
if not mempool: if not mempool:
mempool = await create_mempool(user=g.wallet.user) mempool = await create_mempool(user=g.wallet.user)
return jsonify(mempool._asdict()), HTTPStatus.OK return mempool._asdict(), HTTPStatus.OK

View File

@@ -8,14 +8,14 @@ from . import withdraw_ext
from .crud import get_withdraw_link, chunks from .crud import get_withdraw_link, chunks
@withdraw_ext.route("/") @withdraw_ext.get("/")
@validate_uuids(["usr"], required=True) @validate_uuids(["usr"], required=True)
@check_user_exists() @check_user_exists()
async def index(): async def index():
return await render_template("withdraw/index.html", user=g.user) return await render_template("withdraw/index.html", user=g.user)
@withdraw_ext.route("/<link_id>") @withdraw_ext.get("/<link_id>")
async def display(link_id): async def display(link_id):
link = await get_withdraw_link(link_id, 0) or abort( link = await get_withdraw_link(link_id, 0) or abort(
HTTPStatus.NOT_FOUND, "Withdraw link does not exist." HTTPStatus.NOT_FOUND, "Withdraw link does not exist."
@@ -23,7 +23,7 @@ async def display(link_id):
return await render_template("withdraw/display.html", link=link, unique=True) return await render_template("withdraw/display.html", link=link, unique=True)
@withdraw_ext.route("/img/<link_id>") @withdraw_ext.get("/img/<link_id>")
async def img(link_id): async def img(link_id):
link = await get_withdraw_link(link_id, 0) or abort( link = await get_withdraw_link(link_id, 0) or abort(
HTTPStatus.NOT_FOUND, "Withdraw link does not exist." HTTPStatus.NOT_FOUND, "Withdraw link does not exist."
@@ -43,7 +43,7 @@ async def img(link_id):
) )
@withdraw_ext.route("/print/<link_id>") @withdraw_ext.get("/print/<link_id>")
async def print_qr(link_id): async def print_qr(link_id):
link = await get_withdraw_link(link_id) or abort( link = await get_withdraw_link(link_id) or abort(
HTTPStatus.NOT_FOUND, "Withdraw link does not exist." HTTPStatus.NOT_FOUND, "Withdraw link does not exist."