Add ability to automatically extract video signature funtion

pull/15/head
Omar Roth 7 years ago
parent e1ec1b6837
commit 0bb07e55f5

@ -173,7 +173,6 @@ video_threads.times do |i|
end end
top_videos = [] of Video top_videos = [] of Video
spawn do spawn do
if CONFIG.dl_api_key if CONFIG.dl_api_key
DetectLanguage.configure do |config| DetectLanguage.configure do |config|
@ -213,6 +212,20 @@ spawn do
end end
end end
# Refresh decrypt function
decrypt_function = [] of {name: String, value: Int32}
spawn do
loop do
begin
client = make_client(YT_URL)
decrypt_function = update_decrypt_function(client)
rescue ex
end
Fiber.yield
end
end
before_all do |env| before_all do |env|
if env.request.cookies.has_key? "SID" if env.request.cookies.has_key? "SID"
headers = HTTP::Headers.new headers = HTTP::Headers.new
@ -296,11 +309,11 @@ get "/watch" do |env|
if adaptive_fmts[0]? && adaptive_fmts[0]["s"]? if adaptive_fmts[0]? && adaptive_fmts[0]["s"]?
adaptive_fmts.each do |fmt| adaptive_fmts.each do |fmt|
fmt["url"] += "&signature=" + decrypt_signature(fmt["s"]) fmt["url"] += "&signature=" + decrypt_signature(fmt["s"], decrypt_function)
end end
fmt_stream.each do |fmt| fmt_stream.each do |fmt|
fmt["url"] += "&signature=" + decrypt_signature(fmt["s"]) fmt["url"] += "&signature=" + decrypt_signature(fmt["s"], decrypt_function)
end end
end end
@ -433,11 +446,11 @@ get "/embed/:id" do |env|
if adaptive_fmts[0]? && adaptive_fmts[0]["s"]? if adaptive_fmts[0]? && adaptive_fmts[0]["s"]?
adaptive_fmts.each do |fmt| adaptive_fmts.each do |fmt|
fmt["url"] += "&signature=" + decrypt_signature(fmt["s"]) fmt["url"] += "&signature=" + decrypt_signature(fmt["s"], decrypt_function)
end end
fmt_stream.each do |fmt| fmt_stream.each do |fmt|
fmt["url"] += "&signature=" + decrypt_signature(fmt["s"]) fmt["url"] += "&signature=" + decrypt_signature(fmt["s"], decrypt_function)
end end
end end
@ -1044,7 +1057,7 @@ get "/api/manifest/dash/id/:id" do |env|
if adaptive_fmts[0]? && adaptive_fmts[0]["s"]? if adaptive_fmts[0]? && adaptive_fmts[0]["s"]?
adaptive_fmts.each do |fmt| adaptive_fmts.each do |fmt|
fmt["url"] += "&signature=" + decrypt_signature(fmt["s"]) fmt["url"] += "&signature=" + decrypt_signature(fmt["s"], decrypt_function)
end end
end end

@ -324,16 +324,66 @@ def splice(a, b)
return a return a
end end
def decrypt_signature(a) def decrypt_signature(a, code)
a = a.split("") a = a.split("")
a.reverse! code.each do |item|
a.delete_at(0..0) case item[:name]
a = splice(a, 11) 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("") return a.join("")
end end
def update_decrypt_function(client)
# Video with signature
document = client.get("/watch?v=CvFH_6DNRCY").body
url = document.match(/src="(?<url>\/yts\/jsbin\/player-.{9}\/en_US\/base.js)"/).not_nil!["url"]
player = client.get(url).body
function_name = player.match(/\(b\|\|\(b="signature"\),d.set\(b,(?<name>[a-zA-Z]{2})\(c\)\)\)/).not_nil!["name"]
function_body = player.match(/#{function_name}=function\(a\){(?<body>[^}]+)}/).not_nil!["body"]
function_body = function_body.split(";")[1..-2]
var_name = function_body[0][0, 2]
operations = {} of String => String
matches = player.delete("\n").match(/var #{var_name}={((?<op1>[a-zA-Z]{2}:[^}]+})),((?<op2>[a-zA-Z]{2}:[^}]+})),((?<op3>[a-zA-Z]{2}:[^}]+}))};/).not_nil!
3.times do |i|
operation = matches["op#{i + 1}"]
op_name = operation[0, 2]
op_body = operation.match(/\{[^}]+\}/).not_nil![0]
case op_body
when "{a.reverse()}"
operations[op_name] = "a"
when "{a.splice(0,b)}"
operations[op_name] = "b"
else
operations[op_name] = "c"
end
end
decrypt_function = [] of {name: String, value: Int32}
function_body.each do |function|
function = function.lchop(var_name + ".")
op_name = function[0, 2]
function = function.lchop(op_name + "(a,")
value = function.rchop(")").to_i
decrypt_function << {name: operations[op_name], value: value}
end
return decrypt_function
end
def rank_videos(db, n, filter, url) def rank_videos(db, n, filter, url)
top = [] of {Float64, String} top = [] of {Float64, String}

Loading…
Cancel
Save