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 date = value
when "duration" when "duration"
duration = value duration = value
when "features" when "feature", "features"
features = value.split(",") features = value.split(",")
when "sort" when "sort"
sort = value 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) ) v_search WHERE v_search.document @@ plainto_tsquery($1) LIMIT 20 OFFSET $2;", [search_query, (page - 1) * 20] + ucids, as: ChannelVideo)
count = videos.size count = videos.size
else 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) duration: duration, features: features)
rescue ex
error_message = ex.message
next templated "error"
end
count, videos = search(search_query, page, search_params).as(Tuple) count, videos = search(search_query, page, search_params).as(Tuple)
end end
@ -2643,7 +2649,7 @@ get "/api/v1/search" do |env|
env.response.content_type = "application/json" env.response.content_type = "application/json"
begin 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 rescue ex
next JSON.build do |json| next JSON.build do |json|
json.object do json.object do

View File

@ -145,10 +145,16 @@ def produce_channel_videos_url(ucid, page = 1, auto_generated = nil)
switch = "\x00" switch = "\x00"
end 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.size.to_u8.unsafe_chr
meta += page meta += page
meta += "\xb8\x01\x00"
meta = Base64.urlsafe_encode(meta) meta = Base64.urlsafe_encode(meta)
meta = URI.escape(meta) meta = URI.escape(meta)

View File

@ -88,28 +88,29 @@ def produce_playlist_url(id, index)
end end
ucid = "VL" + id ucid = "VL" + id
continuation = [0x08_u8] + write_var_int(index) meta = "\x08#{write_var_int(index).join}"
slice = continuation.to_unsafe.to_slice(continuation.size) meta = Base64.urlsafe_encode(meta, false)
slice = Base64.urlsafe_encode(slice, false) meta = "PT:#{meta}"
# Inner Base64 wrapped = "\x7a"
continuation = "PT:" + slice wrapped += meta.bytes.size.unsafe_chr
continuation = [0x7a_u8, continuation.bytes.size.to_u8] + continuation.bytes wrapped += meta
slice = continuation.to_unsafe.to_slice(continuation.size)
slice = Base64.urlsafe_encode(slice)
slice = URI.escape(slice)
# Outer Base64 wrapped = Base64.urlsafe_encode(wrapped)
continuation = [0x1a_u8, slice.bytes.size.to_u8] + slice.bytes meta = URI.escape(wrapped)
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
# Wrap bytes continuation = "\x12"
slice = continuation.to_unsafe.to_slice(continuation.size) continuation += ucid.size.unsafe_chr
slice = Base64.urlsafe_encode(slice) continuation += ucid
slice = URI.escape(slice) continuation += "\x1a"
continuation = slice 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}" url = "/browse_ajax?action_continuation=1&continuation=#{continuation}"

View File

@ -49,7 +49,7 @@ def channel_search(query, page, channel)
return count, videos return count, videos
end 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) client = make_client(YT_URL)
if query.empty? if query.empty?
return {0, [] of SearchVideo} 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} return {nodeset.size, videos}
end 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) duration : String = "", features : Array(String) = [] of String)
head = "\x08" head = "\x08"
head += case sort head += case sort
@ -151,7 +151,7 @@ def build_search_params(sort : String = "relevance", date : String = "", content
end end
if body.size > 0 if body.size > 0
token = head + "\x12" + body.size.to_u8.unsafe_chr + body token = head + "\x12" + body.size.unsafe_chr + body
else else
token = head token = head
end end
@ -165,25 +165,30 @@ end
def produce_channel_search_url(ucid, query, page) def produce_channel_search_url(ucid, query, page)
page = "#{page}" page = "#{page}"
meta = "\x12\x06search0\x02\x38\x01\x60\x01\x6a\x00\x7a" meta = "\x12\x06search"
meta += page.size.to_u8.unsafe_chr meta += "\x30\x02"
meta += page meta += "\x38\x01"
meta += "\x60\x01"
meta += "\x6a\x00"
meta += "\xb8\x01\x00" meta += "\xb8\x01\x00"
meta += "\x7a"
meta += page.size.unsafe_chr
meta += page
meta = Base64.urlsafe_encode(meta) meta = Base64.urlsafe_encode(meta)
meta = URI.escape(meta) meta = URI.escape(meta)
continuation = "\x12" continuation = "\x12"
continuation += ucid.size.to_u8.unsafe_chr continuation += ucid.size.unsafe_chr
continuation += ucid continuation += ucid
continuation += "\x1a" continuation += "\x1a"
continuation += meta.size.to_u8.unsafe_chr continuation += meta.size.unsafe_chr
continuation += meta continuation += meta
continuation += "\x5a" continuation += "\x5a"
continuation += query.size.to_u8.unsafe_chr continuation += query.size.unsafe_chr
continuation += query continuation += query
continuation = continuation.size.to_u8.unsafe_chr + continuation continuation = continuation.size.unsafe_chr + continuation
continuation = "\xe2\xa9\x85\xb2\x02" + continuation continuation = "\xe2\xa9\x85\xb2\x02" + continuation
continuation = Base64.urlsafe_encode(continuation) continuation = Base64.urlsafe_encode(continuation)