[utils] Merge base_n functions
authorYen Chi Hsuan <yan12125@gmail.com>
Fri, 26 Feb 2016 06:37:20 +0000 (14:37 +0800)
committerYen Chi Hsuan <yan12125@gmail.com>
Fri, 26 Feb 2016 06:37:20 +0000 (14:37 +0800)
youtube_dl/extractor/iqiyi.py
youtube_dl/extractor/vidzi.py
youtube_dl/utils.py

index 4f02b9f87e868443b1a33f6030b35eef3ff5292a..76ecd55a4a9ad0859d09c48a3d6f20dd088f8785 100644 (file)
@@ -18,7 +18,7 @@ from ..compat import (
     compat_urllib_parse_urlparse,
 )
 from ..utils import (
-    base62,
+    base_n,
     ExtractorError,
     ohdave_rsa_encrypt,
     remove_start,
@@ -143,7 +143,7 @@ class IqiyiSDKInterpreter(object):
 
         while count:
             count -= 1
-            b62count = base62(count)
+            b62count = base_n(count, 62)
             symbol_table[b62count] = symbols[count] or b62count
 
         self.sdk_code = re.sub(
index 4ec07db3a9fbfa468bb79f52c0e964d769dbf152..d671e27decf973bb315bc306f94d89d4774e7ff2 100644 (file)
@@ -5,7 +5,7 @@ import re
 
 from .jwplatform import JWPlatformBaseIE
 from ..utils import (
-    base36,
+    base_n,
     js_to_json,
 )
 
@@ -42,7 +42,7 @@ class VidziIE(JWPlatformBaseIE):
         while count:
             count -= 1
             if symbols[count]:
-                code = re.sub(r'\b%s\b' % base36(count), symbols[count], code)
+                code = re.sub(r'\b%s\b' % base_n(count, 36), symbols[count], code)
 
         code = code.replace('\\\'', '\'')
         jwplayer_data = self._parse_json(
index be1f3b0d77a55a3245b5cff44982256e072d6c0e..900e07a8ec74f529e7bca0eec8b6bafb39ff5faf 100644 (file)
@@ -2621,19 +2621,17 @@ def ohdave_rsa_encrypt(data, exponent, modulus):
     return '%x' % encrypted
 
 
-def base_n(num, n, table):
+def base_n(num, n, table=None):
     if num == 0:
         return '0'
+
+    FULL_TABLE = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
+    assert n <= len(FULL_TABLE)
+    if not table:
+        table = FULL_TABLE[:n]
+
     ret = ''
     while num:
         ret = table[num % n] + ret
         num = num // n
     return ret
-
-
-def base36(num):
-    return base_n(num, 36, '0123456789abcdefghijklmnopqrstuvwxyz')
-
-
-def base62(num):
-    return base_n(num, 62, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')