diff --git a/CHANGELOG.md b/CHANGELOG.md index cef4f8bc..c28bb717 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Channel sort options in Playlet backend (sort videos by Newest, Oldest, Popular) + ### Fixed - [Perf] reverted a perf fix that wasn't helping diff --git a/playlet-lib/src/components/ChannelView/ChannelView.bs b/playlet-lib/src/components/ChannelView/ChannelView.bs index c712880c..5583bc34 100644 --- a/playlet-lib/src/components/ChannelView/ChannelView.bs +++ b/playlet-lib/src/components/ChannelView/ChannelView.bs @@ -349,14 +349,17 @@ function GetSortingContextMenuOptionsForItem(video as object) as object end if feedSource = feedSources[0] - endpoint = feedSource.endpoint - if not m.sortOptions.DoesExist(endpoint) - return [] + sortOptions = feedSource.state.sortOptions + if not IsArray(sortOptions) + endpoint = feedSource.endpoint + if not m.sortOptions.DoesExist(endpoint) + return [] + end if + + sortOptions = m.sortOptions[endpoint] end if title = Tr(Locale.Dialogs.SortFeedBy).replace("%1", `"${feedSource.title}"`) - sortOptions = m.sortOptions[endpoint] - return [ContextMenuUtils.CreateOption(title + "...", m.top, "ShowSortDialog", [title, sortOptions, feedContentNode])] end function diff --git a/playlet-lib/src/components/Services/Innertube/NodesParser.bs b/playlet-lib/src/components/Services/Innertube/NodesParser.bs index 81635494..1834fecf 100644 --- a/playlet-lib/src/components/Services/Innertube/NodesParser.bs +++ b/playlet-lib/src/components/Services/Innertube/NodesParser.bs @@ -81,8 +81,10 @@ namespace Innertube "gridRenderer": ParseGridRenderer "richGridRenderer": ParseRichGridRenderer "richItemRenderer": ParseRichItemRenderer + "feedFilterChipBarRenderer": ParseFeedFilterChipBarRenderer "continuationItemRenderer": ParseContinuationItemRenderer "appendContinuationItemsAction": ParseAppendContinuationItemsAction + "reloadContinuationItemsCommand": ParseReloadContinuationItemsCommand "gridContinuation": ParseGridContinuation "tvSurfaceContentContinuation": ParseTvSurfaceContentContinuation ' Ignored nodes: @@ -144,7 +146,26 @@ namespace Innertube return nodeData["content"] end function - function ParseSectionListRenderer(nodeData as object, _context as object) as object + function ParseSectionListRenderer(nodeData as object, context as object) as object + sortSubMenuItems = ObjectUtils.Dig(nodeData, ["subMenu", "channelSubMenuRenderer", "sortSetting", "sortFilterSubMenuRenderer", "subMenuItems"]) + if IsArray(sortSubMenuItems) + sortOptions = [] + for each item in sortSubMenuItems + params = ValidString(ObjectUtils.Dig(item, ["navigationEndpoint", "browseEndpoint", "params"])) + title = ParseText(item["title"]) + isSelected = ValidBool(item["selected"]) + if params <> "" and title <> "" + sortOptions.Push({ + "title": title + "params": params + "isSelected": isSelected + }) + end if + end for + if sortOptions.Count() > 0 + context.currentFeed.sortOptions = sortOptions + end if + end if return nodeData["contents"] end function @@ -1198,7 +1219,11 @@ namespace Innertube return nodeData["items"] end function - function ParseRichGridRenderer(nodeData as object, _context as object) as object + function ParseRichGridRenderer(nodeData as object, context as object) as object + feedFilterChipBarRenderer = ObjectUtils.Dig(nodeData, ["header", "feedFilterChipBarRenderer"]) + if feedFilterChipBarRenderer <> invalid + ParseFeedFilterChipBarRenderer(feedFilterChipBarRenderer, context) + end if return nodeData["contents"] end function @@ -1206,6 +1231,47 @@ namespace Innertube return nodeData["content"] end function + function ParseFeedFilterChipBarRenderer(nodeData as object, context as object) as object + contents = ObjectUtils.Dig(nodeData, ["contents"]) + if not IsArray(contents) + return invalid + end if + + sortOptions = [] + for each item in contents + if not IsAssociativeArray(item) or not item.DoesExist("chipCloudChipRenderer") + continue for + end if + chipCloudChipRenderer = item["chipCloudChipRenderer"] + if not IsAssociativeArray(chipCloudChipRenderer) + continue for + end if + continuationCommand = ObjectUtils.Dig(chipCloudChipRenderer, ["navigationEndpoint", "continuationCommand"]) + if not IsAssociativeArray(continuationCommand) or ValidString(continuationCommand["request"]) <> "CONTINUATION_REQUEST_TYPE_BROWSE" + continue for + end if + + title = ParseText(chipCloudChipRenderer["text"]) + token = ValidString(continuationCommand["token"]) + isSelected = ValidBool(chipCloudChipRenderer["isSelected"]) + if title = "" or token = "" + continue for + end if + + sortOptions.Push({ + "title": title + "continuation": token + "isSelected": isSelected + }) + end for + + if sortOptions.Count() > 0 + context.currentFeed.sortOptions = sortOptions + end if + + return invalid + end function + function ParseContinuationItemRenderer(nodeData as object, context as object) as object continuation = ObjectUtils.Dig(nodeData, ["continuationEndpoint", "continuationCommand", "token"]) if IsString(continuation) @@ -1222,6 +1288,10 @@ namespace Innertube return nodeData["continuationItems"] end function + function ParseReloadContinuationItemsCommand(nodeData as object, _context as object) as object + return nodeData["continuationItems"] + end function + function ParseGridContinuation(nodeData as object, context as object) as object continuation = ObjectUtils.Dig(nodeData, ["continuations", 0, "nextContinuationData", "continuation"]) if IsString(continuation) diff --git a/playlet-lib/src/components/Services/Innertube/Parser.bs b/playlet-lib/src/components/Services/Innertube/Parser.bs index f599bd42..0e420724 100644 --- a/playlet-lib/src/components/Services/Innertube/Parser.bs +++ b/playlet-lib/src/components/Services/Innertube/Parser.bs @@ -141,12 +141,16 @@ namespace Innertube items = [] continuation = invalid + sortOptions = invalid feeds = ParseResponseDataForFeeds(responseData, { "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 = { @@ -161,9 +165,13 @@ namespace Innertube "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 diff --git a/playlet-lib/src/components/Services/Invidious/InvidiousService.bs b/playlet-lib/src/components/Services/Invidious/InvidiousService.bs index 34d08557..d4c89c46 100644 --- a/playlet-lib/src/components/Services/Invidious/InvidiousService.bs +++ b/playlet-lib/src/components/Services/Invidious/InvidiousService.bs @@ -487,6 +487,7 @@ namespace Invidious return { items: json.videos continuation: json.continuation + sortOptions: json.sortOptions } end if return invalid @@ -510,6 +511,7 @@ namespace Invidious return { items: json.playlists continuation: json.continuation + sortOptions: json.sortOptions } end if return invalid diff --git a/playlet-lib/src/components/VideoFeed/VideoRowListRowContentTask.bs b/playlet-lib/src/components/VideoFeed/VideoRowListRowContentTask.bs index 96cfd760..5ed15142 100644 --- a/playlet-lib/src/components/VideoFeed/VideoRowListRowContentTask.bs +++ b/playlet-lib/src/components/VideoFeed/VideoRowListRowContentTask.bs @@ -129,6 +129,10 @@ function VideoRowListRowContentTask(input as object) as object hasContinuation = not StringUtils.IsNullOrEmpty(response.result.continuation) feedSource.state.continuation = ValidString(response.result.continuation) + if response.result.sortOptions <> invalid + feedSource.state.sortOptions = response.result.sortOptions + end if + if addedItemCount > 0 pageType = feedSource.state.paginationType if response.result.isLastPage = true or (hadContinuation and not hasContinuation) diff --git a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletInvidiousBackendRouter.bs b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletInvidiousBackendRouter.bs index 52dc206c..fccf7211 100644 --- a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletInvidiousBackendRouter.bs +++ b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletInvidiousBackendRouter.bs @@ -735,6 +735,24 @@ namespace Http options.params = params end if + ' sort by is a special sorting argument handled by Playlet backend. + ' It's a JSON object that could have the "params" key or "continuation" + ' key. In this case, we will override these options with the values + ' from the "sort_by" query parameter. If we do get an Invidious "sort_by" + ' (such as "newest", "oldest", etc.), we'll ignore it, as it is not supported. + sortBy = request.query.sort_by + if not StringUtils.IsNullOrEmpty(sortBy) and sortBy.StartsWith("{") + sortBy = ParseJson(sortBy) + if IsAssociativeArray(sortBy) + if sortBy.DoesExist("params") + options.params = sortBy.params + end if + if sortBy.DoesExist("continuation") + options.continuation = sortBy.continuation + end if + end if + end if + channel = InnertubeService.GetChannel(channelId, options) if raw response.Json(channel) @@ -747,28 +765,52 @@ namespace Http return true end if + result = {} 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 + result ["playlists"] = channel.items 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"] + result ["videos"] = channel.items + end if + if channel.DoesExist("continuation") + result.continuation = channel.continuation + end if + if channel.DoesExist("sortOptions") + ' Transform sort options from: + ' [{ + ' "title": "string", + ' "isSelected": "boolean" + ' "params?": "string", + ' "continuation?": "string" + ' }] + ' to: + ' [{ + ' "title": "string", + ' "isSelected": "boolean" + ' "params?": "string", + ' "continuation?": "string" + ' "id": "{ \"params\": \"string\", \"continuation\": \"string\" }" + ' }] + ' This so sorting doesn't interfere with actual params or continuation. + ' Sort options are later parsed by Playlet backend. + sortOptions = channel.sortOptions + if IsArray(sortOptions) and sortOptions.Count() > 0 + for i = 0 to sortOptions.Count() - 1 + sortOption = sortOptions[i] + sortOptionId = {} + if sortOption.DoesExist("params") + sortOptionId["params"] = sortOption.params + end if + if sortOption.DoesExist("continuation") + sortOptionId["continuation"] = sortOption.continuation + end if + sortOption.id = FormatJson(sortOptionId) + end for end if + result["sortOptions"] = sortOptions + end if + response.Json(result) + if channel.DoesExist("cache-control") + response.headers["cache-control"] = channel["cache-control"] end if return true