-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
181 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletInvidiousBackendRouter.bs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters