import "pkg:/components/Services/Innertube/BrowseEndpoint.bs" import "pkg:/components/Services/Innertube/Constants.bs" import "pkg:/components/Services/Innertube/Context.bs" import "pkg:/components/Services/Innertube/NextEndpoint.bs" import "pkg:/components/Services/Innertube/OAuth.bs" import "pkg:/components/Services/Innertube/PlayerEndpoint.bs" import "pkg:/components/Services/Innertube/Search.bs" import "pkg:/components/Services/Innertube/SearchFilters.bs" import "pkg:/source/services/HttpClient.bs" import "pkg:/source/utils/CancellationUtils.bs" import "pkg:/source/utils/ObjectUtils.bs" import "pkg:/source/utils/TimeUtils.bs" namespace InnertubeService function GetVideoMetadata(videoId as string, options = invalid as object) as object cancellation = invalid fetchNext = true ctt = "" if options <> invalid if options.DoesExist("cancellation") cancellation = options.cancellation end if if options.DoesExist("fetchNext") fetchNext = options.fetchNext end if if options.DoesExist("ctt") ctt = options.ctt end if end if playerRequest = Innertube.CreatePlayerRequest(videoId, ctt) playerRequest.Cancellation(cancellation) playerRequest.Send() nextRequest = invalid nextResponse = invalid if fetchNext nextRequest = Innertube.CreateNextRequest(videoId, ctt) nextRequest.Cancellation(cancellation) nextRequest.Send() end if playerResponse = playerRequest.Await() if not playerResponse.IsSuccess() return playerResponse end if if CancellationUtils.IsCancelled(cancellation) return invalid end if if fetchNext nextResponse = nextRequest.Await() end if parsedResponse = Innertube.ParseInnertubePlayerResponse(playerResponse.Json()) if fetchNext parsedResponse = Innertube.ParseInnertubeNextResponse(nextResponse.Json(), parsedResponse) end if if parsedResponse <> invalid if parsedResponse.Success playerResponse.OverrideJson(parsedResponse.Metadata) else playerResponse.OverrideStatusCode(500) playerResponse.OverrideErrorMessage(parsedResponse.Error) end if end if return playerResponse end function function SearchSuggestions(q as string, cancellation = invalid as object) as object suggestions = Innertube.SearchSuggestions(q, cancellation) if suggestions = invalid suggestions = [] end if return { suggestions: suggestions } end function function Search(query as string, options = invalid as object) as object searchFilters = invalid continuation = "" cancellation = invalid if options <> invalid if options.DoesExist("searchFilters") searchFilters = options.searchFilters end if if options.DoesExist("continuation") continuation = options.continuation end if if options.DoesExist("cancellation") cancellation = options.cancellation end if end if return Innertube.Search(query, searchFilters, continuation, cancellation) end function function GetTrending(options = invalid as object) as object cancellation = invalid params = "" if options <> invalid if options.DoesExist("cancellation") cancellation = options.cancellation end if if options.DoesExist("type") trendingType = LCase(ValidString(options.type)) if trendingType = "music" params = "4gINGgt5dG1hX2NoYXJ0cw%3D%3D" else if trendingType = "gaming" params = "4gIcGhpnYW1pbmdfY29ycHVzX21vc3RfcG9wdWxhcg%3D%3D" else if trendingType = "movies" params = "4gIKGgh0cmFpbGVycw%3D%3D" end if end if end if request = Innertube.CreateBrowseRequest("FEtrending", Innertube.ClientType.WEB, params) request.Cancellation(cancellation) response = request.Await() if not response.IsSuccess() return invalid end if json = response.Json() return Innertube.ParseResponse(json) end function function GetHome(options = invalid as object) as object return GetBrowseAuthenticatedPage("FEwhat_to_watch", options) end function function GetSubscriptions(options = invalid as object) as object return GetBrowseAuthenticatedPage("FEsubscriptions", options) end function function GetWatchHistory(options = invalid as object) as object return GetBrowseAuthenticatedPage("FEhistory", options) end function function GetPlaylists(options = invalid as object) as object return GetBrowseAuthenticatedPage("FEplaylist_aggregation", options) end function function GetBrowseAuthenticatedPage(browserId as string, options = invalid as object) as object cancellation = invalid access_token = invalid if options <> invalid if options.DoesExist("cancellation") cancellation = options.cancellation end if if options.DoesExist("access_token") access_token = options.access_token end if end if request = Innertube.CreateBrowseRequest(browserId, Innertube.ClientType.Tv) if access_token <> invalid request.Header("Authorization", "Bearer " + access_token) end if request.Cancellation(cancellation) response = request.Await() if not response.IsSuccess() return invalid end if json = response.Json() return Innertube.ParseResponse(json) end function function GetChannel(channelId as string, options = invalid as object) as object continuation = "" params = "" cancellation = invalid if options <> invalid if options.DoesExist("continuation") continuation = options.continuation end if if options.DoesExist("params") params = options.params end if if options.DoesExist("cancellation") cancellation = options.cancellation end if end if request = Innertube.CreateBrowseRequest(channelId, Innertube.ClientType.Web, params, continuation) request.Cancellation(cancellation) response = request.Await() if not response.IsSuccess() return invalid end if json = response.Json() return Innertube.ParseChannelPageResponse(json) end function function GetPlaylist(playlistId as string, options = invalid as object) as object continuation = "" cancellation = invalid if options <> invalid if options.DoesExist("continuation") continuation = options.continuation end if if options.DoesExist("cancellation") cancellation = options.cancellation end if end if if not playlistId.StartsWith("VL") playlistId = "VL" + playlistId end if request = Innertube.CreateBrowseRequest(playlistId, Innertube.ClientType.Web, "", continuation) request.Cancellation(cancellation) response = request.Await() if not response.IsSuccess() return invalid end if json = response.Json() return Innertube.ParsePlaylistPageResponse(json) end function function AuthGetCode(cancellation = invalid as object) as object result = Innertube.GetClientIdentity(cancellation) if CancellationUtils.IsCancelled(cancellation) return invalid end if result2 = Innertube.GetDeviceAndUserCode(result.client_id, cancellation) if CancellationUtils.IsCancelled(cancellation) return invalid end if result.Append(result2) return result end function function AuthGetActivationUrl(code as object) as string return `https://www.youtube.com/tv/activate?user_code=${code.user_code}` end function function AuthPollForAccessToken(code as object, cancellation = invalid as object) as object return Innertube.PollForAccessToken(code, code, cancellation) end function function AuthRefreshAccessTokenIfNeeded(accessToken as object, cancellation = invalid as object) as boolean if not IsAssociativeArray(accessToken) return false end if nowSeconds = TimeUtils.Now().AsSeconds() expiresTimestamp = accessToken["expires_timestamp"] if expiresTimestamp = invalid or expiresTimestamp < nowSeconds refreshed = Innertube.RefreshAccessToken(accessToken, cancellation) if IsAssociativeArray(refreshed) and refreshed.DoesExist("access_token") accessToken.Append(refreshed) if accessToken.DoesExist("expires_in") accessToken["expires_timestamp"] = TimeUtils.Now().AsSeconds() + accessToken["expires_in"] end if return true end if end if return false end function function AuthRevokeAccessToken(accessToken as object, cancellation = invalid as object) as boolean if not IsAssociativeArray(accessToken) return false end if return Innertube.RevokeAccessToken(accessToken["access_token"], cancellation) end function end namespace