Last active
September 14, 2016 13:48
-
-
Save kl/9672430 to your computer and use it in GitHub Desktop.
test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
def decipher_function_name_pattern | |
# Find "C" in this: var A = B.sig || C (B.s) | |
/ | |
\.sig | |
\s* | |
\|\| | |
(\w+) | |
\( | |
/x | |
end | |
def function_body_pattern | |
# Match nested angle braces | |
/ | |
(?<brace> | |
{ | |
( | |
[^{}] | |
| \g<brace> | |
)* | |
} | |
) | |
/x | |
end | |
def function_pattern(function_name) | |
# Match the function function_name (that has one argument) | |
/ | |
#{function_name} | |
\( | |
\w+ | |
\) | |
#{function_body_pattern} | |
/x | |
end | |
js = open("http://s.ytimg.com/yts/jsbin/html5player-#{ARGV.first}.js").read | |
deciph_func_name = js[decipher_function_name_pattern, 1] | |
deciph_func_pattern = function_pattern(deciph_func_name) | |
body_match = deciph_func_pattern.match(js) | |
raise "could not extract body" unless body_match | |
body = body_match[:brace] | |
lines = body.split(";") | |
# The first line splits the string into an array and the last joins and returns | |
lines.delete_at(0) | |
lines.delete_at(lines.size - 1) | |
lines.map! do |line| | |
if /\(\w+,(?<index>\d+)\)/ =~ line # calling a two argument function (swap) | |
"w#{index}" | |
elsif /slice\((?<index>\d+)\)/ =~ line # calling slice | |
"s#{index}" | |
elsif /reverse\(\)/ =~ line # calling reverse | |
"r" | |
else | |
raise "Cannot parse line: #{line}" | |
end | |
end | |
cipher = lines.join(" ") | |
puts cipher |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment