invidious-copy-2022-04-11/src/invidious/signatures.cr

65 lines
1.7 KiB
Crystal
Raw Normal View History

2018-09-15 00:50:11 +00:00
def fetch_decrypt_function(id = "CvFH_6DNRCY")
client = make_client(YT_URL)
2018-08-04 20:30:44 +00:00
document = client.get("/watch?v=#{id}").body
url = document.match(/src="(?<url>\/yts\/jsbin\/player-.{9}\/en_US\/base.js)"/).not_nil!["url"]
player = client.get(url).body
2018-09-07 20:52:46 +00:00
function_name = player.match(/^(?<name>[^=]+)=function\(a\){a=a\.split\(""\)/m).not_nil!["name"]
2018-08-28 14:51:59 +00:00
function_body = player.match(/^#{function_name}=function\(a\){(?<body>[^}]+)}/m).not_nil!["body"]
2018-08-04 20:30:44 +00:00
function_body = function_body.split(";")[1..-2]
var_name = function_body[0][0, 2]
2018-08-28 14:51:59 +00:00
var_body = player.delete("\n").match(/var #{var_name}={(?<body>(.*?))};/).not_nil!["body"]
2018-08-04 20:30:44 +00:00
operations = {} of String => String
2018-08-28 14:51:59 +00:00
var_body.split("},").each do |operation|
op_name = operation.match(/^[^:]+/).not_nil![0]
op_body = operation.match(/\{[^}]+/).not_nil![0]
2018-08-04 20:30:44 +00:00
case op_body
2018-08-28 14:51:59 +00:00
when "{a.reverse()"
2018-08-04 20:30:44 +00:00
operations[op_name] = "a"
2018-08-28 14:51:59 +00:00
when "{a.splice(0,b)"
2018-08-04 20:30:44 +00:00
operations[op_name] = "b"
else
operations[op_name] = "c"
end
end
decrypt_function = [] of {name: String, value: Int32}
function_body.each do |function|
2018-08-28 14:51:59 +00:00
function = function.lchop(var_name).delete("[].")
2018-08-04 20:30:44 +00:00
2018-08-28 14:51:59 +00:00
op_name = function.match(/[^\(]+/).not_nil![0]
value = function.match(/\(a,(?<value>[\d]+)\)/).not_nil!["value"].to_i
2018-08-04 20:30:44 +00:00
decrypt_function << {name: operations[op_name], value: value}
end
return decrypt_function
end
def decrypt_signature(a, code)
a = a.split("")
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
end
return a.join("")
end
def splice(a, b)
c = a[0]
a[0] = a[b % a.size]
a[b % a.size] = c
return a
end