diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/invidious.cr | 32 | ||||
| -rw-r--r-- | src/invidious/channels.cr | 13 | ||||
| -rw-r--r-- | src/invidious/helpers/signatures.cr | 54 | ||||
| -rw-r--r-- | src/invidious/videos.cr | 5 |
4 files changed, 44 insertions, 60 deletions
diff --git a/src/invidious.cr b/src/invidious.cr index 5d011a5c..f5fe4b3d 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -207,7 +207,7 @@ spawn do end end -decrypt_function = [] of {name: String, value: Int32} +decrypt_function = [] of {SigProc, Int32} spawn do update_decrypt_function do |function| decrypt_function = function @@ -2602,13 +2602,9 @@ post "/data_control" do |env| next match["channel"] elsif match = channel["url"].as_s.match(/\/user\/(?<user>.+)/) response = YT_POOL.client &.get("/user/#{match["user"]}?disable_polymer=1&hl=en&gl=US") - document = XML.parse_html(response.body) - canonical = document.xpath_node(%q(//link[@rel="canonical"])) - - if canonical - ucid = canonical["href"].split("/")[-1] - next ucid - end + html = XML.parse_html(response.body) + ucid = html.xpath_node(%q(//link[@rel="canonical"])).try &.["href"].split("/")[-1] + next ucid if ucid end nil @@ -3841,10 +3837,10 @@ get "/api/v1/captions/:id" do |env| env.response.content_type = "text/vtt; charset=UTF-8" - caption = captions.select { |caption| caption.name.simpleText == label } - if lang caption = captions.select { |caption| caption.languageCode == lang } + else + caption = captions.select { |caption| caption.name.simpleText == label } end if caption.empty? @@ -5155,7 +5151,7 @@ get "/api/manifest/dash/id/:id" do |env| # Since some implementations create playlists based on resolution regardless of different codecs, # we can opt to only add a source to a representation if it has a unique height within that representation - unique_res = env.params.query["unique_res"]? && (env.params.query["unique_res"] == "true" || env.params.query["unique_res"] == "1") + unique_res = env.params.query["unique_res"]?.try { |q| (q == "true" || q == "1").to_unsafe } begin video = get_video(id, PG_DB, region: region) @@ -5167,7 +5163,7 @@ get "/api/manifest/dash/id/:id" do |env| end if dashmpd = video.player_response["streamingData"]?.try &.["dashManifestUrl"]?.try &.as_s - manifest = YT_POOL.client &.get(dashmpd).body + manifest = YT_POOL.client &.get(URI.parse(dashmpd).full_path).body manifest = manifest.gsub(/<BaseURL>[^<]+<\/BaseURL>/) do |baseurl| url = baseurl.lchop("<BaseURL>") @@ -5192,7 +5188,7 @@ get "/api/manifest/dash/id/:id" do |env| end audio_streams = video.audio_streams(adaptive_fmts) - video_streams = video.video_streams(adaptive_fmts).sort_by { |stream| stream["fps"].to_i }.reverse + video_streams = video.video_streams(adaptive_fmts).sort_by { |stream| {stream["size"].split("x")[0].to_i, stream["fps"].to_i} }.reverse XML.build(indent: " ", encoding: "UTF-8") do |xml| xml.element("MPD", "xmlns": "urn:mpeg:dash:schema:mpd:2011", @@ -5230,9 +5226,7 @@ get "/api/manifest/dash/id/:id" do |env| {"video/mp4", "video/webm"}.each do |mime_type| mime_streams = video_streams.select { |stream| stream["type"].starts_with? mime_type } - if mime_streams.empty? - next - end + next if mime_streams.empty? heights = [] of Int32 xml.element("AdaptationSet", id: i, mimeType: mime_type, startWithSAP: 1, subsegmentAlignment: true, scanType: "progressive") do @@ -5875,7 +5869,7 @@ error 404 do |env| response = YT_POOL.client &.get("/#{item}") if response.status_code == 301 - response = YT_POOL.client &.get(response.headers["Location"]) + response = YT_POOL.client &.get(URI.parse(response.headers["Location"]).full_path) end if response.body.empty? @@ -5884,10 +5878,10 @@ error 404 do |env| end html = XML.parse_html(response.body) - ucid = html.xpath_node(%q(//meta[@itemprop="channelId"])) + ucid = html.xpath_node(%q(//link[@rel="canonical"])).try &.["href"].split("/")[-1] if ucid - env.response.headers["Location"] = "/channel/#{ucid["content"]}" + env.response.headers["Location"] = "/channel/#{ucid}" halt env, status_code: 302 end diff --git a/src/invidious/channels.cr b/src/invidious/channels.cr index 433fe074..7cd1bef1 100644 --- a/src/invidious/channels.cr +++ b/src/invidious/channels.cr @@ -533,8 +533,17 @@ def extract_channel_playlists_cursor(url, auto_generated) .try { |i| Base64.decode(i) } .try { |i| IO::Memory.new(i) } .try { |i| Protodec::Any.parse(i) } - .try { |i| i["80226972:0:embedded"]["3:1:base64"].as_h.find { |k, v| k.starts_with?("15:") } } - .try &.[1].as_s || "" + .try { |i| i["80226972:0:embedded"]["3:1:base64"].as_h.find { |k, v| k.starts_with? "15:" } } + .try &.[1] + + if cursor.try &.as_h? + cursor = cursor.try { |i| Protodec::Any.cast_json(i.as_h) } + .try { |i| Protodec::Any.from_json(i) } + .try { |i| Base64.urlsafe_encode(i) } + .try { |i| URI.encode_www_form(i) } || "" + else + cursor = cursor.try &.as_s || "" + end if !auto_generated cursor = URI.decode_www_form(cursor) diff --git a/src/invidious/helpers/signatures.cr b/src/invidious/helpers/signatures.cr index 1d238576..ab864f03 100644 --- a/src/invidious/helpers/signatures.cr +++ b/src/invidious/helpers/signatures.cr @@ -1,69 +1,53 @@ +alias SigProc = Proc(Array(String), Int32, Array(String)) + def fetch_decrypt_function(id = "CvFH_6DNRCY") document = YT_POOL.client &.get("/watch?v=#{id}&gl=US&hl=en&disable_polymer=1").body - url = document.match(/src="(?<url>\/yts\/jsbin\/player_ias-.{9}\/en_US\/base.js)"/).not_nil!["url"] + url = document.match(/src="(?<url>\/yts\/jsbin\/player_ias-[^\/]+\/en_US\/base.js)"/).not_nil!["url"] player = YT_POOL.client &.get(url).body - function_name = player.match(/^(?<name>[^=]+)=function\(a\){a=a\.split\(""\)/m).not_nil!["name"] - function_body = player.match(/^#{Regex.escape(function_name)}=function\(a\){(?<body>[^}]+)}/m).not_nil!["body"] + function_name = player.match(/^(?<name>[^=]+)=function\(\w\){\w=\w\.split\(""\);[^\. ]+\.[^( ]+/m).not_nil!["name"] + function_body = player.match(/^#{Regex.escape(function_name)}=function\(\w\){(?<body>[^}]+)}/m).not_nil!["body"] function_body = function_body.split(";")[1..-2] var_name = function_body[0][0, 2] var_body = player.delete("\n").match(/var #{Regex.escape(var_name)}={(?<body>(.*?))};/).not_nil!["body"] - operations = {} of String => String + operations = {} of String => SigProc var_body.split("},").each do |operation| op_name = operation.match(/^[^:]+/).not_nil![0] op_body = operation.match(/\{[^}]+/).not_nil![0] case op_body when "{a.reverse()" - operations[op_name] = "a" + operations[op_name] = ->(a : Array(String), b : Int32) { a.reverse } when "{a.splice(0,b)" - operations[op_name] = "b" + operations[op_name] = ->(a : Array(String), b : Int32) { a.delete_at(0..(b - 1)); a } else - operations[op_name] = "c" + operations[op_name] = ->(a : Array(String), b : Int32) { c = a[0]; a[0] = a[b % a.size]; a[b % a.size] = c; a } end end - decrypt_function = [] of {name: String, value: Int32} + decrypt_function = [] of {SigProc, Int32} function_body.each do |function| function = function.lchop(var_name).delete("[].") op_name = function.match(/[^\(]+/).not_nil![0] - value = function.match(/\(a,(?<value>[\d]+)\)/).not_nil!["value"].to_i + value = function.match(/\(\w,(?<value>[\d]+)\)/).not_nil!["value"].to_i - decrypt_function << {name: operations[op_name], value: value} + decrypt_function << {operations[op_name], value} end return decrypt_function end -def decrypt_signature(fmt, code) - if !fmt["s"]? - return "" - end - - a = fmt["s"] - a = a.split("") +def decrypt_signature(fmt, op) + return "" if !fmt["s"]? || !fmt["sp"]? - code.each do |item| - case item[:name] - when "a" - a.reverse! - when "b" - a.delete_at(0..(item[:value] - 1)) - when "c" - a = splice(a, item[:value]) - end + sp = fmt["sp"] + sig = fmt["s"].split("") + op.each do |proc, value| + sig = proc.call(sig, value) end - signature = a.join("") - return "&#{fmt["sp"]?}=#{signature}" -end - -def splice(a, b) - c = a[0] - a[0] = a[b % a.size] - a[b % a.size] = c - return a + return "&#{sp}=#{sig.join("")}" end diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr index e9aee092..1c7599f8 100644 --- a/src/invidious/videos.cr +++ b/src/invidious/videos.cr @@ -621,10 +621,7 @@ struct Video if fmts = player_response["streamingData"]?.try &.["adaptiveFormats"]? fmts.as_a.each do |adaptive_fmt| - if !adaptive_fmt.as_h? - next - end - + next if !adaptive_fmt.as_h? fmt = {} of String => String if init = adaptive_fmt["initRange"]? |
