[ie/niconico] Move WebSocket headers to info_dict

Thanks for coletdjnz's suggestion!
This commit is contained in:
Mozi 2024-03-11 15:22:07 +00:00
parent 16974726a4
commit 41c6125907
2 changed files with 4 additions and 6 deletions

View File

@ -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({

View File

@ -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)
@ -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)
@ -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,
}