420 lines
15 KiB
Plaintext
420 lines
15 KiB
Plaintext
import "pkg:/components/Services/Innertube/NodesParser.bs"
|
|
import "pkg:/source/utils/ObjectUtils.bs"
|
|
import "pkg:/source/utils/StringUtils.bs"
|
|
import "pkg:/source/utils/Types.bs"
|
|
|
|
namespace Innertube
|
|
|
|
function ParseResponseDataForFeeds(responseData as object, context as object) as object
|
|
if responseData = invalid
|
|
return []
|
|
end if
|
|
|
|
if not ValidBool(context.noContainers)
|
|
containers = GetContainers(responseData)
|
|
if containers.Count() = 0
|
|
return []
|
|
end if
|
|
else
|
|
containers = responseData
|
|
end if
|
|
|
|
context.Append({
|
|
directives: Innertube.GetParsingDirectives()
|
|
|
|
feeds: []
|
|
currentFeed: {
|
|
title: ""
|
|
items: []
|
|
}
|
|
generateVideoThumbnails: true
|
|
})
|
|
|
|
Innertube.ParseNode(containers, context)
|
|
|
|
if context.currentFeed <> invalid
|
|
hasTitle = context.currentFeed.title <> ""
|
|
hasItems = context.currentFeed.items.Count() > 0
|
|
hasContinuation = context.currentFeed.DoesExist("continuation")
|
|
if hasTitle or hasItems or hasContinuation
|
|
context.feeds.Push(context.currentFeed)
|
|
context.currentFeed = invalid
|
|
end if
|
|
end if
|
|
return context.feeds
|
|
end function
|
|
|
|
function ParseChannelPageResponseData(responseData as object, response as object) as object
|
|
if responseData = invalid
|
|
return {
|
|
error: "Invalid response data"
|
|
}
|
|
end if
|
|
|
|
error = ParseResponseDataForError(responseData)
|
|
if error <> ""
|
|
return {
|
|
error: error
|
|
}
|
|
end if
|
|
|
|
author = ObjectUtils.Dig(responseData, ["metadata", "channelMetadataRenderer", "title"])
|
|
if author = invalid
|
|
author = ObjectUtils.Dig(responseData, ["header", "pageHeaderRenderer", "pageTitle"])
|
|
end if
|
|
authorId = ObjectUtils.Dig(responseData, ["metadata", "channelMetadataRenderer", "externalId"])
|
|
|
|
thumbnails = ObjectUtils.Dig(responseData, ["metadata", "channelMetadataRenderer", "avatar", "thumbnails"])
|
|
if thumbnails = invalid
|
|
thumbnails = ObjectUtils.Dig(responseData, ["header", "pageHeaderRenderer", "content", "pageHeaderViewModel", "image", "decoratedAvatarViewModel", "avatar", "avatarViewModel", "image", "sources"])
|
|
if thumbnails = invalid
|
|
thumbnails = ObjectUtils.Dig(responseData, ["microformat", "microformatDataRenderer", "thumbnail", "thumbnails"])
|
|
if thumbnails = invalid
|
|
thumbnails = []
|
|
end if
|
|
end if
|
|
end if
|
|
|
|
banners = ObjectUtils.Dig(responseData, ["header", "pageHeaderRenderer", "content", "pageHeaderViewModel", "banner", "imageBannerViewModel", "image", "sources"])
|
|
if banners = invalid
|
|
banners = []
|
|
end if
|
|
|
|
authorSubTitle = ""
|
|
contentMetadata = ObjectUtils.Dig(responseData, ["header", "pageHeaderRenderer", "content", "pageHeaderViewModel", "metadata", "contentMetadataViewModel"])
|
|
if contentMetadata <> invalid
|
|
metadataRows = contentMetadata["metadataRows"]
|
|
delimiter = ` ${ValidString(contentMetadata["delimiter"])} `
|
|
if metadataRows <> invalid
|
|
parts = []
|
|
for each row in metadataRows
|
|
metadataParts = row["metadataParts"]
|
|
if metadataParts = invalid
|
|
continue for
|
|
end if
|
|
for each part in metadataParts
|
|
text = ObjectUtils.Dig(part, ["text", "content"])
|
|
if IsString(text)
|
|
parts.Push(text)
|
|
end if
|
|
end for
|
|
end for
|
|
if parts.Count() > 0
|
|
authorSubTitle = parts.Join(delimiter)
|
|
end if
|
|
end if
|
|
end if
|
|
|
|
tabsNames = []
|
|
tabsParams = {}
|
|
tabsTitles = {}
|
|
|
|
tabs = ObjectUtils.Dig(responseData, ["contents", "twoColumnBrowseResultsRenderer", "tabs"])
|
|
if tabs <> invalid
|
|
for each t in tabs
|
|
tabRenderer = t["tabRenderer"]
|
|
if tabRenderer = invalid
|
|
continue for
|
|
end if
|
|
|
|
tabName = ObjectUtils.Dig(tabRenderer, ["endpoint", "commandMetadata", "webCommandMetadata", "url"])
|
|
if tabName = invalid
|
|
continue for
|
|
end if
|
|
tabName = tabName.Split("?")[0]
|
|
tabName = tabName.Split("/")
|
|
tabName = tabName[tabName.Count() - 1]
|
|
if StringUtils.IsNullOrEmpty(tabName)
|
|
continue for
|
|
end if
|
|
|
|
tabsNames.Push(tabName)
|
|
|
|
tabTitle = tabRenderer["title"]
|
|
if IsString(tabTitle)
|
|
tabsTitles[tabName] = tabTitle
|
|
end if
|
|
|
|
tabParams = ObjectUtils.Dig(tabRenderer, ["endpoint", "browseEndpoint", "params"])
|
|
if IsString(tabParams)
|
|
tabsParams[tabName] = tabParams
|
|
end if
|
|
end for
|
|
end if
|
|
|
|
items = []
|
|
continuation = invalid
|
|
sortOptions = invalid
|
|
feeds = ParseResponseDataForFeeds(responseData, {
|
|
response: response
|
|
"currentAuthor": author
|
|
"currentAuthorId": authorId
|
|
})
|
|
if feeds.Count() > 0
|
|
items = ValidArray(feeds[0].items)
|
|
if feeds[0].DoesExist("continuation")
|
|
continuation = feeds[0].continuation
|
|
end if
|
|
if feeds[0].DoesExist("sortOptions")
|
|
sortOptions = feeds[0].sortOptions
|
|
end if
|
|
end if
|
|
|
|
channel = {
|
|
"author": author
|
|
"authorId": authorId
|
|
"authorThumbnails": thumbnails
|
|
"authorBanners": banners
|
|
"authorSubTitle": authorSubTitle
|
|
"tabs": tabsNames
|
|
"tabsParams": tabsParams
|
|
"tabsTitles": tabsTitles
|
|
"items": items
|
|
}
|
|
|
|
' continuation and sort options are specific to certain tabs of the channel
|
|
if continuation <> invalid
|
|
channel["continuation"] = continuation
|
|
end if
|
|
if sortOptions <> invalid
|
|
channel["sortOptions"] = sortOptions
|
|
end if
|
|
|
|
return channel
|
|
end function
|
|
|
|
function ParsePlaylistPageResponseData(responseData as object, response as object) as object
|
|
if responseData = invalid
|
|
return {
|
|
error: "Invalid response data"
|
|
}
|
|
end if
|
|
|
|
error = ParseResponseDataForError(responseData)
|
|
if error <> ""
|
|
return {
|
|
error: error
|
|
}
|
|
end if
|
|
|
|
playlistId = invalid
|
|
title = invalid
|
|
author = invalid
|
|
authorId = invalid
|
|
description = invalid
|
|
videoCount = -1
|
|
thumbnail = invalid
|
|
statsText = invalid
|
|
|
|
playlistSidebarRendererItems = ObjectUtils.Dig(responseData, ["sidebar", "playlistSidebarRenderer", "items"])
|
|
if playlistSidebarRendererItems <> invalid
|
|
primaryInfo = ObjectUtils.Dig(playlistSidebarRendererItems, [0, "playlistSidebarPrimaryInfoRenderer"])
|
|
secondaryInfo = ObjectUtils.Dig(playlistSidebarRendererItems, [1, "playlistSidebarSecondaryInfoRenderer"])
|
|
|
|
playlistId = ObjectUtils.Dig(primaryInfo, ["navigationEndpoint", "watchEndpoint", "playlistId"])
|
|
title = ParseText(ObjectUtils.Dig(primaryInfo, ["title"]))
|
|
description = ParseText(ObjectUtils.Dig(primaryInfo, ["description"]))
|
|
videoCount = ParseText(ObjectUtils.Dig(primaryInfo, ["stats", 0, "runs", 0, "text"])).ToInt()
|
|
thumbnail = ObjectUtils.Dig(primaryInfo, ["thumbnailRenderer", "playlistVideoThumbnailRenderer", "thumbnail", "thumbnails", 0, "url"])
|
|
|
|
author = ParseText(ObjectUtils.Dig(secondaryInfo, ["videoOwner", "videoOwnerRenderer", "title"]))
|
|
authorId = ObjectUtils.Dig(secondaryInfo, ["videoOwner", "videoOwnerRenderer", "navigationEndpoint", "browseEndpoint", "browseId"])
|
|
stats = ObjectUtils.Dig(primaryInfo, ["stats"])
|
|
if IsArray(stats)
|
|
statsTexts = []
|
|
for each stat in stats
|
|
statText = ParseText(stat)
|
|
if statText <> ""
|
|
statsTexts.Push(statText)
|
|
end if
|
|
end for
|
|
statsText = statsTexts.Join(" • ")
|
|
end if
|
|
end if
|
|
|
|
twoColumnRenderer = ObjectUtils.Dig(responseData, ["contents", "tvBrowseRenderer", "content", "tvSurfaceContentRenderer", "content", "twoColumnRenderer"])
|
|
if twoColumnRenderer <> invalid
|
|
entityMetadataRenderer = ObjectUtils.Dig(twoColumnRenderer, ["leftColumn", "entityMetadataRenderer"])
|
|
if entityMetadataRenderer <> invalid
|
|
if title = invalid
|
|
title = ParseText(entityMetadataRenderer["title"])
|
|
end if
|
|
if statsText = invalid
|
|
statsText = ParseLineRenderer(ObjectUtils.Dig(entityMetadataRenderer, ["bylines", 0, "lineRenderer"]))
|
|
end if
|
|
if description = invalid
|
|
description = ParseText(entityMetadataRenderer["description"])
|
|
end if
|
|
if playlistId = invalid
|
|
playlistId = ObjectUtils.Dig(twoColumnRenderer, ["rightColumn", "playlistVideoListRenderer", "playlistId"])
|
|
end if
|
|
end if
|
|
end if
|
|
|
|
playlist = {
|
|
"videoCount": videoCount
|
|
"videos": []
|
|
}
|
|
if playlistId <> invalid
|
|
playlist["playlistId"] = playlistId
|
|
end if
|
|
if title <> invalid
|
|
playlist["title"] = title
|
|
end if
|
|
if author <> invalid
|
|
playlist["author"] = author
|
|
end if
|
|
if authorId <> invalid
|
|
playlist["authorId"] = authorId
|
|
end if
|
|
if description <> invalid
|
|
playlist["description"] = description
|
|
end if
|
|
if thumbnail <> invalid
|
|
playlist["playlistThumbnail"] = thumbnail
|
|
end if
|
|
if statsText <> invalid
|
|
playlist["statsText"] = statsText
|
|
end if
|
|
|
|
feeds = ParseResponseDataForFeeds(responseData, {
|
|
response: response
|
|
})
|
|
if feeds.Count() > 0
|
|
playlist["videos"] = ValidArray(feeds[0].items)
|
|
if feeds[0].DoesExist("continuation")
|
|
playlist["continuation"] = feeds[0].continuation
|
|
end if
|
|
end if
|
|
|
|
if playlist["playlistThumbnail"] = invalid and playlist["videos"].Count() > 0
|
|
playlist["playlistThumbnail"] = ObjectUtils.Dig(playlist, ["videos", 0, "thumbnails", 0, "url"])
|
|
end if
|
|
|
|
return playlist
|
|
end function
|
|
|
|
function ParseMixPageResponseData(responseData as object, response as object) as object
|
|
if responseData = invalid
|
|
return {
|
|
error: "Invalid response data"
|
|
}
|
|
end if
|
|
|
|
error = Innertube.ParseResponseDataForError(responseData)
|
|
if error <> ""
|
|
return {
|
|
error: error
|
|
}
|
|
end if
|
|
|
|
playlist = ObjectUtils.Dig(responseData, ["contents", "twoColumnWatchNextResults", "playlist", "playlist"])
|
|
if playlist = invalid
|
|
return {
|
|
error: "Invalid playlist data"
|
|
}
|
|
end if
|
|
|
|
playlistId = playlist["playlistId"]
|
|
|
|
title = ParseText(playlist["title"])
|
|
if title = ""
|
|
title = ParseText(playlist["titleText"])
|
|
end if
|
|
|
|
author = ParseText(playlist["ownerName"])
|
|
if author = ""
|
|
author = ParseText(playlist["shortBylineText"])
|
|
if author = ""
|
|
author = ParseText(playlist["longBylineText"])
|
|
end if
|
|
end if
|
|
|
|
result = {
|
|
"playlistId": playlistId
|
|
"title": title
|
|
"author": author
|
|
"videos": []
|
|
"videoCount": -1
|
|
"videoCountText": ""
|
|
}
|
|
|
|
feeds = ParseResponseDataForFeeds(playlist, {
|
|
response: response
|
|
})
|
|
if feeds.Count() > 0
|
|
result["videos"] = ValidArray(feeds[0].items)
|
|
end if
|
|
|
|
return result
|
|
end function
|
|
|
|
function ParseResponseDataForError(responseData as object) as string
|
|
error = ""
|
|
if responseData = invalid
|
|
return error
|
|
end if
|
|
|
|
alerts = responseData["alerts"]
|
|
if not IsArray(alerts) or alerts.Count() = 0
|
|
return error
|
|
end if
|
|
|
|
for each alert in alerts
|
|
if not IsAssociativeArray(alert)
|
|
continue for
|
|
end if
|
|
|
|
alertRenderer = alert["alertRenderer"]
|
|
if alertRenderer = invalid
|
|
alertRenderer = alert["alertWithButtonRenderer"]
|
|
end if
|
|
if not IsAssociativeArray(alertRenderer)
|
|
continue for
|
|
end if
|
|
|
|
alertText = ParseText(alertRenderer.text)
|
|
if ValidString(alertRenderer.type) = "INFO"
|
|
LogWarnNoTelemetry("Innertube Alert:", `[${alertRenderer.type}]`, alertText)
|
|
else
|
|
LogWarn("Innertube Alert:", `[${alertRenderer.type}]`, alertText)
|
|
end if
|
|
if alertRenderer.type = "ERROR"
|
|
error += alertText
|
|
end if
|
|
end for
|
|
|
|
return error
|
|
end function
|
|
|
|
function ParseResponseForCacheControl(response as object) as dynamic
|
|
if not response.IsSuccess()
|
|
return invalid
|
|
end if
|
|
|
|
headers = ValidAssociativeArray(response.Headers())
|
|
if headers.DoesExist("cache-control")
|
|
return headers["cache-control"]
|
|
end if
|
|
|
|
json = response.Json()
|
|
maxAgeSeconds = ObjectUtils.Dig(json, ["responseContext", "maxAgeSeconds"])
|
|
if IsInt(maxAgeSeconds)
|
|
return `max-age=${maxAgeSeconds}`
|
|
end if
|
|
|
|
return invalid
|
|
end function
|
|
|
|
function GetContainers(responseData as object) as object
|
|
return [
|
|
responseData["contents"]
|
|
responseData["response"]
|
|
responseData["onResponseReceivedActions"]
|
|
responseData["onResponseReceivedCommands"]
|
|
responseData["continuationContents"]
|
|
]
|
|
end function
|
|
|
|
end namespace
|