diff --git a/CHANGELOG.md b/CHANGELOG.md index e5dc13fe..db5f08a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Region and locale support for Playlet backend +- Search filters support for Playlet backend ### Fixed diff --git a/config/bsconfig.base.jsonc b/config/bsconfig.base.jsonc index 0c212e54..64764f2b 100644 --- a/config/bsconfig.base.jsonc +++ b/config/bsconfig.base.jsonc @@ -68,6 +68,7 @@ "!**/*.psd", "!**/*.svg", "!**/*.svg.meta.json5", + "!**/*.proto", "locale/**", "config/**", "lib/**", @@ -93,7 +94,8 @@ "../tools/bs-plugins/image-gen-plugin.ts", "../tools/bs-plugins/type-gen-plugin.ts", "../tools/bs-plugins/web-server-plugin.ts", - "../tools/bs-plugins/locale-validation-plugin.ts" + "../tools/bs-plugins/locale-validation-plugin.ts", + "../tools/bs-plugins/proto-gen-plugin.ts" ], "retainStagingDir": true, "require": [ diff --git a/config/bsconfig.tests.base.jsonc b/config/bsconfig.tests.base.jsonc index 06bda329..57c79b7e 100644 --- a/config/bsconfig.tests.base.jsonc +++ b/config/bsconfig.tests.base.jsonc @@ -8,6 +8,7 @@ "!**/*.psd", "!**/*.svg", "!**/*.svg.meta.json5", + "!**/*.proto", "locale/**", "config/**", "lib/**", @@ -31,7 +32,8 @@ "../tools/bs-plugins/image-gen-plugin.ts", "../tools/bs-plugins/type-gen-plugin.ts", "../tools/bs-plugins/web-server-plugin.ts", - "../tools/bs-plugins/locale-validation-plugin.ts" + "../tools/bs-plugins/locale-validation-plugin.ts", + "../tools/bs-plugins/proto-gen-plugin.ts" ], "rooibos": { "logLevel": 2, diff --git a/package-lock.json b/package-lock.json index 47ef5614..96f043a2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28,6 +28,7 @@ "ip": "^2.0.1", "json5": "^2.2.3", "mailparser": "^3.7.2", + "protocol-buffers-schema": "^3.6.0", "roku-deploy": "^3.12.3", "rooibos-roku": "5.14.0", "rounded-rect-9patch": "^1.1.2", @@ -4825,6 +4826,12 @@ "node": ">=0.4.0" } }, + "node_modules/protocol-buffers-schema": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/protocol-buffers-schema/-/protocol-buffers-schema-3.6.0.tgz", + "integrity": "sha512-TdDRD+/QNdrCGCE7v8340QyuXd4kIWIgapsE2+n/SaGiSSbomYl4TjHlvIoCWRpE7wFt02EpB35VVA2ImcBVqw==", + "dev": true + }, "node_modules/proxy-addr": { "version": "2.0.7", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", diff --git a/package.json b/package.json index 5a7bc905..38bddb5d 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "ip": "^2.0.1", "json5": "^2.2.3", "mailparser": "^3.7.2", + "protocol-buffers-schema": "^3.6.0", "roku-deploy": "^3.12.3", "rooibos-roku": "5.14.0", "rounded-rect-9patch": "^1.1.2", diff --git a/playlet-lib/src/components/Services/Innertube/InnertubeService.bs b/playlet-lib/src/components/Services/Innertube/InnertubeService.bs index 576de4f6..1df01285 100644 --- a/playlet-lib/src/components/Services/Innertube/InnertubeService.bs +++ b/playlet-lib/src/components/Services/Innertube/InnertubeService.bs @@ -5,6 +5,7 @@ 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" @@ -83,8 +84,8 @@ namespace InnertubeService } end function - function Search(query as string, cancellation = invalid as object) as object - return Innertube.Search(query, cancellation) + function Search(query as string, searchFilters as object, cancellation = invalid as object) as object + return Innertube.Search(query, searchFilters, cancellation) end function function GetTrending(options = invalid as object) as object diff --git a/playlet-lib/src/components/Services/Innertube/Search.bs b/playlet-lib/src/components/Services/Innertube/Search.bs index c954344f..8898aae7 100644 --- a/playlet-lib/src/components/Services/Innertube/Search.bs +++ b/playlet-lib/src/components/Services/Innertube/Search.bs @@ -3,7 +3,7 @@ import "pkg:/components/Services/Innertube/Parser.bs" namespace Innertube - function Search(query as string, cancellation = invalid as object) as object + function Search(query as string, searchFilters as object, cancellation = invalid as object) as object deviceInfo = CreateObject("roDeviceInfo") payload = { @@ -11,6 +11,13 @@ namespace Innertube context: Innertube.CreateContext(Innertube.ClientType.WEB, deviceInfo, "") } + if searchFilters <> invalid and searchFilters.Count() > 0 + params = Innertube.GetEncodedSearchFilters(searchFilters) + if params <> "" + payload["params"] = Innertube.GetEncodedSearchFilters(searchFilters) + end if + end if + request = HttpClient.PostJson("https://www.youtube.com/youtubei/v1/search?prettyPrint=false&alt=json", payload) request.Headers({ "accept": "*/*" diff --git a/playlet-lib/src/components/Services/Innertube/SearchFilters.bs b/playlet-lib/src/components/Services/Innertube/SearchFilters.bs new file mode 100644 index 00000000..e209e9fa --- /dev/null +++ b/playlet-lib/src/components/Services/Innertube/SearchFilters.bs @@ -0,0 +1,93 @@ +import "pkg:/components/Services/Innertube/protos/params.proto.gen.bs" + +namespace Innertube + function GetEncodedSearchFilters(searchFilters as object) as string + searchFiltersObj = { + filters: {} + } + + if searchFilters.DoesExist("sort") + sort = searchFilters["sort"] + if sort = "relevance" + searchFiltersObj["sort_by"] = Protobuf.Generated.misc.SortBy.RELEVANCE + else if sort = "rating" + searchFiltersObj["sort_by"] = Protobuf.Generated.misc.SortBy.RATING + else if sort = "views" + searchFiltersObj["sort_by"] = Protobuf.Generated.misc.SortBy.VIEW_COUNT + else if sort = "date" + searchFiltersObj["sort_by"] = Protobuf.Generated.misc.SortBy.UPLOAD_DATE + end if + end if + + if searchFilters.DoesExist("date") + date = searchFilters["date"] + if date = "hour" + searchFiltersObj["filters"]["upload_date"] = Protobuf.Generated.misc.UploadDate.HOUR + else if date = "today" + searchFiltersObj["filters"]["upload_date"] = Protobuf.Generated.misc.UploadDate.TODAY + else if date = "week" + searchFiltersObj["filters"]["upload_date"] = Protobuf.Generated.misc.UploadDate.WEEK + else if date = "month" + searchFiltersObj["filters"]["upload_date"] = Protobuf.Generated.misc.UploadDate.MONTH + else if date = "year" + searchFiltersObj["filters"]["upload_date"] = Protobuf.Generated.misc.UploadDate.YEAR + end if + end if + + if searchFilters.DoesExist("type") + _type = searchFilters["type"] + if _type = "video" + searchFiltersObj["filters"]["type"] = Protobuf.Generated.misc.SearchType.VIDEO + else if _type = "channel" + searchFiltersObj["filters"]["type"] = Protobuf.Generated.misc.SearchType.CHANNEL + else if _type = "playlist" + searchFiltersObj["filters"]["type"] = Protobuf.Generated.misc.SearchType.PLAYLIST + else if _type = "movie" + searchFiltersObj["filters"]["type"] = Protobuf.Generated.misc.SearchType.MOVIE + end if + end if + + if searchFilters.DoesExist("duration") + duration = searchFilters["duration"] + if duration = "short" + searchFiltersObj["filters"]["duration"] = Protobuf.Generated.misc.Duration.SHORT + else if duration = "long" + searchFiltersObj["filters"]["duration"] = Protobuf.Generated.misc.Duration.LONG + else if duration = "medium" + searchFiltersObj["filters"]["duration"] = Protobuf.Generated.misc.Duration.MEDIUM + end if + end if + + if searchFilters.DoesExist("features") + features = searchFilters["features"] + for each feature in features + if feature = "hd" + searchFiltersObj["filters"]["features_hd"] = true + else if feature = "subtitles" + searchFiltersObj["filters"]["features_subtitles"] = true + else if feature = "creative_commons" + searchFiltersObj["filters"]["features_creative_commons"] = true + else if feature = "3d" + searchFiltersObj["filters"]["features_3d"] = true + else if feature = "live" + searchFiltersObj["filters"]["features_live"] = true + else if feature = "purchased" + searchFiltersObj["filters"]["features_purchased"] = true + else if feature = "4k" + searchFiltersObj["filters"]["features_4k"] = true + else if feature = "360" + searchFiltersObj["filters"]["features_360"] = true + else if feature = "location" + searchFiltersObj["filters"]["features_location"] = true + else if feature = "hdr" + searchFiltersObj["filters"]["features_hdr"] = true + else if feature = "vr180" + searchFiltersObj["filters"]["features_vr180"] = true + end if + end for + end if + + return Protobuf.Generated.misc.encodeSearchFilter(searchFiltersObj) + end function +end namespace + diff --git a/playlet-lib/src/components/Services/Innertube/protos/params.proto b/playlet-lib/src/components/Services/Innertube/protos/params.proto new file mode 100644 index 00000000..41e30e18 --- /dev/null +++ b/playlet-lib/src/components/Services/Innertube/protos/params.proto @@ -0,0 +1,58 @@ +syntax = "proto3"; +package misc; + +// https://github.com/LuanRT/YouTube.js/blob/5f83a7416d5dc3167e6af2767e9b90f4bf89f3dc/protos/misc/params.proto#L9-L71 +message SearchFilter { + optional SortBy sort_by = 1; + + enum SortBy { + RELEVANCE = 0; + RATING = 1; + UPLOAD_DATE = 2; + VIEW_COUNT = 3; + } + + message Filters { + optional UploadDate upload_date = 1; + optional SearchType type = 2; + optional Duration duration = 3; + + optional bool features_hd = 4; + optional bool features_subtitles = 5; + optional bool features_creative_commons = 6; + optional bool features_3d = 7; + optional bool features_live = 8; + optional bool features_purchased = 9; + optional bool features_4k = 14; + optional bool features_360 = 15; + optional bool features_location = 23; + optional bool features_hdr = 25; + optional bool features_vr180 = 26; + + enum UploadDate { + ANY_DATE = 0; + HOUR = 1; + TODAY = 2; + WEEK = 3; + MONTH = 4; + YEAR = 5; + } + + enum SearchType { + ANY_TYPE = 0; + VIDEO = 1; + CHANNEL = 2; + PLAYLIST = 3; + MOVIE = 4; + } + + enum Duration { + ANY_DURATION = 0; + SHORT = 1; + LONG = 2; + MEDIUM = 3; + } + } + + optional Filters filters = 2; +} diff --git a/playlet-lib/src/components/Services/Innertube/protos/params.proto.gen.bs b/playlet-lib/src/components/Services/Innertube/protos/params.proto.gen.bs new file mode 100644 index 00000000..6a716551 --- /dev/null +++ b/playlet-lib/src/components/Services/Innertube/protos/params.proto.gen.bs @@ -0,0 +1,130 @@ +' Auto-generated file - do not modify manually +' md5:dfefe3075a9c6332b6072e7c02fdccf8 + +import "pkg:/source/Protobuf/BinaryReader.bs" +import "pkg:/source/Protobuf/BinaryWriter.bs" +import "pkg:/source/Protobuf/Decoder.bs" +import "pkg:/source/Protobuf/Encoder.bs" + +namespace Protobuf + namespace Generated + namespace misc + + enum SortBy + RELEVANCE = 0 + RATING = 1 + UPLOAD_DATE = 2 + VIEW_COUNT = 3 + end enum + + enum UploadDate + ANY_DATE = 0 + HOUR = 1 + TODAY = 2 + WEEK = 3 + MONTH = 4 + YEAR = 5 + end enum + + enum SearchType + ANY_TYPE = 0 + VIDEO = 1 + CHANNEL = 2 + PLAYLIST = 3 + MOVIE = 4 + end enum + + enum Duration + ANY_DURATION = 0 + SHORT = 1 + LONG = 2 + MEDIUM = 3 + end enum + + + + function encodeFilters(message as dynamic) as string + buffer = CreateObject("roByteArray") + writer = new Protobuf.BinaryWriter() + writer.SetBuffer(buffer) + encoder = new Protobuf.Encoder(writer) + if message.DoesExist("upload_date") + value = message["upload_date"] + encoder.EncodeInt32(1, value) + end if + if message.DoesExist("type") + value = message["type"] + encoder.EncodeInt32(2, value) + end if + if message.DoesExist("duration") + value = message["duration"] + encoder.EncodeInt32(3, value) + end if + if message.DoesExist("features_hd") + value = message["features_hd"] + encoder.EncodeBool(4, value) + end if + if message.DoesExist("features_subtitles") + value = message["features_subtitles"] + encoder.EncodeBool(5, value) + end if + if message.DoesExist("features_creative_commons") + value = message["features_creative_commons"] + encoder.EncodeBool(6, value) + end if + if message.DoesExist("features_3d") + value = message["features_3d"] + encoder.EncodeBool(7, value) + end if + if message.DoesExist("features_live") + value = message["features_live"] + encoder.EncodeBool(8, value) + end if + if message.DoesExist("features_purchased") + value = message["features_purchased"] + encoder.EncodeBool(9, value) + end if + if message.DoesExist("features_4k") + value = message["features_4k"] + encoder.EncodeBool(14, value) + end if + if message.DoesExist("features_360") + value = message["features_360"] + encoder.EncodeBool(15, value) + end if + if message.DoesExist("features_location") + value = message["features_location"] + encoder.EncodeBool(23, value) + end if + if message.DoesExist("features_hdr") + value = message["features_hdr"] + encoder.EncodeBool(25, value) + end if + if message.DoesExist("features_vr180") + value = message["features_vr180"] + encoder.EncodeBool(26, value) + end if + return buffer.ToBase64String().EncodeUriComponent() + end function + + function encodeSearchFilter(message as dynamic) as string + buffer = CreateObject("roByteArray") + writer = new Protobuf.BinaryWriter() + writer.SetBuffer(buffer) + encoder = new Protobuf.Encoder(writer) + if message.DoesExist("sort_by") + value = message["sort_by"] + encoder.EncodeInt32(1, value) + end if + if message.DoesExist("filters") + value = message["filters"] + encoder.EncodeMessage(2, Protobuf.Generated.misc.encodeFilters(value)) + end if + return buffer.ToBase64String().EncodeUriComponent() + end function + + + + end namespace + end namespace +end namespace diff --git a/playlet-lib/src/components/Services/Innertube/tests/SearchFiltersTestNode.xml b/playlet-lib/src/components/Services/Innertube/tests/SearchFiltersTestNode.xml new file mode 100644 index 00000000..d22c3d67 --- /dev/null +++ b/playlet-lib/src/components/Services/Innertube/tests/SearchFiltersTestNode.xml @@ -0,0 +1,6 @@ + + + + +