[utils] Move codes for handling eval() from iqiyi.py
authorYen Chi Hsuan <yan12125@gmail.com>
Fri, 26 Feb 2016 06:58:29 +0000 (14:58 +0800)
committerYen Chi Hsuan <yan12125@gmail.com>
Fri, 26 Feb 2016 06:58:29 +0000 (14:58 +0800)
youtube_dl/extractor/iqiyi.py
youtube_dl/utils.py

index 76ecd55a4a9ad0859d09c48a3d6f20dd088f8785..2b395221077d5f2243f8372167591229a9fa287b 100644 (file)
@@ -18,7 +18,7 @@ from ..compat import (
     compat_urllib_parse_urlparse,
 )
 from ..utils import (
-    base_n,
+    decode_packed_codes,
     ExtractorError,
     ohdave_rsa_encrypt,
     remove_start,
@@ -130,28 +130,8 @@ class IqiyiSDKInterpreter(object):
     def __init__(self, sdk_code):
         self.sdk_code = sdk_code
 
-    def decode_eval_codes(self):
-        self.sdk_code = self.sdk_code[5:-3]
-
-        mobj = re.search(
-            r"'([^']+)',62,(\d+),'([^']+)'\.split\('\|'\),[^,]+,{}",
-            self.sdk_code)
-        obfucasted_code, count, symbols = mobj.groups()
-        count = int(count)
-        symbols = symbols.split('|')
-        symbol_table = {}
-
-        while count:
-            count -= 1
-            b62count = base_n(count, 62)
-            symbol_table[b62count] = symbols[count] or b62count
-
-        self.sdk_code = re.sub(
-            r'\b(\w+)\b', lambda mobj: symbol_table[mobj.group(0)],
-            obfucasted_code)
-
     def run(self, target, ip, timestamp):
-        self.decode_eval_codes()
+        self.sdk_code = decode_packed_codes(self.sdk_code)
 
         functions = re.findall(r'input=([a-zA-Z0-9]+)\(input', self.sdk_code)
 
index 900e07a8ec74f529e7bca0eec8b6bafb39ff5faf..fc7e2fb7f11620bd07c5ba90427170ceedd1a3a3 100644 (file)
@@ -2635,3 +2635,23 @@ def base_n(num, n, table=None):
         ret = table[num % n] + ret
         num = num // n
     return ret
+
+
+def decode_packed_codes(code):
+    mobj = re.search(
+        r"'([^']+)',(\d+),(\d+),'([^']+)'\.split\('\|'\),[^,]+,{}",
+        code)
+    obfucasted_code, base, count, symbols = mobj.groups()
+    base = int(base)
+    count = int(count)
+    symbols = symbols.split('|')
+    symbol_table = {}
+
+    while count:
+        count -= 1
+        base_n_count = base_n(count, base)
+        symbol_table[base_n_count] = symbols[count] or base_n_count
+
+    return re.sub(
+        r'\b(\w+)\b', lambda mobj: symbol_table[mobj.group(0)],
+        obfucasted_code)