Piped backend: absolute HLS URI rewrite, roLongInt-safe epoch conversion, /manifest endpoint

This commit is contained in:
spaghet committed 2026-11-23 14:35:27 -05:00
1 parent a30bf73d0f
commit be8c79c3d1
4 files changed
+342 -148

No files matched your search

@@ -41,6 +41,12 @@ namespace Http
hlsManifest = hlsResponse.Text()
' Some backends (Piped) return master manifests with relative
' variant URIs; since we serve the manifest from 127.0.0.1, the
' player would resolve them against the local server and 404.
' Make them absolute against the original manifest URL.
hlsManifest = m.MakeRelativeUrisAbsolute(hlsManifest, url)
hlsManifest = m.PrependManifestHeader(hlsManifest, v, url)
qualityFilter = m.CreateQualityFilter(quality)
@@ -253,6 +259,54 @@ ${storyboard.templateUrl.replace("$M", `${i}`)}
return hlsManifest
end function
function MakeRelativeUrisAbsolute(manifest as string, baseUrl as string) as string
if manifest = invalid or manifest = "" or baseUrl = invalid or baseUrl = ""
return manifest
end if
parsedUrl = UrlUtils.ParseUrl(baseUrl)
if parsedUrl = invalid or parsedUrl.host = invalid
return manifest
end if
protocol = parsedUrl.protocol
if protocol = invalid or protocol = ""
protocol = "https"
end if
origin = protocol + "://" + parsedUrl.host
if parsedUrl.port <> invalid and parsedUrl.port <> "" and parsedUrl.port <> "80" and parsedUrl.port <> "443"
origin = origin + ":" + parsedUrl.port
end if
' normalize line endings (CR-only manifests would be one line)
manifest = manifest.Replace(chr(13) + chr(10), chr(10))
manifest = manifest.Replace(chr(13), chr(10))
lines = manifest.Split(chr(10))
for i = 0 to lines.Count() - 1
line = lines[i]
trimmed = line.Trim()
if Left(trimmed, 1) = "/"
' bare relative URI line (variant/media playlist)
lines[i] = origin + trimmed
else if Left(trimmed, 1) = "#" and InStr(UCase(trimmed), "URI=" + chr(34) + "/") > 0
' attribute form: #EXT-X-MEDIA:...URI="/..." etc.
' split on double quotes; rewrite any segment that starts
' with a bare slash. no index math needed.
parts = trimmed.Split(chr(34))
for p = 0 to parts.Count() - 1
if Left(parts[p], 1) = "/"
parts[p] = origin + parts[p]
end if
end for
lines[i] = parts.Join(chr(34))
end if
end for
return lines.Join(chr(10))
end function
function GetPlayerMetadata() as object
player = m.videoQueue.player
@@ -5,6 +5,7 @@ import "pkg:/source/utils/Types.bs"
import "pkg:/source/utils/UrlUtils.bs"
import "pkg:/source/utils/Logging.bs"
import "pkg:/components/Web/WebServer/Http/HttpRouter.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/HlsRouter.bs"
' Serves the Invidious /api/v1 JSON shapes that Playlet's client expects,
' translated from a real Piped instance (https://github.com/TeamPiped/Piped).
@@ -38,6 +39,92 @@ namespace Http
return instance
end function
' GET /playlet-piped-backend/api/v1/manifest?url=<piped hls master>
' Fetches the Piped HLS master and rewrites its relative variant and
' media URIs to absolute proxy URLs, so Playlet's /api/hls proxy and
' the Roku player resolve them against the Piped instance, not 127.0.0.1.
function GetRewrittenHlsManifest(context as object) as boolean
request = context.request
response = context.response
url = request.query.url
if StringUtils.IsNullOrEmpty(url)
response.Default(400, "Missing url")
return true
end if
hlsResponse = HttpClient.Get(url).Await()
if not hlsResponse.IsSuccess()
LogWarn("Piped backend: manifest fetch failed", url, hlsResponse.StatusCode())
response.Redirect(url)
return true
end if
manifest = m.MakeRelativeUrisAbsolute(hlsResponse.Text(), url)
response.http_code = 200
response.headers["Content-Type"] = "application/x-mpegURL"
response.headers["Cache-Control"] = "no-cache, must-revalidate"
response.SetBodyDataString(manifest)
return true
end function
function MakeRelativeUrisAbsolute(manifest as string, baseUrl as string) as string
if manifest = invalid or manifest = "" or baseUrl = invalid or baseUrl = ""
return manifest
end if
parsedUrl = UrlUtils.ParseUrl(baseUrl)
if parsedUrl = invalid or parsedUrl.host = invalid
return manifest
end if
protocol = ValidString(parsedUrl.protocol)
if protocol = ""
protocol = "https"
end if
origin = protocol + "://" + parsedUrl.host
port = ValidString(parsedUrl.port)
if port <> "" and port <> "80" and port <> "443"
origin = origin + ":" + port
end if
bareCount = 0
attrCount = 0
' normalize line endings: some hosts serve manifests with CR-only
' line terminators, which Split("\n") would leave as one line
manifest = manifest.Replace(chr(13) + chr(10), chr(10))
manifest = manifest.Replace(chr(13), chr(10))
lines = manifest.Split(chr(10))
for i = 0 to lines.Count() - 1
trimmed = lines[i].Trim()
if Left(trimmed, 1) = "/"
' bare relative URI line (variant/media playlist)
lines[i] = origin + trimmed
bareCount = bareCount + 1
else if Left(trimmed, 1) = "#" and InStr(UCase(trimmed), "URI=" + chr(34) + "/") > 0
' attribute form: #EXT-X-MEDIA:...URI="/..." etc.
parts = trimmed.Split(chr(34))
for q = 0 to parts.Count() - 1
if Left(parts[q], 1) = "/"
parts[q] = origin + parts[q]
end if
end for
lines[i] = parts.Join(chr(34))
attrCount = attrCount + 1
end if
end for
result = lines.Join("\n")
return result
end function
function LibBuildTag() as string
return "PIPED-LIB-14"
end function
function FetchJson(path as string, params = invalid as object) as dynamic
url = m.GetBaseUrl() + path
@@ -93,7 +180,9 @@ namespace Http
head = parts[0]
end if
if head = "stats"
if tail = "manifest"
return m.GetRewrittenHlsManifest(context)
else if head = "stats"
return m.GetStats(context)
else if head = "trending"
return m.GetTrending(context)
@@ -143,7 +232,7 @@ namespace Http
function UnsupportedAuth(context as object) as boolean
m.SendJson(context.response, {
error: "Piped backend accounts are not supported yet. Browse anonymously, or log in to your Piped instance in a browser."
"error": "Piped backend accounts are not supported yet. Browse anonymously, or log in to your Piped instance in a browser."
}, 400)
return true
end function
@@ -159,13 +248,15 @@ namespace Http
end if
m.SendJson(response, {
software: "piped"
version: ValidString(stats.version)
userCount: ValidInt(stats.userCount)
lastStreamed: ""
videoCountPlaylists: 0
videoCount: 0
usage: {}
"buildTag": m.LibBuildTag()
"software": "piped"
"version": ValidString(stats.version)
"userCount": ValidInt(stats.userCount)
"lastStreamed": ""
"videoCountPlaylists": 0
"videoCount": 0
"usage": {}
})
return true
end function
@@ -212,8 +303,8 @@ namespace Http
end if
m.SendJson(response, {
suggestions: suggestions
params: ""
"suggestions": suggestions
"params": ""
})
return true
end function