lnbits-legend/lnbits/extensions/livestream/views.py

49 lines
1.6 KiB
Python
Raw Normal View History

2021-10-28 17:02:07 +01:00
from http import HTTPStatus
2022-06-01 14:53:05 +02:00
2023-01-05 09:28:38 +01:00
from fastapi import Depends, HTTPException, Query, Request
from starlette.datastructures import URL
2021-10-28 17:02:07 +01:00
from starlette.responses import HTMLResponse, RedirectResponse
from lnbits.core.crud import get_wallet_payment
2023-01-05 09:28:38 +01:00
from lnbits.core.models import User
2021-10-28 17:02:07 +01:00
from lnbits.decorators import check_user_exists
from . import livestream_ext, livestream_renderer
from .crud import get_livestream_by_track, get_track
@livestream_ext.get("/", response_class=HTMLResponse)
async def index(request: Request, user: User = Depends(check_user_exists)):
2021-11-12 04:14:55 +00:00
return livestream_renderer().TemplateResponse(
"livestream/index.html", {"request": request, "user": user.dict()}
)
2021-10-28 17:02:07 +01:00
@livestream_ext.get("/track/{track_id}", name="livestream.track_redirect_download")
2021-11-10 11:15:33 +00:00
async def track_redirect_download(track_id, p: str = Query(...)):
payment_hash = p
2021-10-28 17:02:07 +01:00
track = await get_track(track_id)
ls = await get_livestream_by_track(track_id)
2023-01-05 09:28:38 +01:00
assert ls
payment = await get_wallet_payment(ls.wallet, payment_hash)
2021-10-28 17:02:07 +01:00
if not payment:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
2023-01-05 09:28:38 +01:00
detail=f"Couldn't find the payment {payment_hash}.",
)
if not track:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail=f"Couldn't find the track {track_id}.",
2021-10-28 17:02:07 +01:00
)
if payment.pending:
raise HTTPException(
status_code=HTTPStatus.PAYMENT_REQUIRED,
2021-11-12 04:14:55 +00:00
detail=f"Payment {payment_hash} wasn't received yet. Please try again in a minute.",
2021-10-28 17:02:07 +01:00
)
2023-01-05 09:28:38 +01:00
assert track.download_url
return RedirectResponse(url=URL(track.download_url))