]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/downloader/niconico.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / yt_dlp / downloader / niconico.py
index 7d8575c2a4c6af45d29bcc24058c3c499515a019..462c6e2d63ec276fa62166b26cbe032511eb90f9 100644 (file)
@@ -5,13 +5,8 @@
 from . import get_suitable_downloader
 from .common import FileDownloader
 from .external import FFmpegFD
-from ..utils import (
-    DownloadError,
-    WebSocketsWrapper,
-    sanitized_Request,
-    str_or_none,
-    try_get,
-)
+from ..networking import Request
+from ..utils import DownloadError, str_or_none, try_get
 
 
 class NiconicoDmcFD(FileDownloader):
@@ -20,7 +15,7 @@ class NiconicoDmcFD(FileDownloader):
     def real_download(self, filename, info_dict):
         from ..extractor.niconico import NiconicoIE
 
-        self.to_screen('[%s] Downloading from DMC' % self.FD_NAME)
+        self.to_screen(f'[{self.FD_NAME}] Downloading from DMC')
         ie = NiconicoIE(self.ydl)
         info_dict, heartbeat_info_dict = ie._get_heartbeat_info(info_dict)
 
@@ -33,13 +28,13 @@ def real_download(self, filename, info_dict):
         heartbeat_data = heartbeat_info_dict['data'].encode()
         heartbeat_interval = heartbeat_info_dict.get('interval', 30)
 
-        request = sanitized_Request(heartbeat_url, heartbeat_data)
+        request = Request(heartbeat_url, heartbeat_data)
 
         def heartbeat():
             try:
                 self.ydl.urlopen(request).read()
             except Exception:
-                self.to_screen('[%s] Heartbeat failed' % self.FD_NAME)
+                self.to_screen(f'[{self.FD_NAME}] Heartbeat failed')
 
             with heartbeat_lock:
                 if not download_complete:
@@ -69,7 +64,6 @@ def real_download(self, filename, info_dict):
         ws_url = info_dict['url']
         ws_extractor = info_dict['ws']
         ws_origin_host = info_dict['origin']
-        cookies = info_dict.get('cookies')
         live_quality = info_dict.get('live_quality', 'high')
         live_latency = info_dict.get('live_latency', 'high')
         dl = FFmpegFD(self.ydl, self.params or {})
@@ -81,12 +75,7 @@ def real_download(self, filename, info_dict):
 
         def communicate_ws(reconnect):
             if reconnect:
-                ws = WebSocketsWrapper(ws_url, {
-                    'Cookies': str_or_none(cookies) or '',
-                    'Origin': f'https://{ws_origin_host}',
-                    'Accept': '*/*',
-                    'User-Agent': self.params['http_headers']['User-Agent'],
-                })
+                ws = self.ydl.urlopen(Request(ws_url, headers={'Origin': f'https://{ws_origin_host}'}))
                 if self.ydl.params.get('verbose', False):
                     self.to_screen('[debug] Sending startWatching request')
                 ws.send(json.dumps({
@@ -96,14 +85,14 @@ def communicate_ws(reconnect):
                             'quality': live_quality,
                             'protocol': 'hls+fmp4',
                             'latency': live_latency,
-                            'chasePlay': False
+                            'chasePlay': False,
                         },
                         'room': {
                             'protocol': 'webSocket',
-                            'commentable': True
+                            'commentable': True,
                         },
                         'reconnect': True,
-                    }
+                    },
                 }))
             else:
                 ws = ws_extractor
@@ -129,7 +118,7 @@ def communicate_ws(reconnect):
                     elif self.ydl.params.get('verbose', False):
                         if len(recv) > 100:
                             recv = recv[:100] + '...'
-                        self.to_screen('[debug] Server said: %s' % recv)
+                        self.to_screen(f'[debug] Server said: {recv}')
 
         def ws_main():
             reconnect = False
@@ -139,7 +128,7 @@ def ws_main():
                     if ret is True:
                         return
                 except BaseException as e:
-                    self.to_screen('[%s] %s: Connection error occured, reconnecting after 10 seconds: %s' % ('niconico:live', video_id, str_or_none(e)))
+                    self.to_screen('[{}] {}: Connection error occured, reconnecting after 10 seconds: {}'.format('niconico:live', video_id, str_or_none(e)))
                     time.sleep(10)
                     continue
                 finally: