From 7a9ef0d664c9d8d4eec879fadeb823568877bf28 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sat, 9 Feb 2019 10:15:14 -0600 Subject: [PATCH] Add produce_channel_playlists_url --- spec/helpers_spec.cr | 4 +- src/invidious/channels.cr | 3 +- src/invidious/helpers/utils.cr | 12 ++-- src/invidious/playlists.cr | 111 +++++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+), 9 deletions(-) diff --git a/spec/helpers_spec.cr b/spec/helpers_spec.cr index fddf55a58..6f33babd1 100644 --- a/spec/helpers_spec.cr +++ b/spec/helpers_spec.cr @@ -16,9 +16,9 @@ describe "Helpers" do produce_channel_videos_url(ucid: "UCXuqSBlHAE6Xw-yeJA0Tunw", page: 20).should eq("/browse_ajax?continuation=4qmFsgJEEhhVQ1h1cVNCbEhBRTZYdy15ZUpBMFR1bncaKEVnWjJhV1JsYjNNd0FqZ0JZQUZxQUxnQkFDQUFlZ0l5TUElM0QlM0Q%3D&gl=US&hl=en") - produce_channel_videos_url(ucid: "UC-9-kyTW8ZkZNDHQJ6FgpwQ", auto_generated: true).should eq("/browse_ajax?continuation=4qmFsgJIEhhVQy05LWt5VFc4WmtaTkRIUUo2Rmdwd1EaLEVnWjJhV1JsYjNNd0FqZ0JZQUZxQUxnQkFDQTJlZ294TlRRNU5qQXpOelE1&gl=US&hl=en") + produce_channel_videos_url(ucid: "UC-9-kyTW8ZkZNDHQJ6FgpwQ", auto_generated: true).should eq("/browse_ajax?continuation=4qmFsgJIEhhVQy05LWt5VFc4WmtaTkRIUUo2Rmdwd1EaLEVnWjJhV1JsYjNNd0FqZ0JZQUZxQUxnQkFDQTJlZ294TlRVeU1ESXlPVFE1&gl=US&hl=en") - produce_channel_videos_url(ucid: "UC-9-kyTW8ZkZNDHQJ6FgpwQ", page: 20, auto_generated: true, sort_by: "popular").should eq("/browse_ajax?continuation=4qmFsgJOEhhVQy05LWt5VFc4WmtaTkRIUUo2Rmdwd1EaMkVnWjJhV1JsYjNNd0FqZ0JZQUZxQUxnQkFDQTJlZ294TkRrNU5Ea3hOelE1R0FFJTNE&gl=US&hl=en") + produce_channel_videos_url(ucid: "UC-9-kyTW8ZkZNDHQJ6FgpwQ", page: 20, auto_generated: true, sort_by: "popular").should eq("/browse_ajax?continuation=4qmFsgJOEhhVQy05LWt5VFc4WmtaTkRIUUo2Rmdwd1EaMkVnWjJhV1JsYjNNd0FqZ0JZQUZxQUxnQkFDQTJlZ294TlRBeU1UY3dNVFE1R0FFJTNE&gl=US&hl=en") end end diff --git a/src/invidious/channels.cr b/src/invidious/channels.cr index 28fbdcd63..956450d59 100644 --- a/src/invidious/channels.cr +++ b/src/invidious/channels.cr @@ -218,7 +218,8 @@ def produce_channel_videos_url(ucid, page = 1, auto_generated = nil, sort_by = " meta.write(Bytes[0x6a, 0x00]) meta.write(Bytes[0xb8, 0x01, 0x00]) - meta.write(Bytes[0x20, switch, 0x7a, page.size]) + meta.write(Bytes[0x20, switch]) + meta.write(Bytes[0x7a, page.size]) meta.print(page) case sort_by diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr index 282e8ce52..ef2e35ddb 100644 --- a/src/invidious/helpers/utils.cr +++ b/src/invidious/helpers/utils.cr @@ -240,21 +240,21 @@ def get_referer(env, fallback = "/") end def read_var_int(bytes) - numRead = 0 + num_read = 0 result = 0 - read = bytes[numRead] + read = bytes[num_read] if bytes.size == 1 result = bytes[0].to_i32 else while ((read & 0b10000000) != 0) - read = bytes[numRead].to_u64 + read = bytes[num_read].to_u64 value = (read & 0b01111111) - result |= (value << (7 * numRead)) + result |= (value << (7 * num_read)) - numRead += 1 - if numRead > 5 + num_read += 1 + if num_read > 5 raise "VarInt is too big" end end diff --git a/src/invidious/playlists.cr b/src/invidious/playlists.cr index 28f2e4ced..220a0ef78 100644 --- a/src/invidious/playlists.cr +++ b/src/invidious/playlists.cr @@ -161,6 +161,117 @@ def produce_playlist_url(id, index) return url end +def produce_channel_playlists_url(ucid, cursor, sort = "newest") + cursor = Base64.urlsafe_encode(cursor, false) + + meta = IO::Memory.new + meta.write(Bytes[0x12, 0x09]) + meta.print("playlists") + + # TODO: Look at 0x01, 0x00 + case sort + when "oldest", "oldest_created" + meta.write(Bytes[0x18, 0x02]) + when "newest", "newest_created" + meta.write(Bytes[0x18, 0x03]) + when "last", "last_added" + meta.write(Bytes[0x18, 0x04]) + end + + meta.write(Bytes[0x20, 0x01]) + meta.write(Bytes[0x30, 0x02]) + meta.write(Bytes[0x38, 0x01]) + meta.write(Bytes[0x60, 0x01]) + meta.write(Bytes[0x6a, 0x00]) + + meta.write(Bytes[0x7a, cursor.size]) + meta.print(cursor) + + meta.write(Bytes[0xb8, 0x01, 0x00]) + + meta.rewind + meta = Base64.urlsafe_encode(meta.to_slice) + meta = URI.escape(meta) + + continuation = IO::Memory.new + continuation.write(Bytes[0x12, ucid.size]) + continuation.print(ucid) + + continuation.write(Bytes[0x1a]) + continuation.write(write_var_int(meta.size)) + continuation.print(meta) + + continuation.rewind + continuation = continuation.gets_to_end + + wrapper = IO::Memory.new + wrapper.write(Bytes[0xe2, 0xa9, 0x85, 0xb2, 0x02]) + wrapper.write(write_var_int(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 +end + +def extract_channel_playlists_cursor(url) + wrapper = HTTP::Params.parse(URI.parse(url).query.not_nil!)["continuation"] + + wrapper = URI.unescape(wrapper) + wrapper = Base64.decode(wrapper) + + # 0xe2 0xa9 0x85 0xb2 0x02 + wrapper += 5 + + continuation_size = read_var_int(wrapper[0, 4]) + wrapper += write_var_int(continuation_size).size + continuation = wrapper[0, continuation_size] + + # 0x12 + continuation += 1 + ucid_size = continuation[0] + continuation += 1 + ucid = continuation[0, ucid_size] + continuation += ucid_size + + # 0x1a + continuation += 1 + meta_size = read_var_int(continuation[0, 4]) + continuation += write_var_int(meta_size).size + meta = continuation[0, meta_size] + continuation += meta_size + + meta = String.new(meta) + meta = URI.unescape(meta) + meta = Base64.decode(meta) + + # 0x12 0x09 playlists + meta += 11 + + until meta[0] == 0x7a + tag = read_var_int(meta[0, 4]) + meta += write_var_int(tag).size + value = meta[0] + meta += 1 + end + + # 0x7a + meta += 1 + cursor_size = meta[0] + meta += 1 + cursor = meta[0, cursor_size] + + cursor = String.new(cursor) + cursor = URI.unescape(cursor) + cursor = Base64.decode_string(cursor) + + return cursor +end + def fetch_playlist(plid, locale) client = make_client(YT_URL)