Skip to content

Commit

Permalink
WIP: built-in instance
Browse files Browse the repository at this point in the history
  • Loading branch information
iBicha committed Dec 21, 2024
1 parent e6b44cd commit 565bb77
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace InnertubeService
cancellation = options.cancellation
end if
if options.DoesExist("type")
trendingType = options.type
trendingType = LCase(ValidString(options.type))

if trendingType = "music"
params = "4gINGgt5dG1hX2NoYXJ0cw%3D%3D"
Expand Down
4 changes: 2 additions & 2 deletions playlet-lib/src/components/Services/Innertube/NodesParser.bs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ namespace Innertube

channel = {
"type": "channel"
"channelId": channelId
"title": ParseText(nodeData["title"])
"authorId": channelId
"author": ParseText(nodeData["title"])
"authorThumbnails": ObjectUtils.Dig(nodeData, ["thumbnail", "thumbnails"])
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import "pkg:/source/utils/TimeUtils.bs"
namespace Invidious
const INSTANCES_API = "https://api.invidious.io/instances.json?sort_by=type,users"

const DEFAULT_INSTANCE = "https://inv.nadeko.net"
const DEFAULT_INSTANCE = "http://127.0.0.1:8888/playlet-invidious-backend"

const TOKEN_CALLBACK = "/invidious/token_callback"

Expand Down Expand Up @@ -71,7 +71,13 @@ namespace Invidious
return instance
end if

return DEFAULT_INSTANCE
instance = DEFAULT_INSTANCE
if instance.StartsWith("http://127.0.0.1:8888")
address = m.invidiousNode.webServer@.GetServerAddress()
instance = instance.Replace("http://127.0.0.1:8888", address)
end if

return instance
end function

function MarkVideoWatched(videoId as dynamic) as void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import "pkg:/components/Services/Innertube/InnertubeService.bs"

namespace Http

class PlayletInvidiousBackendRouter extends HttpRouter

function new()
super()
end function

@get("/playlet-invidious-backend/authorize_token")
function AuthorizeToken(context as object) as boolean
response = context.response

error = "Playlet built-in Invidious backend is not a real Invidious instance, and does not support accounts."
response.Default(400, error)
return true
end function

@get("/playlet-invidious-backend/api/v1/stats")
function GetStats(context as object) as boolean
response = context.response

error = "Playlet built-in Invidious backend is not a real Invidious instance, and does not support stats."
response.Default(400, error)
return true
end function

@get("/playlet-invidious-backend/api/v1/trending")
function GetTrending(context as object) as boolean
request = context.request
response = context.response

feeds = InnertubeService.GetTrending({ type: request.query.type })
if not IsArray(feeds) or feeds.Count() = 0
response.Default(500, "Failed to get trending feed")
return true
end if

response.Json(ValidArray(feeds[0].items))
return true
end function

@get("/playlet-invidious-backend/api/v1/popular")
function GetPopular(context as object) as boolean
return m.GetTrending(context)
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

suggestions = InnertubeService.SearchSuggestions(query)
suggestions.query = query
response.Json(suggestions)
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

feeds = InnertubeService.Search(query)
if not IsArray(feeds) or feeds.Count() = 0
response.Default(500, "Failed to search")
return true
end if

response.Json(ValidArray(feeds[0].items))
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/channels/")
return m.GetChannel(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())

playerResponse = InnertubeService.GetVideoMetadata(videoId)
if not playerResponse.IsSuccess()
response.Default(playerResponse.StatusCode(), playerResponse.ErrorMessage())
return true
end if

response.Json(playerResponse.Json())
return true
end function

function GetPlaylist(context as object) as boolean
request = context.request
response = context.response
route = request.route

playlistId = route.Mid("/playlet-invidious-backend/api/v1/playlists/".Len())

playlist = InnertubeService.GetPlaylist(playlistId)
if playlist = invalid
response.Default(500, "Failed to get playlist")
return true
end if

response.Json(playlist)
return true
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())
channelAndTab = channelAndTab.Split("/")

if channelAndTab.Count() = 1
channelId = channelAndTab[0]
channel = InnertubeService.GetChannel(channelId)
if channel = invalid
response.Default(500, "Failed to get channel")
return true
end if

response.Json(channel)
return true
else if channelAndTab.Count() = 2
' channelId = channelAndTab[0]
_tab = channelAndTab[1]

if _tab = "playlists" or _tab = "podcasts" or _tab = "releases"
response.Json({
playlists: []
})
else
response.Json({
videos: []
})
end if

return true
end if

return false
end function
end class

end namespace
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "pkg:/components/Web/PlayletWebServer/Middleware/HlsRouter.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/HomeLayoutRouter.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/HomeRouter.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/InvidiousRouter.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/PlayletInvidiousBackendRouter.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"
Expand Down Expand Up @@ -57,6 +58,7 @@ function SetupRoutes(server as object)
server.UseRouter(new Http.CacheRouter())
server.UseRouter(new Http.ProxyRouter(server))
server.UseRouter(new Http.RegistryRouter(server))
server.UseRouter(new Http.PlayletInvidiousBackendRouter())

etags = new Http.EtagUtils()
server.UseRouter(new Http.HttpStaticFilesRouter("/", "libpkg:/www", etags, { staticFiles: true, staticCompressed: true }))
Expand Down

0 comments on commit 565bb77

Please sign in to comment.