Play API prototype (#8)
* Play API prototype * Web app * Drag and drop support * Handle different urls * Fix redirect * Increase max connections * changelog * Limit timeout * Use fetch * Redirects * Formatting
This commit is contained in:
1 parent
192eec9519
commit
651bb54dee
15 files changed
+180
-39
No files matched your search
@@ -5,6 +5,7 @@ function Init()
|
||||
m.rowList = m.top.FindNode("rowList")
|
||||
m.top.ObserveField("rowItemSelected", "OnHomeGridScreenVideoSelected")
|
||||
m.top.ObserveField("focus", "OnFocusChange")
|
||||
m.top.ObserveField("playvideo", "OnPlayVideoCommand")
|
||||
|
||||
m.top.focus = true
|
||||
|
||||
@@ -132,6 +133,12 @@ function CloseVideo()
|
||||
end if
|
||||
end function
|
||||
|
||||
function OnPlayVideoCommand()
|
||||
videoId = m.top.playvideo
|
||||
CloseVideo()
|
||||
ShowVideoScreenFull(videoId)
|
||||
end function
|
||||
|
||||
function OnHomeGridScreenVideoFocused(event as object) as void
|
||||
' Skip loading the video after first load
|
||||
if m.firstFocus = invalid
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
<field id="jumpToRowItem" type="intarray" alias="RowList.jumpToRowItem" />
|
||||
<field id="escape" type="string" alwaysNotify="true" />
|
||||
<field id="focus" type="boolean" alwaysNotify="true" />
|
||||
<field id="playvideo" type="string" alwaysNotify="true" />
|
||||
</interface>
|
||||
<children>
|
||||
<RowList
|
||||
|
||||
@@ -28,6 +28,7 @@ function InitWebServer()
|
||||
m.serverTask = createObject("roSGNode", "WebServerTask")
|
||||
m.serverTask.id = "WebServerTask"
|
||||
m.serverTask.ObserveField("login", "OnWebServerLogin")
|
||||
m.serverTask.ObserveField("command", "OnWebServerCommand")
|
||||
m.serverTask.control = "RUN"
|
||||
end function
|
||||
|
||||
@@ -85,6 +86,12 @@ function OnWebServerLogin()
|
||||
m.settingsScreen.visible = false
|
||||
end function
|
||||
|
||||
function OnWebServerCommand()
|
||||
if m.serverTask.command.StartsWith("play:")
|
||||
m.homeGridScreen.playvideo = m.serverTask.command.Mid(5)
|
||||
end if
|
||||
end function
|
||||
|
||||
function GetIndexOfChild(parent as object, childId as string) as integer
|
||||
count = parent.getChildCount()
|
||||
for i = 0 to count - 1
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import "pkg:/source/utils/General.bs"
|
||||
|
||||
function Init()
|
||||
m.top.url = `http:/${GetLocalIpAddress()}:8888/index.html?tab=settings`
|
||||
m.top.url = `http:/${GetLocalIpAddress()}:8888`
|
||||
end function
|
||||
|
||||
function OnUrlSet()
|
||||
|
||||
@@ -77,10 +77,10 @@ namespace Http
|
||||
m.source = Http.HttpResponseSource.GENERATED
|
||||
end function
|
||||
|
||||
function Redirect(where as string)
|
||||
function Redirect(where as string, code = 301 as integer)
|
||||
m.location = where
|
||||
message = "Moved to: <a href=" + Quote() + where + Quote() + ">" + where + "</a>"
|
||||
m.Default(301, message)
|
||||
m.Default(code, message)
|
||||
end function
|
||||
|
||||
function Json(data as object)
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Http
|
||||
PackageName = "Playlet"
|
||||
Port = 8888
|
||||
MessagePort = invalid
|
||||
MaxConnections = 3
|
||||
MaxConnections = 10
|
||||
Timeout = 60 * 1000
|
||||
MaxRequestLength = 4000
|
||||
Idletime = 60
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
namespace Http
|
||||
|
||||
class CommandsApiRouter extends HttpRouter
|
||||
|
||||
function new()
|
||||
super()
|
||||
|
||||
m.Get("*", function(context as object) as boolean
|
||||
request = context.request
|
||||
response = context.response
|
||||
server = context.server
|
||||
|
||||
route = request.uri
|
||||
|
||||
if route.StartsWith("/api/command/play")
|
||||
components = WebUtils.UrlQueryComponents(route)
|
||||
if components["v"] = invalid
|
||||
response.Default(400, "Missing arg 'v'")
|
||||
return true
|
||||
end if
|
||||
|
||||
server.task.top.command = `play:${components["v"]}`
|
||||
if components["redirect"] = "true"
|
||||
' 302 to prevent redirection to current host permanently
|
||||
response.Redirect(`${Invidious.GetCurrentHost()}/watch?v=${components["v"]}`, 302)
|
||||
else
|
||||
response.Default(200, "OK")
|
||||
end if
|
||||
return true
|
||||
end if
|
||||
|
||||
return false
|
||||
end function)
|
||||
end function
|
||||
|
||||
end class
|
||||
|
||||
end namespace
|
||||
@@ -10,12 +10,12 @@ namespace Http
|
||||
response = context.response
|
||||
|
||||
method = request.method
|
||||
route = request.uri
|
||||
uri = request.uri
|
||||
|
||||
if method = ""
|
||||
response.Default(400, "You sent a request that the server couldn't understand.")
|
||||
else
|
||||
response.Default(404, `Cannot ${method} ${route}`)
|
||||
response.Default(404, `Cannot ${method} ${uri}`)
|
||||
end if
|
||||
|
||||
return true
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace Http
|
||||
RegistryUtils.Write("invidious_token", token)
|
||||
server.task.top.login = true
|
||||
end if
|
||||
response.Redirect("/index.html?tab=settings")
|
||||
response.Redirect("/", 302)
|
||||
return true
|
||||
end if
|
||||
return false
|
||||
|
||||
@@ -5,6 +5,7 @@ import "pkg:/components/WebServer/Middleware/HttpDefaultRouter.bs"
|
||||
import "pkg:/components/WebServer/Middleware/InvidiousRouter.bs"
|
||||
import "pkg:/components/WebServer/Middleware/RegistryApiRouter.bs"
|
||||
import "pkg:/components/WebServer/Middleware/StateApiRouter.bs"
|
||||
import "pkg:/components/WebServer/Middleware/CommandsApiRouter.bs"
|
||||
|
||||
function Init()
|
||||
m.top.functionName = "WebServerLoop"
|
||||
@@ -16,8 +17,18 @@ function WebServerLoop()
|
||||
m.settings = new Http.HttpSettings(m.webServerPort)
|
||||
' Root at www to get http://IP_ADDRESS:PORT/index.html
|
||||
m.settings.WwwRoot = "pkg:/www"
|
||||
m.settings.Timeout = 5000
|
||||
m.server = new Http.HttpServer(m.settings, m)
|
||||
|
||||
homeRouter = new Http.HttpRouter()
|
||||
homeRouter.Get("/", function(context as object) as boolean
|
||||
response = context.response
|
||||
response.Redirect("/index.html?tab=settings", 302)
|
||||
return true
|
||||
end function)
|
||||
|
||||
m.server.UseRouter(homeRouter)
|
||||
m.server.UseRouter(new Http.CommandsApiRouter())
|
||||
m.server.UseRouter(new Http.StateApiRouter())
|
||||
m.server.UseRouter(new Http.RegistryApiRouter())
|
||||
m.server.UseRouter(new Http.InvidiousRouter())
|
||||
|
||||
@@ -3,5 +3,6 @@
|
||||
<component name="WebServerTask" extends="Task">
|
||||
<interface>
|
||||
<field id="login" type="boolean" alwaysNotify="true" />
|
||||
<field id="command" type="string" alwaysNotify="true" />
|
||||
</interface>
|
||||
</component>
|
||||
Reference in new issue
Block a user