Refactor protocol buffers

This commit is contained in:
Omar Roth 2018-09-17 16:38:18 -05:00
parent 83ba4e2a4c
commit 296771809a
4 changed files with 54 additions and 36 deletions

View file

@ -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",
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

View file

@ -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)

View file

@ -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}"

View file

@ -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,7 +67,7 @@ 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 = "",
def produce_search_params(sort : String = "relevance", date : String = "", content_type : String = "",
duration : String = "", features : Array(String) = [] of String)
head = "\x08"
head += case sort
@ -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)