black
This commit is contained in:
@@ -17,14 +17,13 @@ livestream_static_files = [
|
||||
}
|
||||
]
|
||||
|
||||
livestream_ext: APIRouter = APIRouter(
|
||||
prefix="/livestream",
|
||||
tags=["livestream"]
|
||||
)
|
||||
livestream_ext: APIRouter = APIRouter(prefix="/livestream", tags=["livestream"])
|
||||
|
||||
|
||||
def livestream_renderer():
|
||||
return template_renderer(["lnbits/extensions/livestream/templates"])
|
||||
|
||||
|
||||
from .lnurl import * # noqa
|
||||
from .tasks import wait_for_paid_invoices
|
||||
from .views import * # noqa
|
||||
|
||||
@@ -67,8 +67,7 @@ async def update_current_track(ls_id: int, track_id: Optional[int]):
|
||||
|
||||
async def update_livestream_fee(ls_id: int, fee_pct: int):
|
||||
await db.execute(
|
||||
"UPDATE livestream.livestreams SET fee_pct = ? WHERE id = ?",
|
||||
(fee_pct, ls_id),
|
||||
"UPDATE livestream.livestreams SET fee_pct = ? WHERE id = ?", (fee_pct, ls_id)
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -19,21 +19,17 @@ async def lnurl_livestream(ls_id, request: Request):
|
||||
ls = await get_livestream(ls_id)
|
||||
if not ls:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Livestream not found."
|
||||
status_code=HTTPStatus.NOT_FOUND, detail="Livestream not found."
|
||||
)
|
||||
|
||||
track = await get_track(ls.current_track)
|
||||
if not track:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="This livestream is offline."
|
||||
status_code=HTTPStatus.NOT_FOUND, detail="This livestream is offline."
|
||||
)
|
||||
|
||||
resp = LnurlPayResponse(
|
||||
callback=request.url_for(
|
||||
"livestream.lnurl_callback", track_id=track.id
|
||||
),
|
||||
callback=request.url_for("livestream.lnurl_callback", track_id=track.id),
|
||||
min_sendable=track.min_sendable,
|
||||
max_sendable=track.max_sendable,
|
||||
metadata=await track.lnurlpay_metadata(),
|
||||
@@ -49,15 +45,10 @@ async def lnurl_livestream(ls_id, request: Request):
|
||||
async def lnurl_track(track_id, request: Request):
|
||||
track = await get_track(track_id)
|
||||
if not track:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Track not found."
|
||||
)
|
||||
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="Track not found.")
|
||||
|
||||
resp = LnurlPayResponse(
|
||||
callback=request.url_for(
|
||||
"livestream.lnurl_callback", track_id=track.id
|
||||
),
|
||||
callback=request.url_for("livestream.lnurl_callback", track_id=track.id),
|
||||
min_sendable=track.min_sendable,
|
||||
max_sendable=track.max_sendable,
|
||||
metadata=await track.lnurlpay_metadata(),
|
||||
@@ -70,29 +61,28 @@ async def lnurl_track(track_id, request: Request):
|
||||
|
||||
|
||||
@livestream_ext.get("/lnurl/cb/{track_id}", name="livestream.lnurl_callback")
|
||||
async def lnurl_callback(track_id, request: Request, amount: int = Query(...), comment: str = Query("")):
|
||||
async def lnurl_callback(
|
||||
track_id, request: Request, amount: int = Query(...), comment: str = Query("")
|
||||
):
|
||||
track = await get_track(track_id)
|
||||
if not track:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Track not found."
|
||||
)
|
||||
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="Track not found.")
|
||||
|
||||
amount_received = int(amount or 0)
|
||||
|
||||
if amount_received < track.min_sendable:
|
||||
return LnurlErrorResponse(
|
||||
reason=f"Amount {round(amount_received / 1000)} is smaller than minimum {math.floor(track.min_sendable)}."
|
||||
).dict()
|
||||
reason=f"Amount {round(amount_received / 1000)} is smaller than minimum {math.floor(track.min_sendable)}."
|
||||
).dict()
|
||||
elif track.max_sendable < amount_received:
|
||||
return LnurlErrorResponse(
|
||||
reason=f"Amount {round(amount_received / 1000)} is greater than maximum {math.floor(track.max_sendable)}."
|
||||
).dict()
|
||||
reason=f"Amount {round(amount_received / 1000)} is greater than maximum {math.floor(track.max_sendable)}."
|
||||
).dict()
|
||||
|
||||
if len(comment or "") > 300:
|
||||
return LnurlErrorResponse(
|
||||
reason=f"Got a comment with {len(comment)} characters, but can only accept 300"
|
||||
).dict()
|
||||
reason=f"Got a comment with {len(comment)} characters, but can only accept 300"
|
||||
).dict()
|
||||
|
||||
ls = await get_livestream_by_track(track_id)
|
||||
|
||||
@@ -112,9 +102,7 @@ async def lnurl_callback(track_id, request: Request, amount: int = Query(...), c
|
||||
success_action = track.success_action(payment_hash, request=request)
|
||||
|
||||
resp = LnurlPayActionResponse(
|
||||
pr=payment_request,
|
||||
success_action=success_action,
|
||||
routes=[],
|
||||
pr=payment_request, success_action=success_action, routes=[]
|
||||
)
|
||||
|
||||
return resp.dict()
|
||||
|
||||
@@ -17,6 +17,7 @@ class CreateTrack(BaseModel):
|
||||
producer_id: str = Query(None)
|
||||
producer_name: str = Query(None)
|
||||
|
||||
|
||||
class Livestream(BaseModel):
|
||||
id: int
|
||||
wallet: str
|
||||
@@ -68,15 +69,15 @@ class Track(BaseModel):
|
||||
|
||||
return LnurlPayMetadata(json.dumps([["text/plain", description]]))
|
||||
|
||||
def success_action(self, payment_hash: str, request: Request) -> Optional[LnurlPaySuccessAction]:
|
||||
def success_action(
|
||||
self, payment_hash: str, request: Request
|
||||
) -> Optional[LnurlPaySuccessAction]:
|
||||
if not self.download_url:
|
||||
return None
|
||||
|
||||
return UrlAction(
|
||||
url=request.url_for(
|
||||
"livestream.track_redirect_download",
|
||||
track_id=self.id,
|
||||
p=payment_hash
|
||||
"livestream.track_redirect_download", track_id=self.id, p=payment_hash
|
||||
),
|
||||
description=f"Download the track {self.name}!",
|
||||
)
|
||||
|
||||
@@ -18,6 +18,7 @@ async def wait_for_paid_invoices():
|
||||
payment = await invoice_queue.get()
|
||||
await on_invoice_paid(payment)
|
||||
|
||||
|
||||
# async def register_listeners():
|
||||
# invoice_paid_chan_send, invoice_paid_chan_recv = trio.open_memory_channel(2)
|
||||
# register_invoice_listener(invoice_paid_chan_send)
|
||||
|
||||
@@ -17,7 +17,9 @@ 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)):
|
||||
return livestream_renderer().TemplateResponse("livestream/index.html", {"request": request, "user": user.dict()})
|
||||
return livestream_renderer().TemplateResponse(
|
||||
"livestream/index.html", {"request": request, "user": user.dict()}
|
||||
)
|
||||
|
||||
|
||||
@livestream_ext.get("/track/{track_id}", name="livestream.track_redirect_download")
|
||||
@@ -31,12 +33,12 @@ async def track_redirect_download(track_id, p: str = Query(...)):
|
||||
if not payment:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail=f"Couldn't find the payment {payment_hash} or track {track.id}."
|
||||
detail=f"Couldn't find the payment {payment_hash} or track {track.id}.",
|
||||
)
|
||||
|
||||
if payment.pending:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.PAYMENT_REQUIRED,
|
||||
detail=f"Payment {payment_hash} wasn't received yet. Please try again in a minute."
|
||||
detail=f"Payment {payment_hash} wasn't received yet. Please try again in a minute.",
|
||||
)
|
||||
return RedirectResponse(url=track.download_url)
|
||||
|
||||
@@ -23,27 +23,29 @@ from .crud import (
|
||||
|
||||
|
||||
@livestream_ext.get("/api/v1/livestream")
|
||||
async def api_livestream_from_wallet(req: Request, g: WalletTypeInfo = Depends(get_key_type)):
|
||||
async def api_livestream_from_wallet(
|
||||
req: Request, g: WalletTypeInfo = Depends(get_key_type)
|
||||
):
|
||||
ls = await get_or_create_livestream_by_wallet(g.wallet.id)
|
||||
tracks = await get_tracks(ls.id)
|
||||
producers = await get_producers(ls.id)
|
||||
print("INIT", ls, tracks, producers)
|
||||
try:
|
||||
return {
|
||||
**ls.dict(),
|
||||
**{
|
||||
"lnurl": ls.lnurl(request=req),
|
||||
"tracks": [
|
||||
dict(lnurl=track.lnurl(request=req), **track.dict())
|
||||
for track in tracks
|
||||
],
|
||||
"producers": [producer.dict() for producer in producers],
|
||||
},
|
||||
}
|
||||
**ls.dict(),
|
||||
**{
|
||||
"lnurl": ls.lnurl(request=req),
|
||||
"tracks": [
|
||||
dict(lnurl=track.lnurl(request=req), **track.dict())
|
||||
for track in tracks
|
||||
],
|
||||
"producers": [producer.dict() for producer in producers],
|
||||
},
|
||||
}
|
||||
except LnurlInvalidUrl:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.UPGRADE_REQUIRED,
|
||||
detail="LNURLs need to be delivered over a publically accessible `https` domain or Tor."
|
||||
detail="LNURLs need to be delivered over a publically accessible `https` domain or Tor.",
|
||||
)
|
||||
|
||||
|
||||
@@ -70,7 +72,9 @@ async def api_update_fee(fee_pct, g: WalletTypeInfo = Depends(get_key_type)):
|
||||
|
||||
@livestream_ext.post("/api/v1/livestream/tracks")
|
||||
@livestream_ext.put("/api/v1/livestream/tracks/{id}")
|
||||
async def api_add_track(data: CreateTrack, id=None, g: WalletTypeInfo = Depends(get_key_type)):
|
||||
async def api_add_track(
|
||||
data: CreateTrack, id=None, g: WalletTypeInfo = Depends(get_key_type)
|
||||
):
|
||||
ls = await get_or_create_livestream_by_wallet(g.wallet.id)
|
||||
|
||||
if data.producer_id:
|
||||
@@ -82,21 +86,10 @@ async def api_add_track(data: CreateTrack, id=None, g: WalletTypeInfo = Depends(
|
||||
|
||||
if id:
|
||||
await update_track(
|
||||
ls.id,
|
||||
id,
|
||||
data.name,
|
||||
data.download_url,
|
||||
data.price_msat or 0,
|
||||
p_id,
|
||||
ls.id, id, data.name, data.download_url, data.price_msat or 0, p_id
|
||||
)
|
||||
else:
|
||||
await add_track(
|
||||
ls.id,
|
||||
data.name,
|
||||
data.download_url,
|
||||
data.price_msat or 0,
|
||||
p_id,
|
||||
)
|
||||
await add_track(ls.id, data.name, data.download_url, data.price_msat or 0, p_id)
|
||||
return
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user