912 lines
30 KiB
Plaintext
912 lines
30 KiB
Plaintext
import "pkg:/components/Services/Innertube/InnertubeService.bs"
|
|
import "pkg:/components/Web/WebServer/Http/HttpRouter.bs"
|
|
|
|
namespace Http
|
|
|
|
class PlayletInvidiousBackendRouter extends HttpRouter
|
|
|
|
function new(server as object)
|
|
super()
|
|
|
|
m.innertube = server.task.innertube
|
|
end function
|
|
|
|
@get("/playlet-invidious-backend/authorize_token")
|
|
function AuthorizeToken(context as object) as boolean
|
|
response = context.response
|
|
|
|
response.Json({
|
|
"error": "Playlet built-in Invidious backend is not a real Invidious instance, and does not support accounts. Please use the login with YouTube option instead."
|
|
})
|
|
response.http_code = 400
|
|
return true
|
|
end function
|
|
|
|
@post("/playlet-invidious-backend/api/v1/auth/tokens/unregister")
|
|
function UnregisterToken(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
|
|
accessToken = request.body
|
|
|
|
if StringUtils.IsNullOrEmpty(accessToken)
|
|
response.Json({
|
|
"error": "Missing 'accessToken' body parameter"
|
|
})
|
|
response.http_code = 400
|
|
return true
|
|
end if
|
|
|
|
if not InnertubeService.AuthRevokeAccessToken({ "accessToken": accessToken })
|
|
response.Json({
|
|
"error": "Failed to unregister token"
|
|
})
|
|
response.http_code = 500
|
|
return true
|
|
end if
|
|
|
|
response.Default(204, "OK")
|
|
return true
|
|
end function
|
|
|
|
@get("/playlet-invidious-backend/api/v1/auth/feed")
|
|
function GetAuthFeed(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
|
|
accessToken = request.headers["authorization"]
|
|
if StringUtils.IsNullOrEmpty(accessToken) or not accessToken.StartsWith("Bearer ")
|
|
response.Json({
|
|
"error": "Missing or invalid 'Authorization' header"
|
|
})
|
|
response.http_code = 403
|
|
return true
|
|
end if
|
|
|
|
options = {
|
|
"accessToken": accessToken.Mid("Bearer ".Len())
|
|
}
|
|
continuation = request.query.continuation
|
|
if not StringUtils.IsNullOrEmpty(continuation)
|
|
options.continuation = continuation
|
|
end if
|
|
|
|
result = InnertubeService.GetSubscriptions(options)
|
|
if result.error <> invalid
|
|
response.Json(result)
|
|
response.http_code = 500
|
|
return true
|
|
end if
|
|
|
|
if not IsArray(result.feeds) or result.feeds.Count() = 0
|
|
' Not an error, just no subscriptions
|
|
response.Json({
|
|
"videos": []
|
|
})
|
|
return true
|
|
end if
|
|
|
|
responseBody = {
|
|
"videos": ValidArray(result.feeds[0].items)
|
|
}
|
|
|
|
if result.feeds[0].DoesExist("continuation")
|
|
responseBody.continuation = result.feeds[0].continuation
|
|
end if
|
|
|
|
response.Json(responseBody)
|
|
|
|
if result.DoesExist("cache-control")
|
|
response.headers["cache-control"] = result["cache-control"]
|
|
end if
|
|
|
|
return true
|
|
end function
|
|
|
|
@get("/playlet-invidious-backend/api/v1/auth/subscriptions")
|
|
function GetAuthSubscriptions(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
|
|
accessToken = request.headers["authorization"]
|
|
if StringUtils.IsNullOrEmpty(accessToken) or not accessToken.StartsWith("Bearer ")
|
|
response.Json({
|
|
"error": "Missing or invalid 'Authorization' header"
|
|
})
|
|
response.http_code = 403
|
|
return true
|
|
end if
|
|
|
|
response.Json([])
|
|
return true
|
|
end function
|
|
|
|
@get("/playlet-invidious-backend/api/v1/auth/playlists")
|
|
function GetAuthPlaylists(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
|
|
accessToken = request.headers["authorization"]
|
|
if StringUtils.IsNullOrEmpty(accessToken) or not accessToken.StartsWith("Bearer ")
|
|
response.Json({
|
|
"error": "Missing or invalid 'Authorization' header"
|
|
})
|
|
response.http_code = 403
|
|
return true
|
|
end if
|
|
|
|
options = {
|
|
"accessToken": accessToken.Mid("Bearer ".Len())
|
|
}
|
|
continuation = request.query.continuation
|
|
if not StringUtils.IsNullOrEmpty(continuation)
|
|
options.continuation = continuation
|
|
end if
|
|
|
|
result = InnertubeService.GetPlaylists(options)
|
|
if result.error <> invalid
|
|
response.Json(result)
|
|
response.http_code = 500
|
|
return true
|
|
end if
|
|
|
|
if not IsArray(result.feeds) or result.feeds.Count() = 0
|
|
response.Json({
|
|
"error": "Failed to get auth playlists"
|
|
})
|
|
response.http_code = 500
|
|
return true
|
|
end if
|
|
|
|
response.Json(result.feeds[0])
|
|
|
|
if result.DoesExist("cache-control")
|
|
response.headers["cache-control"] = result["cache-control"]
|
|
end if
|
|
|
|
return true
|
|
end function
|
|
|
|
@get("/playlet-invidious-backend/api/v1/auth/history")
|
|
function GetAuthHistory(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
|
|
accessToken = request.headers["authorization"]
|
|
if StringUtils.IsNullOrEmpty(accessToken) or not accessToken.StartsWith("Bearer ")
|
|
response.Json({
|
|
"error": "Missing or invalid 'Authorization' header"
|
|
})
|
|
response.http_code = 403
|
|
return true
|
|
end if
|
|
|
|
options = {
|
|
"accessToken": accessToken.Mid("Bearer ".Len())
|
|
}
|
|
|
|
continuation = request.query.continuation
|
|
hasContinuation = not StringUtils.IsNullOrEmpty(continuation)
|
|
if hasContinuation
|
|
options.continuation = continuation
|
|
end if
|
|
|
|
if not hasContinuation
|
|
page = ValidString(request.query.page).ToInt()
|
|
if page > 1
|
|
LogError("Playlet backend does not use history pagination (use continuation instead)")
|
|
response.Json({
|
|
"items": []
|
|
})
|
|
return true
|
|
end if
|
|
end if
|
|
|
|
result = InnertubeService.GetWatchHistory(options)
|
|
if result.error <> invalid
|
|
response.Json(result)
|
|
response.http_code = 500
|
|
return true
|
|
end if
|
|
|
|
if not IsArray(result.feeds) or result.feeds.Count() = 0
|
|
' Not an error, just no history
|
|
response.Json({
|
|
"items": []
|
|
})
|
|
return true
|
|
end if
|
|
|
|
response.Json(result.feeds[0])
|
|
|
|
if result.DoesExist("cache-control")
|
|
response.headers["cache-control"] = result["cache-control"]
|
|
end if
|
|
|
|
return true
|
|
end function
|
|
|
|
@get("/playlet-invidious-backend/api/v1/stats")
|
|
function GetStats(context as object) as boolean
|
|
response = context.response
|
|
|
|
response.Json({
|
|
"error": "Playlet built-in Invidious backend is not a real Invidious instance, and does not support stats."
|
|
})
|
|
response.http_code = 400
|
|
return true
|
|
end function
|
|
|
|
@get("/playlet-invidious-backend/api/v1/trending")
|
|
@get("/playlet-invidious-backend/api/v1/popular")
|
|
function GetTrending(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
|
|
isRecommended = request.query.recommended = "true"
|
|
if isRecommended
|
|
return m.GetAuthRecommended(context)
|
|
end if
|
|
|
|
raw = request.query.raw <> invalid
|
|
|
|
options = {
|
|
raw: raw
|
|
type: request.query.type
|
|
}
|
|
|
|
result = InnertubeService.GetTrending(options)
|
|
if raw
|
|
response.Json(result)
|
|
return true
|
|
end if
|
|
|
|
if result.error <> invalid
|
|
response.Json(result)
|
|
response.http_code = 500
|
|
return true
|
|
end if
|
|
|
|
if not IsArray(result.feeds) or result.feeds.Count() = 0
|
|
response.Json({
|
|
"error": "Failed to get trending feed"
|
|
})
|
|
response.http_code = 500
|
|
return true
|
|
end if
|
|
|
|
response.Json(ValidArray(result.feeds[0].items))
|
|
|
|
if result.DoesExist("cache-control")
|
|
response.headers["cache-control"] = result["cache-control"]
|
|
end if
|
|
|
|
return true
|
|
end function
|
|
|
|
function GetAuthRecommended(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
|
|
accessToken = request.headers["authorization"]
|
|
if StringUtils.IsNullOrEmpty(accessToken) or not accessToken.StartsWith("Bearer ")
|
|
response.Json({
|
|
"error": "Missing or invalid 'Authorization' header"
|
|
})
|
|
response.http_code = 403
|
|
return true
|
|
end if
|
|
|
|
options = {
|
|
"accessToken": accessToken.Mid("Bearer ".Len())
|
|
}
|
|
|
|
continuation = request.query.continuation
|
|
if not StringUtils.IsNullOrEmpty(continuation)
|
|
options.continuation = continuation
|
|
end if
|
|
|
|
result = InnertubeService.GetHome(options)
|
|
if result.error <> invalid
|
|
response.Json(result)
|
|
response.http_code = 500
|
|
return true
|
|
end if
|
|
|
|
if not IsArray(result.feeds) or result.feeds.Count() = 0
|
|
' Not an error, just no recommended videos, usually when there's no history
|
|
response.Json({
|
|
"items": []
|
|
})
|
|
return true
|
|
end if
|
|
|
|
response.Json(result.feeds[0])
|
|
|
|
if result.DoesExist("cache-control")
|
|
response.headers["cache-control"] = result["cache-control"]
|
|
end if
|
|
|
|
return true
|
|
end function
|
|
|
|
@get("/playlet-invidious-backend/api/v1/search/suggestions")
|
|
function GetSearchSuggestions(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
|
|
query = request.query.q
|
|
|
|
result = InnertubeService.SearchSuggestions(query)
|
|
if result.error <> invalid
|
|
response.Json(result)
|
|
response.http_code = 500
|
|
return true
|
|
end if
|
|
|
|
result.query = query
|
|
|
|
if result.DoesExist("cache-control")
|
|
response.headers["cache-control"] = result["cache-control"]
|
|
end if
|
|
|
|
response.Json(result)
|
|
return true
|
|
end function
|
|
|
|
@get("/playlet-invidious-backend/api/v1/search")
|
|
function GetSearch(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
|
|
query = request.query.q
|
|
continuation = request.query.continuation
|
|
raw = request.query.raw <> invalid
|
|
|
|
options = {
|
|
raw: raw
|
|
}
|
|
|
|
searchFilters = {}
|
|
sort = request.query.sort
|
|
if not StringUtils.IsNullOrEmpty(sort)
|
|
searchFilters.sort = sort
|
|
end if
|
|
date = request.query.date
|
|
if not StringUtils.IsNullOrEmpty(date)
|
|
searchFilters.date = date
|
|
end if
|
|
_type = request.query.type
|
|
if not StringUtils.IsNullOrEmpty(_type)
|
|
searchFilters.type = _type
|
|
end if
|
|
duration = request.query.duration
|
|
if not StringUtils.IsNullOrEmpty(duration)
|
|
searchFilters.duration = duration
|
|
end if
|
|
features = request.query.features
|
|
if not StringUtils.IsNullOrEmpty(features)
|
|
searchFilters.features = features.Split(",")
|
|
end if
|
|
|
|
if searchFilters.Count() > 0
|
|
options.searchFilters = searchFilters
|
|
end if
|
|
if not StringUtils.IsNullOrEmpty(continuation)
|
|
options.continuation = continuation
|
|
end if
|
|
|
|
result = InnertubeService.Search(query, options)
|
|
|
|
if raw
|
|
response.Json(result)
|
|
return true
|
|
end if
|
|
|
|
if result.error <> invalid
|
|
response.Json(result)
|
|
response.http_code = 500
|
|
return true
|
|
end if
|
|
|
|
if not IsArray(result.feeds) or result.feeds.Count() = 0
|
|
response.Json({
|
|
"error": "Failed to search: no results found"
|
|
})
|
|
response.http_code = 500
|
|
return true
|
|
end if
|
|
|
|
response.Json(result.feeds[0])
|
|
if result.DoesExist("cache-control")
|
|
response.headers["cache-control"] = result["cache-control"]
|
|
end if
|
|
|
|
return true
|
|
end function
|
|
|
|
@get("/playlet-invidious-backend/api/v1/resolveurl")
|
|
function ResolveUrl(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
|
|
url = request.query.url
|
|
raw = request.query.raw <> invalid
|
|
|
|
if StringUtils.IsNullOrEmpty(url)
|
|
response.Json({
|
|
"error": "Missing 'url' query parameter"
|
|
})
|
|
response.http_code = 400
|
|
return true
|
|
end if
|
|
|
|
json = InnertubeService.ResolveUrl(url)
|
|
if json = invalid
|
|
response.Json({
|
|
"error": "Failed to resolve URL"
|
|
})
|
|
response.http_code = 500
|
|
return true
|
|
end if
|
|
|
|
if raw
|
|
response.Json(json)
|
|
return true
|
|
end if
|
|
|
|
endpoint = json["endpoint"]
|
|
pageType = ValidString(ObjectUtils.Dig(endpoint, ["commandMetadata", "webCommandMetadata", "webPageType"]))
|
|
if pageType = "WEB_PAGE_TYPE_UNKNOWN" or pageType = ""
|
|
response.Json({
|
|
"error": "Unknown page type"
|
|
})
|
|
response.http_code = 400
|
|
return true
|
|
end if
|
|
|
|
subEndpoint = endpoint["watchEndpoint"]
|
|
if subEndpoint = invalid
|
|
subEndpoint = endpoint["browseEndpoint"]
|
|
if subEndpoint = invalid
|
|
subEndpoint = endpoint
|
|
end if
|
|
end if
|
|
|
|
params = ObjectUtils.Dig(subEndpoint, ["params"])
|
|
|
|
result = {
|
|
"pageType": pageType
|
|
}
|
|
|
|
browseId = ObjectUtils.Dig(subEndpoint, ["browseId"])
|
|
if not StringUtils.IsNullOrEmpty(browseId)
|
|
result["ucid"] = browseId
|
|
end if
|
|
|
|
videoId = ObjectUtils.Dig(subEndpoint, ["videoId"])
|
|
if not StringUtils.IsNullOrEmpty(videoId)
|
|
result["videoId"] = videoId
|
|
end if
|
|
|
|
playlistId = ObjectUtils.Dig(subEndpoint, ["playlistId"])
|
|
if not StringUtils.IsNullOrEmpty(playlistId)
|
|
result["playlistId"] = playlistId
|
|
end if
|
|
|
|
startTimeSeconds = ObjectUtils.Dig(subEndpoint, ["startTimeSeconds"])
|
|
if ValidInt(startTimeSeconds) > 0
|
|
result["startTimeSeconds"] = startTimeSeconds
|
|
end if
|
|
|
|
if IsString(params)
|
|
result["params"] = params
|
|
end if
|
|
|
|
response.Json(result)
|
|
return true
|
|
end function
|
|
|
|
@get("*")
|
|
function GetAll(context as object) as boolean
|
|
request = context.request
|
|
route = request.route
|
|
|
|
if route.StartsWith("/playlet-invidious-backend/api/v1/videos/")
|
|
return m.GetVideo(context)
|
|
else if route.StartsWith("/playlet-invidious-backend/api/v1/playlists/")
|
|
return m.GetPlaylist(context)
|
|
else if route.StartsWith("/playlet-invidious-backend/api/v1/auth/playlists/")
|
|
return m.GetAuthPlaylist(context)
|
|
else if route.StartsWith("/playlet-invidious-backend/api/v1/channels/")
|
|
return m.GetChannel(context)
|
|
else if route.StartsWith("/playlet-invidious-backend/channel")
|
|
return m.RedirectChannel(context)
|
|
end if
|
|
|
|
return false
|
|
end function
|
|
|
|
@post("*")
|
|
function PostAll(context as object) as boolean
|
|
request = context.request
|
|
route = request.route
|
|
|
|
if route.StartsWith("/playlet-invidious-backend/api/v1/auth/subscriptions/")
|
|
return m.SubscribeToChannel(context)
|
|
end if
|
|
|
|
return false
|
|
end function
|
|
|
|
function GetVideo(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
route = request.route
|
|
|
|
videoId = route.Mid("/playlet-invidious-backend/api/v1/videos/".Len())
|
|
if videoId.InStr("?") > 0
|
|
videoId = videoId.Left(videoId.InStr("?"))
|
|
end if
|
|
raw = request.query.raw <> invalid
|
|
|
|
options = {
|
|
"raw": raw
|
|
"visitorData": m.innertube.visitorData
|
|
}
|
|
|
|
metadata = InnertubeService.GetVideoMetadata(videoId, options)
|
|
response.Json(metadata)
|
|
|
|
if metadata.error <> invalid
|
|
response.http_code = 500
|
|
end if
|
|
|
|
return true
|
|
end function
|
|
|
|
function GetPlaylist(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
route = request.route
|
|
|
|
playlistId = context.playlistId
|
|
if StringUtils.IsNullOrEmpty(playlistId)
|
|
playlistId = route.Mid("/playlet-invidious-backend/api/v1/playlists/".Len())
|
|
if playlistId.InStr("?") > 0
|
|
playlistId = playlistId.Left(playlistId.InStr("?"))
|
|
end if
|
|
end if
|
|
continuation = request.query.continuation
|
|
raw = request.query.raw <> invalid
|
|
|
|
options = {
|
|
raw: raw
|
|
}
|
|
|
|
accessToken = request.headers["authorization"]
|
|
if not StringUtils.IsNullOrEmpty(accessToken) and accessToken.StartsWith("Bearer ")
|
|
options["accessToken"] = accessToken.Mid("Bearer ".Len())
|
|
end if
|
|
|
|
if playlistId.StartsWith("RD")
|
|
if not StringUtils.IsNullOrEmpty(continuation)
|
|
options.videoId = continuation
|
|
else
|
|
videoId = request.query.videoId
|
|
if not StringUtils.IsNullOrEmpty(videoId)
|
|
options.videoId = videoId
|
|
end if
|
|
end if
|
|
|
|
playlist = InnertubeService.GetMix(playlistId, options)
|
|
else
|
|
if not StringUtils.IsNullOrEmpty(continuation)
|
|
options.continuation = continuation
|
|
end if
|
|
|
|
playlist = InnertubeService.GetPlaylist(playlistId, options)
|
|
end if
|
|
|
|
if raw
|
|
response.Json(playlist)
|
|
return true
|
|
end if
|
|
|
|
if playlist.error <> invalid
|
|
response.Json(playlist)
|
|
response.http_code = 500
|
|
return true
|
|
end if
|
|
|
|
index = request.query.index
|
|
if not StringUtils.IsNullOrEmpty(index)
|
|
index = index.ToInt()
|
|
if index > 1
|
|
LogError("Playlet backend does not use playlist pagination (use continuation instead)")
|
|
playlist.videos = []
|
|
end if
|
|
end if
|
|
|
|
response.Json(playlist)
|
|
return true
|
|
end function
|
|
|
|
function GetAuthPlaylist(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
route = request.route
|
|
|
|
accessToken = request.headers["authorization"]
|
|
if StringUtils.IsNullOrEmpty(accessToken) or not accessToken.StartsWith("Bearer ")
|
|
response.Json({
|
|
"error": "Missing or invalid 'Authorization' header"
|
|
})
|
|
response.http_code = 403
|
|
return true
|
|
end if
|
|
|
|
playlistId = route.Mid("/playlet-invidious-backend/api/v1/auth/playlists/".Len())
|
|
if playlistId.InStr("?") > 0
|
|
playlistId = playlistId.Left(playlistId.InStr("?"))
|
|
end if
|
|
|
|
context.playlistId = playlistId
|
|
return m.GetPlaylist(context)
|
|
end function
|
|
|
|
function GetChannel(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
route = request.route
|
|
|
|
channelAndTab = route.Mid("/playlet-invidious-backend/api/v1/channels/".Len())
|
|
if channelAndTab.InStr("?") > 0
|
|
channelAndTab = channelAndTab.Left(channelAndTab.InStr("?"))
|
|
end if
|
|
channelAndTab = channelAndTab.Split("/")
|
|
|
|
continuation = request.query.continuation
|
|
raw = request.query.raw <> invalid
|
|
|
|
options = {
|
|
raw: raw
|
|
}
|
|
|
|
if not StringUtils.IsNullOrEmpty(continuation)
|
|
options.continuation = continuation
|
|
end if
|
|
|
|
if channelAndTab.Count() = 1
|
|
channelId = channelAndTab[0]
|
|
channel = InnertubeService.GetChannel(channelId, options)
|
|
if raw
|
|
response.Json(channel)
|
|
return true
|
|
end if
|
|
|
|
if channel.error <> invalid
|
|
response.Json(channel)
|
|
response.http_code = 500
|
|
return true
|
|
end if
|
|
|
|
response.Json(channel)
|
|
if channel.DoesExist("cache-control")
|
|
response.headers["cache-control"] = channel["cache-control"]
|
|
end if
|
|
|
|
return true
|
|
else if channelAndTab.Count() = 2
|
|
channelId = channelAndTab[0]
|
|
_tab = channelAndTab[1]
|
|
|
|
params = request.query.params
|
|
if not StringUtils.IsNullOrEmpty(params)
|
|
options.params = params
|
|
end if
|
|
|
|
channel = InnertubeService.GetChannel(channelId, options)
|
|
if raw
|
|
response.Json(channel)
|
|
return true
|
|
end if
|
|
|
|
if channel.error <> invalid
|
|
response.Json(channel)
|
|
response.http_code = 500
|
|
return true
|
|
end if
|
|
|
|
if _tab = "playlists" or _tab = "podcasts" or _tab = "releases" or _tab = "courses"
|
|
result = {
|
|
"playlists": channel.items
|
|
}
|
|
if channel.DoesExist("continuation")
|
|
result.continuation = channel.continuation
|
|
end if
|
|
response.Json(result)
|
|
if channel.DoesExist("cache-control")
|
|
response.headers["cache-control"] = channel["cache-control"]
|
|
end if
|
|
else
|
|
result = {
|
|
"videos": channel.items
|
|
}
|
|
if channel.DoesExist("continuation")
|
|
result.continuation = channel.continuation
|
|
end if
|
|
response.Json(result)
|
|
if channel.DoesExist("cache-control")
|
|
response.headers["cache-control"] = channel["cache-control"]
|
|
end if
|
|
end if
|
|
|
|
return true
|
|
end if
|
|
|
|
return false
|
|
end function
|
|
|
|
function SubscribeToChannel(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
route = request.route
|
|
|
|
accessToken = request.headers["authorization"]
|
|
if StringUtils.IsNullOrEmpty(accessToken) or not accessToken.StartsWith("Bearer ")
|
|
response.Json({
|
|
"error": "Missing or invalid 'Authorization' header"
|
|
})
|
|
response.http_code = 403
|
|
return true
|
|
end if
|
|
|
|
channelId = route.Mid("/playlet-invidious-backend/api/v1/auth/subscriptions/".Len())
|
|
if channelId.InStr("?") > 0
|
|
channelId = channelId.Left(channelId.InStr("?"))
|
|
end if
|
|
|
|
if StringUtils.IsNullOrEmpty(channelId)
|
|
response.Json({
|
|
"error": "Missing 'channelId' path parameter"
|
|
})
|
|
response.http_code = 400
|
|
return true
|
|
end if
|
|
|
|
options = {
|
|
"accessToken": accessToken.Mid("Bearer ".Len())
|
|
}
|
|
|
|
result = InnertubeService.SubscribeToChannel(channelId, options)
|
|
|
|
if result.success <> true
|
|
response.Json(result)
|
|
response.http_code = 500
|
|
return true
|
|
end if
|
|
|
|
response.Default(204, "OK")
|
|
return true
|
|
end function
|
|
|
|
@delete("*")
|
|
function DeleteAll(context as object) as boolean
|
|
request = context.request
|
|
route = request.route
|
|
|
|
if route.StartsWith("/playlet-invidious-backend/api/v1/auth/subscriptions/")
|
|
return m.UnsubscribeFromChannel(context)
|
|
end if
|
|
|
|
return false
|
|
end function
|
|
|
|
function UnsubscribeFromChannel(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
route = request.route
|
|
|
|
accessToken = request.headers["authorization"]
|
|
if StringUtils.IsNullOrEmpty(accessToken) or not accessToken.StartsWith("Bearer ")
|
|
response.Json({
|
|
"error": "Missing or invalid 'Authorization' header"
|
|
})
|
|
response.http_code = 403
|
|
return true
|
|
end if
|
|
|
|
channelId = route.Mid("/playlet-invidious-backend/api/v1/auth/subscriptions/".Len())
|
|
if channelId.InStr("?") > 0
|
|
channelId = channelId.Left(channelId.InStr("?"))
|
|
end if
|
|
|
|
if StringUtils.IsNullOrEmpty(channelId)
|
|
response.Json({
|
|
"error": "Missing 'channelId' path parameter"
|
|
})
|
|
response.http_code = 400
|
|
return true
|
|
end if
|
|
|
|
options = {
|
|
"accessToken": accessToken.Mid("Bearer ".Len())
|
|
}
|
|
|
|
result = InnertubeService.UnsubscribeFromChannel(channelId, options)
|
|
|
|
if result.success <> true
|
|
response.Json(result)
|
|
response.http_code = 500
|
|
return true
|
|
end if
|
|
|
|
response.Default(204, "OK")
|
|
return true
|
|
end function
|
|
|
|
@get("/playlet-invidious-backend/watch")
|
|
function RedirectWatch(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
|
|
videoId = request.query.v
|
|
t = request.query.t
|
|
|
|
if StringUtils.IsNullOrEmpty(videoId)
|
|
response.Json({
|
|
"error": "Missing 'v' query parameter"
|
|
})
|
|
response.http_code = 400
|
|
return true
|
|
end if
|
|
|
|
url = `https://redirect.invidious.io/watch?v=${videoId}`
|
|
if not StringUtils.IsNullOrEmpty(t)
|
|
url = `${url}&t=${t}`
|
|
end if
|
|
response.Redirect(url)
|
|
return true
|
|
end function
|
|
|
|
@get("/playlet-invidious-backend/playlist")
|
|
function RedirectPlaylist(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
|
|
playlistId = request.query.list
|
|
if StringUtils.IsNullOrEmpty(playlistId)
|
|
response.Json({
|
|
"error": "Missing 'list' query parameter"
|
|
})
|
|
response.http_code = 400
|
|
return true
|
|
end if
|
|
|
|
response.Redirect(`https://redirect.invidious.io/playlist?list=${playlistId}`)
|
|
return true
|
|
end function
|
|
|
|
function RedirectChannel(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
route = request.route
|
|
|
|
channelId = route.Mid("/playlet-invidious-backend/channel/".Len())
|
|
if StringUtils.IsNullOrEmpty(channelId)
|
|
response.Json({
|
|
"error": "Missing 'channelId' path parameter"
|
|
})
|
|
response.http_code = 400
|
|
return true
|
|
end if
|
|
|
|
response.Redirect(`https://redirect.invidious.io/channel/${channelId}`)
|
|
return true
|
|
end function
|
|
|
|
end class
|
|
|
|
end namespace
|