188 lines
6.5 KiB
Plaintext
188 lines
6.5 KiB
Plaintext
import "pkg:/components/Services/Invidious/InvidiousService.bs"
|
|
import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs"
|
|
import "pkg:/components/VideoQueue/VideoQueueUtils.bs"
|
|
import "pkg:/components/Web/WebServer/Http/HttpRouter.bs"
|
|
import "pkg:/source/utils/MathUtils.bs"
|
|
|
|
namespace Http
|
|
|
|
class VideoQueueRouter extends HttpRouter
|
|
|
|
function new(server as object)
|
|
super()
|
|
|
|
m.videoQueueNode = server.sceneNodes.videoQueue
|
|
m.invidiousNode = server.sceneNodes.invidious
|
|
m.invidiousService = new Invidious.InvidiousService(m.invidiousNode)
|
|
end function
|
|
|
|
@get("/api/queue")
|
|
function GetQueue(context as object) as boolean
|
|
response = context.response
|
|
|
|
queue = VideoQueueUtils.GetQueueDto(m.videoQueueNode)
|
|
response.Json(queue)
|
|
return true
|
|
end function
|
|
|
|
@post("/api/queue")
|
|
function AddToQueue(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
|
|
payload = request.BodyJson()
|
|
if payload = invalid
|
|
response.Default(400, `Invalid JSON payload`)
|
|
return true
|
|
end if
|
|
|
|
videoId = payload.videoId
|
|
playlistId = payload.playlistId
|
|
if StringUtils.IsNullOrEmpty(videoId) and StringUtils.IsNullOrEmpty(playlistId)
|
|
response.Default(400, `Missing "videoId" or "playlistId" in payload`)
|
|
return true
|
|
end if
|
|
|
|
if IsString(payload.timestamp)
|
|
payload.timestamp = payload.timestamp.toInt()
|
|
else if IsString(payload.percentDurationWatched)
|
|
payload.percentDurationWatched = payload.percentDurationWatched.toFloat()
|
|
end if
|
|
|
|
instance = m.invidiousService.GetInstance()
|
|
|
|
contentNode = InvidiousContent.ToRowCellContentNode(payload, instance)
|
|
|
|
nodeType = contentNode.type
|
|
if nodeType = "playlist"
|
|
m.videoQueueNode.addToQueuePlaylist = contentNode
|
|
else if nodeType = "video"
|
|
m.videoQueueNode.addToQueue = [contentNode]
|
|
end if
|
|
|
|
response.Default(204, "OK")
|
|
return true
|
|
end function
|
|
|
|
@post("/api/queue/play")
|
|
function Play(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
|
|
payload = request.BodyJson()
|
|
if payload = invalid
|
|
response.Default(400, `Invalid JSON payload`)
|
|
return true
|
|
end if
|
|
|
|
videoId = payload.videoId
|
|
playlistId = payload.playlistId
|
|
if StringUtils.IsNullOrEmpty(videoId) and StringUtils.IsNullOrEmpty(playlistId)
|
|
response.Default(400, `Missing "videoId" or "playlistId" in payload`)
|
|
return true
|
|
end if
|
|
|
|
if IsString(payload.timestamp)
|
|
payload.timestamp = payload.timestamp.toInt()
|
|
else if IsString(payload.percentDurationWatched)
|
|
payload.percentDurationWatched = payload.percentDurationWatched.toFloat()
|
|
end if
|
|
|
|
instance = m.invidiousService.GetInstance()
|
|
|
|
contentNode = InvidiousContent.ToRowCellContentNode(payload, instance)
|
|
|
|
nodeType = contentNode.type
|
|
|
|
if nodeType = "playlist"
|
|
playlistIndex = MathUtils.Max(0, ValidInt(payload.playlistIndex))
|
|
|
|
m.videoQueueNode.playPlaylist = {
|
|
playlistNode: contentNode
|
|
playlistIndex: playlistIndex
|
|
}
|
|
else if nodeType = "video"
|
|
m.videoQueueNode.playVideo = contentNode
|
|
end if
|
|
|
|
response.Default(204, "OK")
|
|
return true
|
|
end function
|
|
|
|
' A shortcut to play a video using a /watch endpoint
|
|
@get("/watch")
|
|
function PlayWatch(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
|
|
videoId = request.query.v
|
|
playlistId = request.query.list
|
|
playlistIndex = request.query.index
|
|
|
|
hasValidVideo = not StringUtils.IsNullOrEmpty(videoId)
|
|
hasValidPlaylist = not StringUtils.IsNullOrEmpty(playlistId) and not StringUtils.IsNullOrEmpty(playlistIndex)
|
|
if not hasValidVideo and not hasValidPlaylist
|
|
response.Default(400, `Missing video id ("v") or playlist id and index ("list" and "index") in query`)
|
|
return true
|
|
end if
|
|
|
|
instance = m.invidiousService.GetInstance()
|
|
|
|
if hasValidPlaylist
|
|
payload = {
|
|
"type": "playlist"
|
|
"playlistId": playlistId
|
|
}
|
|
|
|
contentNode = InvidiousContent.ToRowCellContentNode(payload, instance)
|
|
playlistIndex = MathUtils.Max(0, ValidInt(playlistIndex.toInt()) - 1)
|
|
|
|
' TODO:P1 handle playlist with timestamp
|
|
' TODO:P1 videoId should take precedence over index
|
|
m.videoQueueNode.playPlaylist = {
|
|
playlistNode: contentNode
|
|
playlistIndex: playlistIndex
|
|
}
|
|
|
|
response.Default(200, "Now playing playlist - " + playlistId)
|
|
return true
|
|
else if hasValidVideo
|
|
timestamp = request.query.t
|
|
if IsString(timestamp)
|
|
if timestamp.endsWith("s")
|
|
timestamp = timestamp.left(timestamp.Len() - 1)
|
|
end if
|
|
timestamp = timestamp.toInt()
|
|
end if
|
|
timestamp = ValidInt(timestamp)
|
|
|
|
payload = {
|
|
"type": "video"
|
|
"videoId": videoId
|
|
"timestamp": timestamp
|
|
}
|
|
|
|
contentNode = InvidiousContent.ToRowCellContentNode(payload, instance)
|
|
m.videoQueueNode.playVideo = contentNode
|
|
|
|
response.Default(200, "Now playing video - " + videoId)
|
|
return true
|
|
end if
|
|
|
|
return false
|
|
end function
|
|
|
|
@delete("/api/queue")
|
|
function ClearQueue(context as object) as boolean
|
|
response = context.response
|
|
|
|
m.videoQueueNode.clearQueue = true
|
|
|
|
response.Default(204, "OK")
|
|
return true
|
|
end function
|
|
|
|
end class
|
|
|
|
end namespace
|