Add Piped backend: translation router + client wiring
- PipedBackendRouter serves Invidious-shaped /api/v1 JSON on the local web server at /playlet-piped-backend, translating from a real Piped instance (trending/search/suggestions/videos/comments/channels/ playlists/stats; auth endpoints return unsupported for now). - PipedToInvidious converters incl. HLS-first playback metadata (Piped's hls master via proxy.piped instance; muxed mp4 fallback) and previewFrames -> Playlet storyboard shape. - preferences: backend.selected gains 'piped'; new piped.instance (default https://api.piped.spaghet.io). - InvidiousService.GetInstance routes backend=piped to the local translation router; ResolveBackendId returns 'piped'.
This commit is contained in:
1 parent
4bd5c11f42
commit
ee36c76e8f
7 files changed
+965
-1
No files matched your search
@@ -0,0 +1,331 @@
|
||||
import "pkg:/components/Services/Piped/PipedToInvidious.bs"
|
||||
import "pkg:/source/services/HttpClient.bs"
|
||||
import "pkg:/source/utils/StringUtils.bs"
|
||||
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"
|
||||
|
||||
' Serves the Invidious /api/v1 JSON shapes that Playlet's client expects,
|
||||
' translated from a real Piped instance (https://github.com/TeamPiped/Piped).
|
||||
' Mounted on Playlet's local web server, the client talks to:
|
||||
' http://127.0.0.1:8888/playlet-piped-backend/api/v1/...
|
||||
' and this router fetches:
|
||||
' {piped.instance}/trending, /search, /streams/{id}, /channel/{id}, ...
|
||||
' (Piped serves its REST API at the instance root, no /api/v1 prefix.)
|
||||
namespace Http
|
||||
|
||||
class PipedBackendRouter extends HttpRouter
|
||||
|
||||
function new(server as object)
|
||||
super()
|
||||
|
||||
m.preferences = server.sceneNodes.preferences
|
||||
end function
|
||||
|
||||
function GetBaseUrl() as string
|
||||
instance = ValidString(m.preferences["piped.instance"])
|
||||
if StringUtils.IsNullOrEmpty(instance)
|
||||
instance = "https://api.piped.spaghet.io"
|
||||
end if
|
||||
if instance.Right(1) = "/"
|
||||
instance = instance.Left(instance.Len() - 1)
|
||||
end if
|
||||
v1 = instance.InStr("/api/v1")
|
||||
if v1 > 0
|
||||
instance = instance.Left(v1)
|
||||
end if
|
||||
return instance
|
||||
end function
|
||||
|
||||
function FetchJson(path as string, params = invalid as object) as dynamic
|
||||
url = m.GetBaseUrl() + path
|
||||
|
||||
request = HttpClient.Get(url)
|
||||
if params <> invalid
|
||||
for each key in params
|
||||
value = params[key]
|
||||
if value <> invalid and value <> ""
|
||||
request.QueryParam(key, value)
|
||||
end if
|
||||
end for
|
||||
end if
|
||||
|
||||
response = request.Await()
|
||||
if not response.IsSuccess()
|
||||
LogWarn("Piped backend: failed", path, response.StatusCode(), response.ErrorMessage())
|
||||
return invalid
|
||||
end if
|
||||
|
||||
return response.Json()
|
||||
end function
|
||||
|
||||
function SendJson(response as object, body as object, httpCode = 200 as integer) as void
|
||||
response.Json(body)
|
||||
if httpCode <> 200
|
||||
response.http_code = httpCode
|
||||
end if
|
||||
end function
|
||||
|
||||
function SendError(response as object, message as string) as void
|
||||
m.SendJson(response, { error: message }, 500)
|
||||
end function
|
||||
|
||||
' ---------------- GET dispatch ----------------
|
||||
|
||||
@get("*")
|
||||
function GetAll(context as object) as boolean
|
||||
route = context.request.route
|
||||
|
||||
prefix = "/playlet-piped-backend/api/v1/"
|
||||
if not route.StartsWith(prefix)
|
||||
return false
|
||||
end if
|
||||
|
||||
tail = route.Mid(prefix.Len())
|
||||
q = tail.InStr("?")
|
||||
if q > 0
|
||||
tail = tail.Left(q - 1)
|
||||
end if
|
||||
parts = tail.Split("/")
|
||||
head = ""
|
||||
if parts.Count() > 0
|
||||
head = parts[0]
|
||||
end if
|
||||
|
||||
if head = "stats"
|
||||
return m.GetStats(context)
|
||||
else if head = "trending"
|
||||
return m.GetTrending(context)
|
||||
else if head = "popular"
|
||||
' Piped has no "popular" feed; trending is the closest match
|
||||
return m.GetTrending(context)
|
||||
else if tail = "search/suggestions"
|
||||
return m.GetSearchSuggestions(context)
|
||||
else if head = "search"
|
||||
return m.GetSearch(context)
|
||||
else if head = "videos" and parts.Count() >= 2
|
||||
context.videoId = parts[1]
|
||||
return m.GetVideo(context)
|
||||
else if head = "comments" and parts.Count() >= 2
|
||||
context.videoId = parts[1]
|
||||
return m.GetComments(context)
|
||||
else if head = "channels" and parts.Count() >= 2
|
||||
context.channelId = parts[1]
|
||||
return m.GetChannel(context)
|
||||
else if head = "playlists" and parts.Count() >= 2
|
||||
context.playlistId = parts[1]
|
||||
return m.GetPlaylist(context)
|
||||
else if head = "auth"
|
||||
return m.UnsupportedAuth(context)
|
||||
end if
|
||||
|
||||
return false
|
||||
end function
|
||||
|
||||
' ---------------- POST/DELETE dispatch (auth) ----------------
|
||||
|
||||
@post("*")
|
||||
function PostAll(context as object) as boolean
|
||||
if context.request.route.StartsWith("/playlet-piped-backend/api/v1/auth/")
|
||||
return m.UnsupportedAuth(context)
|
||||
end if
|
||||
return false
|
||||
end function
|
||||
|
||||
@delete("*")
|
||||
function DeleteAll(context as object) as boolean
|
||||
if context.request.route.StartsWith("/playlet-piped-backend/api/v1/auth/")
|
||||
return m.UnsupportedAuth(context)
|
||||
end if
|
||||
return false
|
||||
end function
|
||||
|
||||
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."
|
||||
}, 400)
|
||||
return true
|
||||
end function
|
||||
|
||||
' ---------------- handlers ----------------
|
||||
|
||||
function GetStats(context as object) as boolean
|
||||
response = context.response
|
||||
|
||||
stats = m.FetchJson("/stats")
|
||||
if not IsAssociativeArray(stats)
|
||||
stats = {}
|
||||
end if
|
||||
|
||||
m.SendJson(response, {
|
||||
software: "piped"
|
||||
version: ValidString(stats.version)
|
||||
userCount: ValidInt(stats.userCount)
|
||||
lastStreamed: ""
|
||||
videoCountPlaylists: 0
|
||||
videoCount: 0
|
||||
usage: {}
|
||||
})
|
||||
return true
|
||||
end function
|
||||
|
||||
function GetTrending(context as object) as boolean
|
||||
request = context.request
|
||||
response = context.response
|
||||
|
||||
region = ValidString(request.query.region)
|
||||
if StringUtils.IsNullOrEmpty(region)
|
||||
region = "US"
|
||||
end if
|
||||
|
||||
items = m.FetchJson("/trending", { region: region })
|
||||
if not IsArray(items)
|
||||
m.SendError(response, "Failed to get trending feed")
|
||||
return true
|
||||
end if
|
||||
|
||||
videos = PipedToInvidious.VideosFromPiped(items)
|
||||
if videos.Count() = 0
|
||||
m.SendError(response, "Failed to get trending feed")
|
||||
return true
|
||||
end if
|
||||
|
||||
' Invidious /api/v1/trending returns a bare array; DefaultHandler
|
||||
' wraps it as {items: [...]}.
|
||||
response.Json(videos)
|
||||
return true
|
||||
end function
|
||||
|
||||
function GetSearchSuggestions(context as object) as boolean
|
||||
request = context.request
|
||||
response = context.response
|
||||
|
||||
query = ValidString(request.query.q)
|
||||
|
||||
items = m.FetchJson("/suggestions", { query: query })
|
||||
suggestions = []
|
||||
if IsArray(items)
|
||||
for each item in items
|
||||
suggestions.push(ValidString(item))
|
||||
end for
|
||||
end if
|
||||
|
||||
m.SendJson(response, {
|
||||
suggestions: suggestions
|
||||
params: ""
|
||||
})
|
||||
return true
|
||||
end function
|
||||
|
||||
function GetSearch(context as object) as boolean
|
||||
request = context.request
|
||||
response = context.response
|
||||
|
||||
query = ValidString(request.query.q)
|
||||
continuation = ValidString(request.query.continuation)
|
||||
|
||||
if StringUtils.IsNullOrEmpty(continuation)
|
||||
params = { q: query, filter: "all" }
|
||||
page = ValidString(request.query.page)
|
||||
if not StringUtils.IsNullOrEmpty(page)
|
||||
params.page = page
|
||||
end if
|
||||
data = m.FetchJson("/search", params)
|
||||
else
|
||||
nextpage = PipedToInvidious.Base64Decode(continuation)
|
||||
data = m.FetchJson("/nextpage/search", { q: query, filter: "all", nextpage: nextpage })
|
||||
end if
|
||||
|
||||
if not IsAssociativeArray(data) or not IsArray(data.items)
|
||||
m.SendError(response, "Failed to search: no results found")
|
||||
return true
|
||||
end if
|
||||
|
||||
m.SendJson(response, PipedToInvidious.SearchFromPiped(data))
|
||||
return true
|
||||
end function
|
||||
|
||||
function GetVideo(context as object) as boolean
|
||||
response = context.response
|
||||
|
||||
data = m.FetchJson("/streams/" + context.videoId)
|
||||
if not IsAssociativeArray(data) or StringUtils.IsNullOrEmpty(ValidString(data.title))
|
||||
m.SendError(response, "Failed to get video info")
|
||||
return true
|
||||
end if
|
||||
|
||||
m.SendJson(response, PipedToInvidious.VideoMetadataFromPiped(data, ValidString(context.videoId)))
|
||||
return true
|
||||
end function
|
||||
|
||||
function GetComments(context as object) as boolean
|
||||
request = context.request
|
||||
response = context.response
|
||||
|
||||
videoId = ValidString(context.videoId)
|
||||
continuation = ValidString(request.query.continuation)
|
||||
|
||||
if StringUtils.IsNullOrEmpty(continuation)
|
||||
data = m.FetchJson("/comments/" + videoId)
|
||||
else
|
||||
nextpage = PipedToInvidious.Base64Decode(continuation)
|
||||
data = m.FetchJson("/nextpage/comments/" + videoId, { nextpage: nextpage })
|
||||
end if
|
||||
|
||||
if not IsAssociativeArray(data)
|
||||
m.SendError(response, "Failed to get comments")
|
||||
return true
|
||||
end if
|
||||
|
||||
m.SendJson(response, PipedToInvidious.CommentsFromPiped(data))
|
||||
return true
|
||||
end function
|
||||
|
||||
function GetChannel(context as object) as boolean
|
||||
request = context.request
|
||||
response = context.response
|
||||
|
||||
channelId = ValidString(context.channelId)
|
||||
continuation = ValidString(request.query.continuation)
|
||||
|
||||
if StringUtils.IsNullOrEmpty(continuation)
|
||||
data = m.FetchJson("/channel/" + channelId)
|
||||
else
|
||||
nextpage = PipedToInvidious.Base64Decode(continuation)
|
||||
data = m.FetchJson("/nextpage/channel/" + channelId, { nextpage: nextpage })
|
||||
end if
|
||||
|
||||
if not IsAssociativeArray(data)
|
||||
m.SendError(response, "Failed to get channel")
|
||||
return true
|
||||
end if
|
||||
|
||||
m.SendJson(response, PipedToInvidious.ChannelFromPiped(data, channelId))
|
||||
return true
|
||||
end function
|
||||
|
||||
function GetPlaylist(context as object) as boolean
|
||||
request = context.request
|
||||
response = context.response
|
||||
|
||||
playlistId = ValidString(context.playlistId)
|
||||
params = {}
|
||||
continuation = ValidString(request.query.continuation)
|
||||
if not StringUtils.IsNullOrEmpty(continuation)
|
||||
params.nextpage = PipedToInvidious.Base64Decode(continuation)
|
||||
end if
|
||||
|
||||
data = m.FetchJson("/playlists/" + playlistId, params)
|
||||
if not IsAssociativeArray(data) or StringUtils.IsNullOrEmpty(ValidString(data.name))
|
||||
m.SendError(response, "Failed to get playlist")
|
||||
return true
|
||||
end if
|
||||
|
||||
m.SendJson(response, PipedToInvidious.PlaylistFromPiped(data, playlistId))
|
||||
return true
|
||||
end function
|
||||
|
||||
end class
|
||||
|
||||
end namespace
|
||||
@@ -8,6 +8,7 @@ import "pkg:/components/Web/PlayletWebServer/Middleware/HomeRouter.bs"
|
||||
import "pkg:/components/Web/PlayletWebServer/Middleware/InnertubeRouter.bs"
|
||||
import "pkg:/components/Web/PlayletWebServer/Middleware/InvidiousRouter.bs"
|
||||
import "pkg:/components/Web/PlayletWebServer/Middleware/PlayletInvidiousBackendRouter.bs"
|
||||
import "pkg:/components/Web/PlayletWebServer/Middleware/PipedBackendRouter.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"
|
||||
@@ -86,6 +87,7 @@ function SetupRoutes(server as object)
|
||||
server.UseRouter(new Http.CacheRouter(server))
|
||||
server.UseRouter(new Http.RegistryRouter())
|
||||
server.UseRouter(new Http.PlayletInvidiousBackendRouter(server))
|
||||
server.UseRouter(new Http.PipedBackendRouter(server))
|
||||
server.UseRouter(new Http.InnertubeRouter(server))
|
||||
|
||||
etags = new Http.EtagUtils()
|
||||
|
||||
Reference in new issue
Block a user