]> jfr.im git - yt-dlp.git/commitdiff
[cleanup] Use `random.choices` (#5800)
authorAnant Murmu <redacted>
Fri, 30 Dec 2022 02:43:49 +0000 (08:13 +0530)
committerGitHub <redacted>
Fri, 30 Dec 2022 02:43:49 +0000 (08:13 +0530)
Authored by: freezboltz

yt_dlp/YoutubeDL.py
yt_dlp/extractor/adn.py
yt_dlp/extractor/discovery.py
yt_dlp/extractor/funimation.py
yt_dlp/extractor/linuxacademy.py
yt_dlp/extractor/tencent.py
yt_dlp/extractor/tiktok.py
yt_dlp/extractor/videa.py
yt_dlp/extractor/viu.py
yt_dlp/extractor/vrv.py
yt_dlp/extractor/youku.py

index abb0ddfe522222c578f45cba4ae6cd9359e13506..17f37a643246034da20b044c3b988c7653df469f 100644 (file)
@@ -1068,7 +1068,7 @@ def _outtmpl_expandpath(outtmpl):
         # correspondingly that is not what we want since we need to keep
         # '%%' intact for template dict substitution step. Working around
         # with boundary-alike separator hack.
-        sep = ''.join([random.choice(ascii_letters) for _ in range(32)])
+        sep = ''.join(random.choices(ascii_letters, k=32))
         outtmpl = outtmpl.replace('%%', f'%{sep}%').replace('$$', f'${sep}$')
 
         # outtmpl should be expand_path'ed before template dict substitution
index e0c18c8773604560cfefcd08b0f69144bd154fce..f1f55e87fc9b6d54d408a21cf1ca468848476776 100644 (file)
@@ -168,7 +168,7 @@ def _real_extract(self, url):
             }, data=b'')['token']
 
         links_url = try_get(options, lambda x: x['video']['url']) or (video_base_url + 'link')
-        self._K = ''.join([random.choice('0123456789abcdef') for _ in range(16)])
+        self._K = ''.join(random.choices('0123456789abcdef', k=16))
         message = bytes_to_intlist(json.dumps({
             'k': self._K,
             't': token,
index fd3fc8fb0f5bac33be4648317cd05d8a8cf3adc9..e6e109d5c54c3fb6b9390243b8f951b343921055 100644 (file)
@@ -78,7 +78,7 @@ def _real_extract(self, url):
                 'Downloading token JSON metadata', query={
                     'authRel': 'authorization',
                     'client_id': '3020a40c2356a645b4b4',
-                    'nonce': ''.join([random.choice(string.ascii_letters) for _ in range(32)]),
+                    'nonce': ''.join(random.choices(string.ascii_letters, k=32)),
                     'redirectUri': 'https://www.discovery.com/',
                 })['access_token']
 
index 18363c1b917438ecbe280dfa0e2ac7f87b9b5688..47c316664ad89ce291f2aa351602a67c3c48e37c 100644 (file)
@@ -210,7 +210,7 @@ def _real_extract(self, url):
             page = self._download_json(
                 'https://www.funimation.com/api/showexperience/%s/' % experience_id,
                 display_id, headers=headers, expected_status=403, query={
-                    'pinst_id': ''.join([random.choice(string.digits + string.ascii_letters) for _ in range(8)]),
+                    'pinst_id': ''.join(random.choices(string.digits + string.ascii_letters, k=8)),
                 }, note=f'Downloading {format_name} JSON')
             sources = page.get('items') or []
             if not sources:
index a570248b7a94b56a6ab333d3b893b41dd172b5cb..7bb64e17c45026b19b46bf80ae049ffd04c807c4 100644 (file)
@@ -75,9 +75,8 @@ class LinuxAcademyIE(InfoExtractor):
 
     def _perform_login(self, username, password):
         def random_string():
-            return ''.join([
-                random.choice('0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._~')
-                for _ in range(32)])
+            return ''.join(random.choices(
+                '0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._~', k=32))
 
         webpage, urlh = self._download_webpage_handle(
             self._AUTHORIZE_URL, None, 'Downloading authorize page', query={
index ff8bf991efd68d5392eb9ee8ab8d77c70758931c..44cae047205cad38103e1e59fb8d6c11d1d28f88 100644 (file)
@@ -32,7 +32,7 @@ def _get_ckey(self, video_id, url, guid):
             padding_mode='whitespace').hex().upper()
 
     def _get_video_api_response(self, video_url, video_id, series_id, subtitle_format, video_format, video_quality):
-        guid = ''.join([random.choice(string.digits + string.ascii_lowercase) for _ in range(16)])
+        guid = ''.join(random.choices(string.digits + string.ascii_lowercase, k=16))
         ckey = self._get_ckey(video_id, video_url, guid)
         query = {
             'vid': video_id,
@@ -55,7 +55,7 @@ def _get_video_api_response(self, video_url, video_id, series_id, subtitle_forma
             'platform': self._PLATFORM,
             # For VQQ
             'guid': guid,
-            'flowid': ''.join(random.choice(string.digits + string.ascii_lowercase) for _ in range(32)),
+            'flowid': ''.join(random.choices(string.digits + string.ascii_lowercase, k=32)),
         }
 
         return self._search_json(r'QZOutputJson=', self._download_webpage(
index 2dd4510cc3893b2e4c5899efbd495e60b460c8e9..709d944dc6f6fed4d303dea6222a691a4ce52140 100644 (file)
@@ -49,7 +49,7 @@ def _get_sigi_state(self, webpage, display_id):
 
     def _call_api_impl(self, ep, query, manifest_app_version, video_id, fatal=True,
                        note='Downloading API JSON', errnote='Unable to download API page'):
-        self._set_cookie(self._API_HOSTNAME, 'odin_tt', ''.join(random.choice('0123456789abcdef') for _ in range(160)))
+        self._set_cookie(self._API_HOSTNAME, 'odin_tt', ''.join(random.choices('0123456789abcdef', k=160)))
         webpage_cookies = self._get_cookies(self._WEBPAGE_HOST)
         if webpage_cookies.get('sid_tt'):
             self._set_cookie(self._API_HOSTNAME, 'sid_tt', webpage_cookies['sid_tt'].value)
@@ -68,8 +68,8 @@ def _build_api_query(self, query, app_version, manifest_app_version):
             'build_number': app_version,
             'manifest_version_code': manifest_app_version,
             'update_version_code': manifest_app_version,
-            'openudid': ''.join(random.choice('0123456789abcdef') for _ in range(16)),
-            'uuid': ''.join([random.choice(string.digits) for _ in range(16)]),
+            'openudid': ''.join(random.choices('0123456789abcdef', k=16)),
+            'uuid': ''.join(random.choices(string.digits, k=16)),
             '_rticket': int(time.time() * 1000),
             'ts': int(time.time()),
             'device_brand': 'Google',
@@ -638,7 +638,7 @@ def _video_entries_api(self, webpage, user_id, username):
             'max_cursor': 0,
             'min_cursor': 0,
             'retry_type': 'no_retry',
-            'device_id': ''.join(random.choice(string.digits) for _ in range(19)),  # Some endpoints don't like randomized device_id, so it isn't directly set in _call_api.
+            'device_id': ''.join(random.choices(string.digits, k=19)),  # Some endpoints don't like randomized device_id, so it isn't directly set in _call_api.
         }
 
         for page in itertools.count(1):
@@ -686,7 +686,7 @@ def _entries(self, list_id, display_id):
             'cursor': 0,
             'count': 20,
             'type': 5,
-            'device_id': ''.join(random.choice(string.digits) for i in range(19))
+            'device_id': ''.join(random.choices(string.digits, k=19))
         }
 
         for page in itertools.count(1):
index 52fa8fcec2ab5909ad78483d3072fdb3d74ce54e..59ae933b08a8a18d652c4e9e6cf041933dd27b09 100644 (file)
@@ -119,7 +119,7 @@ def _real_extract(self, url):
             result += s[i - (self._STATIC_SECRET.index(l[i]) - 31)]
 
         query = parse_qs(player_url)
-        random_seed = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(8))
+        random_seed = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
         query['_s'] = random_seed
         query['_t'] = result[:16]
 
index 19d48234e444a1df4ac8e207ddf8f5664ecaf18e..dd4cad7ba80bccd95a86d72e9eb8f0513b02d2cb 100644 (file)
@@ -251,7 +251,7 @@ def _login(self, country_code, video_id):
         return self._user_token
 
     def _get_token(self, country_code, video_id):
-        rand = ''.join(random.choice('0123456789') for _ in range(10))
+        rand = ''.join(random.choices('0123456789', k=10))
         return self._download_json(
             f'https://api-gateway-global.viu.com/api/auth/token?v={rand}000', video_id,
             headers={'Content-Type': 'application/json'}, note='Getting bearer token',
index 89fa7affc260d1e82fc3298aea599eb0067f6e2b..ad9dc568a637dfe2c8552af3c6ee14295696f284 100644 (file)
@@ -30,7 +30,7 @@ def _call_api(self, path, video_id, note, data=None):
         base_url = self._API_DOMAIN + '/core/' + path
         query = [
             ('oauth_consumer_key', self._API_PARAMS['oAuthKey']),
-            ('oauth_nonce', ''.join([random.choice(string.ascii_letters) for _ in range(32)])),
+            ('oauth_nonce', ''.join(random.choices(string.ascii_letters, k=32))),
             ('oauth_signature_method', 'HMAC-SHA1'),
             ('oauth_timestamp', int(time.time())),
         ]
index ab59200d79931f2240b785687701b80b352cacd1..404f196f46fc5486890d52874af292151d2d5e03 100644 (file)
@@ -129,8 +129,8 @@ class YoukuIE(InfoExtractor):
 
     @staticmethod
     def get_ysuid():
-        return '%d%s' % (int(time.time()), ''.join([
-            random.choice(string.ascii_letters) for i in range(3)]))
+        return '%d%s' % (int(time.time()), ''.join(
+            random.choices(string.ascii_letters, k=3)))
 
     def get_format_name(self, fm):
         _dict = {