Use IO::Memory for creating continuation tokens

This commit is contained in:
Omar Roth 2019-02-04 15:17:10 -06:00
parent ed8a9af355
commit 276662a147
5 changed files with 101 additions and 79 deletions

View File

@ -24,25 +24,25 @@ describe "Helpers" do
describe "#produce_channel_search_url" do describe "#produce_channel_search_url" do
it "correctly produces token for searching a specific channel" do it "correctly produces token for searching a specific channel" do
produce_channel_search_url("UCXuqSBlHAE6Xw-yeJA0Tunw", "", 100).should eq("/browse_ajax?continuation=4qmFsgI-EhhVQ1h1cVNCbEhBRTZYdy15ZUpBMFR1bncaIEVnWnpaV0Z5WTJnd0FqZ0JZQUZxQUxnQkFIb0RNVEF3WgA%3D") produce_channel_search_url("UCXuqSBlHAE6Xw-yeJA0Tunw", "", 100).should eq("/browse_ajax?continuation=4qmFsgI-EhhVQ1h1cVNCbEhBRTZYdy15ZUpBMFR1bncaIEVnWnpaV0Z5WTJnd0FqZ0JZQUZxQUxnQkFIb0RNVEF3WgA%3D&gl=US&hl=en")
produce_channel_search_url("UCXuqSBlHAE6Xw-yeJA0Tunw", "По ожиशुपतिरपि子而時ஸ்றீனி", 0).should eq("/browse_ajax?continuation=4qmFsgJZEhhVQ1h1cVNCbEhBRTZYdy15ZUpBMFR1bncaJEVnWnpaV0Z5WTJnd0FqZ0JZQUZxQUxnQkFIb0JNQSUzRCUzRFoX0J_QviDQvtC20LjgpLbgpYHgpKrgpKTgpL_gpLDgpKrgpL_lrZDogIzmmYLgrrjgr43grrHgr4Dgrqngrr8%3D") produce_channel_search_url("UCXuqSBlHAE6Xw-yeJA0Tunw", "По ожиशुपतिरपि子而時ஸ்றீனி", 0).should eq("/browse_ajax?continuation=4qmFsgJZEhhVQ1h1cVNCbEhBRTZYdy15ZUpBMFR1bncaJEVnWnpaV0Z5WTJnd0FqZ0JZQUZxQUxnQkFIb0JNQSUzRCUzRFoX0J_QviDQvtC20LjgpLbgpYHgpKrgpKTgpL_gpLDgpKrgpL_lrZDogIzmmYLgrrjgr43grrHgr4Dgrqngrr8%3D&gl=US&hl=en")
end end
end end
describe "#produce_playlist_url" do describe "#produce_playlist_url" do
it "correctly produces url for requesting index `x` of a playlist" do it "correctly produces url for requesting index `x` of a playlist" do
produce_playlist_url("UUCla9fZca4I7KagBtgRGnOw", 0).should eq("/browse_ajax?continuation=4qmFsgIsEhpWTFVVQ2xhOWZaY2E0STdLYWdCdGdSR25PdxoOZWdaUVZEcERRVUUlM0Q%3D") produce_playlist_url("UUCla9fZca4I7KagBtgRGnOw", 0).should eq("/browse_ajax?continuation=4qmFsgIsEhpWTFVVQ2xhOWZaY2E0STdLYWdCdGdSR25PdxoOZWdaUVZEcERRVUUlM0Q%3D&gl=US&hl=en")
produce_playlist_url("UCCla9fZca4I7KagBtgRGnOw", 0).should eq("/browse_ajax?continuation=4qmFsgIsEhpWTFVVQ2xhOWZaY2E0STdLYWdCdGdSR25PdxoOZWdaUVZEcERRVUUlM0Q%3D") produce_playlist_url("UCCla9fZca4I7KagBtgRGnOw", 0).should eq("/browse_ajax?continuation=4qmFsgIsEhpWTFVVQ2xhOWZaY2E0STdLYWdCdGdSR25PdxoOZWdaUVZEcERRVUUlM0Q%3D&gl=US&hl=en")
produce_playlist_url("PLt5AfwLFPxWLNVixpe1w3fi6lE2OTq0ET", 0).should eq("/browse_ajax?continuation=4qmFsgI2EiRWTFBMdDVBZndMRlB4V0xOVml4cGUxdzNmaTZsRTJPVHEwRVQaDmVnWlFWRHBEUVVFJTNE") produce_playlist_url("PLt5AfwLFPxWLNVixpe1w3fi6lE2OTq0ET", 0).should eq("/browse_ajax?continuation=4qmFsgI2EiRWTFBMdDVBZndMRlB4V0xOVml4cGUxdzNmaTZsRTJPVHEwRVQaDmVnWlFWRHBEUVVFJTNE&gl=US&hl=en")
produce_playlist_url("PLt5AfwLFPxWLNVixpe1w3fi6lE2OTq0ET", 10000).should eq("/browse_ajax?continuation=4qmFsgI0EiRWTFBMdDVBZndMRlB4V0xOVml4cGUxdzNmaTZsRTJPVHEwRVQaDGVnZFFWRHBEU2tKUA%3D%3D") produce_playlist_url("PLt5AfwLFPxWLNVixpe1w3fi6lE2OTq0ET", 10000).should eq("/browse_ajax?continuation=4qmFsgI0EiRWTFBMdDVBZndMRlB4V0xOVml4cGUxdzNmaTZsRTJPVHEwRVQaDGVnZFFWRHBEU2tKUA%3D%3D&gl=US&hl=en")
produce_playlist_url("PL55713C70BA91BD6E", 0).should eq("/browse_ajax?continuation=4qmFsgImEhRWTFBMNTU3MTNDNzBCQTkxQkQ2RRoOZWdaUVZEcERRVUUlM0Q%3D") produce_playlist_url("PL55713C70BA91BD6E", 0).should eq("/browse_ajax?continuation=4qmFsgImEhRWTFBMNTU3MTNDNzBCQTkxQkQ2RRoOZWdaUVZEcERRVUUlM0Q%3D&gl=US&hl=en")
produce_playlist_url("PL55713C70BA91BD6E", 10000).should eq("/browse_ajax?continuation=4qmFsgIkEhRWTFBMNTU3MTNDNzBCQTkxQkQ2RRoMZWdkUVZEcERTa0pQ") produce_playlist_url("PL55713C70BA91BD6E", 10000).should eq("/browse_ajax?continuation=4qmFsgIkEhRWTFBMNTU3MTNDNzBCQTkxQkQ2RRoMZWdkUVZEcERTa0pQ&gl=US&hl=en")
end end
end end

View File

@ -202,50 +202,58 @@ def produce_channel_videos_url(ucid, page = 1, auto_generated = nil, sort_by = "
timestamp = seed - (page - 1).months timestamp = seed - (page - 1).months
page = "#{timestamp.to_unix}" page = "#{timestamp.to_unix}"
switch = "\x36" switch = 0x36
else else
page = "#{page}" page = "#{page}"
switch = "\x00" switch = 0x00
end end
meta = "\x12\x06videos" meta = IO::Memory.new
meta += "\x30\x02" meta.write(Bytes[0x12, 0x06])
meta += "\x38\x01" meta.print("videos")
meta += "\x60\x01"
meta += "\x6a\x00" meta.write(Bytes[0x30, 0x02])
meta += "\xb8\x01\x00" meta.write(Bytes[0x38, 0x01])
meta += "\x20#{switch}" meta.write(Bytes[0x60, 0x01])
meta += "\x7a" meta.write(Bytes[0x6a, 0x00])
meta += page.size.to_u8.unsafe_chr meta.write(Bytes[0xb8, 0x01, 0x00])
meta += page
meta.write(Bytes[0x20, switch, 0x7a, page.size])
meta.print(page)
case sort_by case sort_by
when "newest" when "newest"
# Empty tags can be omitted # Empty tags can be omitted
# meta += "\x18\x00" # meta.write(Bytes[0x18,0x00])
when "popular" when "popular"
meta += "\x18\x01" meta.write(Bytes[0x18, 0x01])
when "oldest" when "oldest"
meta += "\x18\x02" meta.write(Bytes[0x18, 0x02])
end end
meta = Base64.urlsafe_encode(meta) meta.rewind
meta = Base64.urlsafe_encode(meta.to_slice)
meta = URI.escape(meta) meta = URI.escape(meta)
continuation = "\x12" continuation = IO::Memory.new
continuation += ucid.size.to_u8.unsafe_chr continuation.write(Bytes[0x12, ucid.size])
continuation += ucid continuation.print(ucid)
continuation += "\x1a"
continuation += meta.size.to_u8.unsafe_chr
continuation += meta
continuation = continuation.size.to_u8.unsafe_chr + continuation continuation.write(Bytes[0x1a, meta.size])
continuation = "\xe2\xa9\x85\xb2\x02" + continuation continuation.print(meta)
continuation = Base64.urlsafe_encode(continuation) continuation.rewind
continuation = URI.escape(continuation) continuation = continuation.gets_to_end
url = "/browse_ajax?continuation=#{continuation}&gl=US&hl=en" wrapper = IO::Memory.new
wrapper.write(Bytes[0xe2, 0xa9, 0x85, 0xb2, 0x02, continuation.size])
wrapper.print(continuation)
wrapper.rewind
wrapper = Base64.urlsafe_encode(wrapper.to_slice)
wrapper = URI.escape(wrapper)
url = "/browse_ajax?continuation=#{wrapper}&gl=US&hl=en"
return url return url
end end

View File

@ -282,7 +282,7 @@ def write_var_int(value : Int)
end end
end end
return bytes return Slice.new(bytes.to_unsafe, bytes.size)
end end
def sha256(text) def sha256(text)

View File

@ -126,32 +126,37 @@ def produce_playlist_url(id, index)
end end
ucid = "VL" + id ucid = "VL" + id
meta = [0x08_u8] + write_var_int(index) meta = IO::Memory.new
meta = Slice.new(meta.to_unsafe, meta.size) meta.write(Bytes[0x08])
meta = Base64.urlsafe_encode(meta, false) meta.write(write_var_int(index))
meta.rewind
meta = Base64.urlsafe_encode(meta.to_slice, false)
meta = "PT:#{meta}" meta = "PT:#{meta}"
wrapped = "\x7a" continuation = IO::Memory.new
wrapped += meta.bytes.size.unsafe_chr continuation.write(Bytes[0x7a, meta.size])
wrapped += meta continuation.print(meta)
wrapped = Base64.urlsafe_encode(wrapped) continuation.rewind
meta = URI.escape(wrapped) meta = Base64.urlsafe_encode(continuation.to_slice)
meta = URI.escape(meta)
continuation = "\x12" continuation = IO::Memory.new
continuation += ucid.size.unsafe_chr continuation.write(Bytes[0x12, ucid.size])
continuation += ucid continuation.print(ucid)
continuation += "\x1a" continuation.write(Bytes[0x1a, meta.size])
continuation += meta.bytes.size.unsafe_chr continuation.print(meta)
continuation += meta
continuation = continuation.size.to_u8.unsafe_chr + continuation wrapper = IO::Memory.new
continuation = "\xe2\xa9\x85\xb2\x02" + continuation wrapper.write(Bytes[0xe2, 0xa9, 0x85, 0xb2, 0x02, continuation.size])
wrapper.print(continuation)
wrapper.rewind
continuation = Base64.urlsafe_encode(continuation) wrapper = Base64.urlsafe_encode(wrapper.to_slice)
continuation = URI.escape(continuation) wrapper = URI.escape(wrapper)
url = "/browse_ajax?continuation=#{continuation}" url = "/browse_ajax?continuation=#{wrapper}&gl=US&hl=en"
return url return url
end end

View File

@ -203,36 +203,45 @@ end
def produce_channel_search_url(ucid, query, page) def produce_channel_search_url(ucid, query, page)
page = "#{page}" page = "#{page}"
meta = "\x12\x06search" meta = IO::Memory.new
meta += "\x30\x02" meta.write(Bytes[0x12, 0x06])
meta += "\x38\x01" meta.print("search")
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.write(Bytes[0x30, 0x02])
meta.write(Bytes[0x38, 0x01])
meta.write(Bytes[0x60, 0x01])
meta.write(Bytes[0x6a, 0x00])
meta.write(Bytes[0xb8, 0x01, 0x00])
meta.write(Bytes[0x7a, page.size])
meta.print(page)
meta.rewind
meta = Base64.urlsafe_encode(meta.to_slice)
meta = URI.escape(meta) meta = URI.escape(meta)
continuation = "\x12" continuation = IO::Memory.new
continuation += ucid.size.unsafe_chr continuation.write(Bytes[0x12, ucid.size])
continuation += ucid continuation.print(ucid)
continuation += "\x1a"
continuation += meta.size.unsafe_chr
continuation += meta
continuation += "\x5a"
continuation += query.size.unsafe_chr
continuation += query
continuation = continuation.size.unsafe_chr + continuation continuation.write(Bytes[0x1a, meta.size])
continuation = "\xe2\xa9\x85\xb2\x02" + continuation continuation.print(meta)
continuation = Base64.urlsafe_encode(continuation) continuation.write(Bytes[0x5a, query.size])
continuation = URI.escape(continuation) continuation.print(query)
url = "/browse_ajax?continuation=#{continuation}" continuation.rewind
continuation = continuation.gets_to_end
wrapper = IO::Memory.new
wrapper.write(Bytes[0xe2, 0xa9, 0x85, 0xb2, 0x02, continuation.size])
wrapper.print(continuation)
wrapper.rewind
wrapper = Base64.urlsafe_encode(wrapper.to_slice)
wrapper = URI.escape(wrapper)
url = "/browse_ajax?continuation=#{wrapper}&gl=US&hl=en"
return url return url
end end