Skip to content

Commit

Permalink
Web app hot fix (#459)
Browse files Browse the repository at this point in the history
* Web app hot fix

* Small tweak
  • Loading branch information
iBicha authored Sep 22, 2024
1 parent 4f31c20 commit 11d1aef
Show file tree
Hide file tree
Showing 8 changed files with 451 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Support for casting clips from web app
- A workaround for video loading errors: uses Playlet Web app and [YouTube.js](https://github.com/LuanRT/YouTube.js) to load streaming data. This workaround is limited, and might not work. It doesn't have closed captions, nor trickplay thumbnails (storyboards).
- **How To use**: Open the web app, click on a video, and choose "Play on <TV_NAME> (HOT FIX)".

## [0.25.6] - 2024-09-12

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import "pkg:/source/services/HttpClient.bs"

namespace Http

class ProxyRouter extends HttpRouter

function new(server as object)
super()

task = server.task
m.invidiousNode = task.invidious
m.invidiousService = new Invidious.InvidiousService(m.invidiousNode)
end function

@post("/api/proxy")
function ProxyRequest(context as object) as boolean
request = context.request
response = context.response

requestArgs = request.Json()
if requestArgs = invalid
response.Default(400, "Invalid request body")
return true
end if

httpReq = HttpClient.FromObject(requestArgs)
httpRes = httpReq.Await()

result = {
"status": httpRes.StatusCode()
"headers": httpRes.Headers()
"body": httpRes.Text()
}

response.Json(result)
return true
end function

@post("/api/ytjs-cache")
function CacheVideoInfo(context as object) as boolean
request = context.request
response = context.response

json = request.Json()
if json = invalid
response.Default(400, "Invalid request body")
return true
end if

instance = m.invidiousService.GetInstance()
videoId = json.videoId

url = `${instance}${Invidious.VIDEOS_ENDPOINT}/${videoId}`

' Place a fake cache so we can read it later
requestObj = HttpClient.Get(url)
requestObj.CacheSeconds(18000)
responseObj = new HttpClient.HttpResponse(requestObj, invalid)
responseObj.OverrideStatusCode(200)
responseObj.OverrideText(request.body)
HttpClientCache.Set(responseObj)

response.Default(204, "OK")
return true
end function

end class

end namespace
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "pkg:/components/Web/PlayletWebServer/Middleware/InvidiousRouter.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/PlayletLibUrlsRouter.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/PreferencesRouter.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/ProfilesRouter.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/ProxyRouter.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/RegistryRouter.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/SearchHistoryRouter.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/StateApiRouter.bs"
Expand Down Expand Up @@ -52,6 +53,7 @@ function SetupRoutes(server as object)
server.UseRouter(new Http.PlayletLibUrlsRouter(server))
server.UseRouter(new Http.DialRouter(server))
server.UseRouter(new Http.CacheRouter())
server.UseRouter(new Http.ProxyRouter(server))

etags = new Http.EtagUtils()
server.UseRouter(new Http.HttpStaticFilesRouter("/", "libpkg:/www", etags, { staticFiles: true }))
Expand Down
18 changes: 18 additions & 0 deletions playlet-lib/src/source/services/HttpClient.bs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ namespace HttpClient
return (new HttpRequest()).Method("HEAD").Url(url)
end function

function FromObject(obj as object) as HttpRequest
request = new HttpRequest()

for each key in obj
if IsFunction(request[key])
request[key](obj[key])
else
throw `Invalid key: "${key}" in object passed to HttpClient.FromObject`
end if
end for

return request
end function

class HttpRequest

public urlTransfer as object
Expand Down Expand Up @@ -598,6 +612,10 @@ namespace HttpClient
m._statusCode = statusCode
end function

function OverrideText(text as string)
m._text = text
end function

function IsSuccess() as boolean
statusCode = m.StatusCode()
return statusCode >= 200 and statusCode < 400
Expand Down
54 changes: 52 additions & 2 deletions playlet-web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions playlet-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@
"typescript": "^5.6.2",
"vite": "^5.4.7",
"vite-tsconfig-paths": "^5.0.1"
},
"dependencies": {
"youtubei.js": "^10.5.0"
}
}
Loading

0 comments on commit 11d1aef

Please sign in to comment.