]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/extractor/huya.py
[ie/matchtv] Fix extractor (#10190)
[yt-dlp.git] / yt_dlp / extractor / huya.py
index b6e9eec24b0148c236c6df622076bb3237ae60bb..5663a78a3791e0a52026ca57e5fa66d963826f5d 100644 (file)
@@ -1,8 +1,10 @@
+import base64
 import hashlib
 import random
+import re
+import urllib.parse
 
-from ..compat import compat_urlparse, compat_b64decode
-
+from .common import InfoExtractor
 from ..utils import (
     ExtractorError,
     int_or_none,
@@ -12,8 +14,6 @@
     update_url_query,
 )
 
-from .common import InfoExtractor
-
 
 class HuyaLiveIE(InfoExtractor):
     _VALID_URL = r'https?://(?:www\.|m\.)?huya\.com/(?P<id>[^/#?&]+)(?:\D|$)'
@@ -33,11 +33,11 @@ class HuyaLiveIE(InfoExtractor):
         },
     }, {
         'url': 'https://www.huya.com/xiaoyugame',
-        'only_matching': True
+        'only_matching': True,
     }]
 
     _RESOLUTION = {
-        '蓝光4M': {
+        '蓝光': {
             'width': 1920,
             'height': 1080,
         },
@@ -47,8 +47,8 @@ class HuyaLiveIE(InfoExtractor):
         },
         '流畅': {
             'width': 800,
-            'height': 480
-        }
+            'height': 480,
+        },
     }
 
     def _real_extract(self, url):
@@ -71,16 +71,20 @@ def _real_extract(self, url):
                 continue
             stream_name = stream_info.get('sStreamName')
             re_secret = not screen_type and live_source_type in (0, 8, 13)
-            params = dict(compat_urlparse.parse_qsl(unescapeHTML(stream_info['sFlvAntiCode'])))
+            params = dict(urllib.parse.parse_qsl(unescapeHTML(stream_info['sFlvAntiCode'])))
             fm, ss = '', ''
             if re_secret:
                 fm, ss = self.encrypt(params, stream_info, stream_name)
             for si in stream_data.get('vMultiStreamInfo'):
+                display_name, bitrate = re.fullmatch(
+                    r'(.+?)(?:(\d+)M)?', si.get('sDisplayName')).groups()
                 rate = si.get('iBitRate')
                 if rate:
                     params['ratio'] = rate
                 else:
                     params.pop('ratio', None)
+                    if bitrate:
+                        rate = int(bitrate) * 1000
                 if re_secret:
                     params['wsSecret'] = hashlib.md5(
                         '_'.join([fm, params['u'], stream_name, ss, params['wsTime']]))
@@ -90,7 +94,7 @@ def _real_extract(self, url):
                     'tbr': rate,
                     'url': update_url_query(f'{stream_url}/{stream_name}.{stream_info.get("sFlvUrlSuffix")}',
                                             query=params),
-                    **self._RESOLUTION.get(si.get('sDisplayName'), {}),
+                    **self._RESOLUTION.get(display_name, {}),
                 })
 
         return {
@@ -124,6 +128,6 @@ def encrypt(self, params, stream_info, stream_name):
             'uuid': int_or_none(ct % 1e7 * 1e6 % 0xffffffff),
             't': '100',
         })
-        fm = compat_b64decode(params['fm']).decode().split('_', 1)[0]
+        fm = base64.b64decode(params['fm']).decode().split('_', 1)[0]
         ss = hashlib.md5('|'.join([params['seqid'], params['ctype'], params['t']]))
         return fm, ss