[vlc-commits] youtube.lua: helper to extract signature descrambling javascript code

Pierre Ynard git at videolan.org
Sun Nov 22 21:35:40 CET 2015


vlc/vlc-2.2 | branch: master | Pierre Ynard <linkfanel at yahoo.fr> | Wed Nov 11 16:14:21 2015 +0100| [bd908d93cac78623e82a4d8cdfbf99d8b19e2edb] | committer: Jean-Baptiste Kempf

youtube.lua: helper to extract signature descrambling javascript code

(cherry picked from commit 8074c41dc022bda1170fe4c4084bd37f17ddbba2)
Signed-off-by: Jean-Baptiste Kempf <jb at videolan.org>

> http://git.videolan.org/gitweb.cgi/vlc/vlc-2.2.git/?a=commit;h=bd908d93cac78623e82a4d8cdfbf99d8b19e2edb
---

 share/lua/playlist/youtube.lua |   33 ++++++++++++++++++---------------
 1 file changed, 18 insertions(+), 15 deletions(-)

diff --git a/share/lua/playlist/youtube.lua b/share/lua/playlist/youtube.lua
index b2c1060..b0bdb52 100644
--- a/share/lua/playlist/youtube.lua
+++ b/share/lua/playlist/youtube.lua
@@ -81,24 +81,33 @@ function buf_iter( s )
     return line
 end
 
+-- Helper to search and extract code from javascript stream
+function js_extract( js, pattern )
+    js.i = 0 -- Reset to beginning
+    for line in buf_iter, js do
+        local ex = string.match( line, pattern )
+        if ex then
+            return ex
+        end
+    end
+    vlc.msg.err( "Couldn't process youtube video URL, please check for updates to this script" )
+    return nil
+end
+
 -- Descramble the URL signature using the javascript code that does that
 -- in the web page
 function js_descramble( sig, js_url )
     -- Fetch javascript code
     local js = { stream = vlc.stream( js_url ), lines = {}, i = 0 }
     if not js.stream then
+        vlc.msg.err( "Couldn't process youtube video URL, please check for updates to this script" )
         return sig
     end
 
     -- Look for the descrambler function's name
-    local descrambler = nil
-    for line in buf_iter, js do
-        -- c&&a.set("signature",br(c));
-        descrambler = string.match( line, "%.set%(\"signature\",(.-)%(" )
-        if descrambler then break end
-    end
+    -- c&&a.set("signature",br(c));
+    local descrambler = js_extract( js, "%.set%(\"signature\",(.-)%(" )
     if not descrambler then
-        vlc.msg.err( "Couldn't process youtube video URL, please check for updates to this script" )
         return sig
     end
 
@@ -106,15 +115,9 @@ function js_descramble( sig, js_url )
     -- conveniently preceded by the definition of a helper object
     -- that it uses. Example:
     -- var Fo={TR:function(a){a.reverse()},TU:function(a,b){var c=a[0];a[0]=a[b%a.length];a[b]=c},sH:function(a,b){a.splice(0,b)}};function Go(a){a=a.split("");Fo.sH(a,2);Fo.TU(a,28);Fo.TU(a,44);Fo.TU(a,26);Fo.TU(a,40);Fo.TU(a,64);Fo.TR(a,26);Fo.sH(a,1);return a.join("")};
-    local transformations = nil
-    local rules = nil
-    js.i = 0 -- Reset to beginning
-    for line in buf_iter, js do
-        transformations, rules = string.match( line, "var ..={(.-)};function "..descrambler.."%([^)]*%){(.-)}" )
-        if transformations or rules then break end
-    end
+    local rules = js_extract( js, "function "..descrambler.."%([^)]*%){(.-)}" )
+    local transformations = js_extract( js, "var ..={(.-)};function "..descrambler.."%(" )
     if not transformations or not rules then
-        vlc.msg.err( "Couldn't process youtube video URL, please check for updates to this script" )
         return sig
     end
 



More information about the vlc-commits mailing list