From 296771809a73f6dec1fbe86c58106f06097299c8 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Mon, 17 Sep 2018 16:38:18 -0500 Subject: [PATCH] Refactor protocol buffers --- src/invidious.cr | 14 ++++++++++---- src/invidious/channels.cr | 10 ++++++++-- src/invidious/playlists.cr | 39 +++++++++++++++++++------------------- src/invidious/search.cr | 27 +++++++++++++++----------- 4 files changed, 54 insertions(+), 36 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 38c3e0185..3286f3aef 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -455,7 +455,7 @@ get "/search" do |env| date = value when "duration" duration = value - when "features" + when "feature", "features" features = value.split(",") when "sort" sort = value @@ -478,8 +478,14 @@ get "/search" do |env| ) v_search WHERE v_search.document @@ plainto_tsquery($1) LIMIT 20 OFFSET $2;", [search_query, (page - 1) * 20] + ucids, as: ChannelVideo) count = videos.size else - search_params = build_search_params(sort: sort, date: date, content_type: "video", - duration: duration, features: features) + begin + search_params = produce_search_params(sort: sort, date: date, content_type: "video", + duration: duration, features: features) + rescue ex + error_message = ex.message + next templated "error" + end + count, videos = search(search_query, page, search_params).as(Tuple) end @@ -2643,7 +2649,7 @@ get "/api/v1/search" do |env| env.response.content_type = "application/json" begin - search_params = build_search_params(sort_by, date, content_type, duration, features) + search_params = produce_search_params(sort_by, date, content_type, duration, features) rescue ex next JSON.build do |json| json.object do diff --git a/src/invidious/channels.cr b/src/invidious/channels.cr index 8d4519762..02e260ada 100644 --- a/src/invidious/channels.cr +++ b/src/invidious/channels.cr @@ -145,10 +145,16 @@ def produce_channel_videos_url(ucid, page = 1, auto_generated = nil) switch = "\x00" end - meta = "\x12\x06videos #{switch}\x30\x02\x38\x01\x60\x01\x6a\x00\x7a" + meta = "\x12\x06videos" + meta += "\x30\x02" + meta += "\x38\x01" + meta += "\x60\x01" + meta += "\x6a\x00" + meta += "\xb8\x01\x00" + meta += "\x20#{switch}" + meta += "\x7a" meta += page.size.to_u8.unsafe_chr meta += page - meta += "\xb8\x01\x00" meta = Base64.urlsafe_encode(meta) meta = URI.escape(meta) diff --git a/src/invidious/playlists.cr b/src/invidious/playlists.cr index 6a0c6c385..4b00e4297 100644 --- a/src/invidious/playlists.cr +++ b/src/invidious/playlists.cr @@ -88,28 +88,29 @@ def produce_playlist_url(id, index) end ucid = "VL" + id - continuation = [0x08_u8] + write_var_int(index) - slice = continuation.to_unsafe.to_slice(continuation.size) - slice = Base64.urlsafe_encode(slice, false) + meta = "\x08#{write_var_int(index).join}" + meta = Base64.urlsafe_encode(meta, false) + meta = "PT:#{meta}" - # Inner Base64 - continuation = "PT:" + slice - continuation = [0x7a_u8, continuation.bytes.size.to_u8] + continuation.bytes - slice = continuation.to_unsafe.to_slice(continuation.size) - slice = Base64.urlsafe_encode(slice) - slice = URI.escape(slice) + wrapped = "\x7a" + wrapped += meta.bytes.size.unsafe_chr + wrapped += meta - # Outer Base64 - continuation = [0x1a_u8, slice.bytes.size.to_u8] + slice.bytes - continuation = ucid.bytes + continuation - continuation = [0x12_u8, ucid.size.to_u8] + continuation - continuation = [0xe2_u8, 0xa9_u8, 0x85_u8, 0xb2_u8, 2_u8, continuation.size.to_u8] + continuation + wrapped = Base64.urlsafe_encode(wrapped) + meta = URI.escape(wrapped) - # Wrap bytes - slice = continuation.to_unsafe.to_slice(continuation.size) - slice = Base64.urlsafe_encode(slice) - slice = URI.escape(slice) - continuation = slice + continuation = "\x12" + continuation += ucid.size.unsafe_chr + continuation += ucid + continuation += "\x1a" + continuation += meta.bytes.size.unsafe_chr + continuation += meta + + continuation = continuation.size.to_u8.unsafe_chr + continuation + continuation = "\xe2\xa9\x85\xb2\x02" + continuation + + continuation = Base64.urlsafe_encode(continuation) + continuation = URI.escape(continuation) url = "/browse_ajax?action_continuation=1&continuation=#{continuation}" diff --git a/src/invidious/search.cr b/src/invidious/search.cr index 9e4d277a5..32282afcd 100644 --- a/src/invidious/search.cr +++ b/src/invidious/search.cr @@ -49,7 +49,7 @@ def channel_search(query, page, channel) return count, videos end -def search(query, page = 1, search_params = build_search_params(content_type: "video")) +def search(query, page = 1, search_params = produce_search_params(content_type: "video")) client = make_client(YT_URL) if query.empty? return {0, [] of SearchVideo} @@ -67,8 +67,8 @@ def search(query, page = 1, search_params = build_search_params(content_type: "v return {nodeset.size, videos} end -def build_search_params(sort : String = "relevance", date : String = "", content_type : String = "", - duration : String = "", features : Array(String) = [] of String) +def produce_search_params(sort : String = "relevance", date : String = "", content_type : String = "", + duration : String = "", features : Array(String) = [] of String) head = "\x08" head += case sort when "relevance" @@ -151,7 +151,7 @@ def build_search_params(sort : String = "relevance", date : String = "", content end if body.size > 0 - token = head + "\x12" + body.size.to_u8.unsafe_chr + body + token = head + "\x12" + body.size.unsafe_chr + body else token = head end @@ -165,25 +165,30 @@ end def produce_channel_search_url(ucid, query, page) page = "#{page}" - meta = "\x12\x06search0\x02\x38\x01\x60\x01\x6a\x00\x7a" - meta += page.size.to_u8.unsafe_chr - meta += page + meta = "\x12\x06search" + meta += "\x30\x02" + meta += "\x38\x01" + meta += "\x60\x01" + meta += "\x6a\x00" meta += "\xb8\x01\x00" + meta += "\x7a" + meta += page.size.unsafe_chr + meta += page meta = Base64.urlsafe_encode(meta) meta = URI.escape(meta) continuation = "\x12" - continuation += ucid.size.to_u8.unsafe_chr + continuation += ucid.size.unsafe_chr continuation += ucid continuation += "\x1a" - continuation += meta.size.to_u8.unsafe_chr + continuation += meta.size.unsafe_chr continuation += meta continuation += "\x5a" - continuation += query.size.to_u8.unsafe_chr + continuation += query.size.unsafe_chr continuation += query - continuation = continuation.size.to_u8.unsafe_chr + continuation + continuation = continuation.size.unsafe_chr + continuation continuation = "\xe2\xa9\x85\xb2\x02" + continuation continuation = Base64.urlsafe_encode(continuation)