Skip to content

Commit

Permalink
[ie/niconico] Move WebSocket headers to info_dict
Browse files Browse the repository at this point in the history
Thanks for coletdjnz's suggestion!
  • Loading branch information
pzhlkj6612 committed Mar 11, 2024
1 parent 1697472 commit 41c6125
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
3 changes: 1 addition & 2 deletions yt_dlp/downloader/niconico.py
Expand Up @@ -84,8 +84,7 @@ def _ws_context(self, info_dict):

def communicate_ws(reconnect):
if reconnect:
self.ws = self.ydl.urlopen(Request(
self.ws.url, headers={'Origin': self.ws.wsw.request.headers['Origin']}))
self.ws = self.ydl.urlopen(Request(self.ws.url, headers=info_dict.get('http_headers')))
if self.ydl.params.get('verbose', False):
self.to_screen('[debug] Sending startWatching request')
self.ws.send(json.dumps({
Expand Down
7 changes: 3 additions & 4 deletions yt_dlp/extractor/niconico.py
Expand Up @@ -1009,6 +1009,7 @@ def _yield_formats(self, ws, video_id, latency, is_live):
def _real_extract(self, url):
video_id = self._match_id(url)
webpage, urlh = self._download_webpage_handle(f'https://live.nicovideo.jp/watch/{video_id}', video_id)
headers = {'Origin': 'https://' + remove_start(urlparse(urlh.url).hostname, 'sp.')}

embedded_data = self._parse_json(unescapeHTML(self._search_regex(
r'<script\s+id="embedded-data"\s*data-props="(.+?)"', webpage, 'embedded data')), video_id)
Expand All @@ -1019,11 +1020,8 @@ def _real_extract(self, url):
ws_url = update_url_query(ws_url, {
'frontend_id': traverse_obj(embedded_data, ('site', 'frontendId')) or '9',
})
hostname = remove_start(urlparse(urlh.url).hostname, 'sp.')

ws = self._request_webpage(
Request(ws_url, headers={'Origin': f'https://{hostname}'}),
video_id=video_id, note='Connecting to WebSocket server')
Request(ws_url, headers=headers), video_id, note='Connecting to WebSocket server')
else:
self.raise_no_formats('The live hasn\'t started yet or already ended.', expected=True)

Expand Down Expand Up @@ -1078,5 +1076,6 @@ def _real_extract(self, url):
'thumbnails': thumbnails,
'formats': [*self._yield_formats(ws, video_id, latency, live_status == 'is_live')] if ws else None,
'live_latency': latency,
'http_headers': headers,
'__ws': ws,
}

0 comments on commit 41c6125

Please sign in to comment.