2023-05-29 03:35:10 -06:00
|
|
|
import json
|
2021-02-09 23:45:20 -07:00
|
|
|
import threading
|
2023-05-29 03:35:10 -06:00
|
|
|
import time
|
2021-02-09 23:45:20 -07:00
|
|
|
|
2022-04-17 11:18:50 -06:00
|
|
|
from . import get_suitable_downloader
|
2021-02-09 23:45:20 -07:00
|
|
|
from .common import FileDownloader
|
2023-05-29 03:35:10 -06:00
|
|
|
from .external import FFmpegFD
|
2023-07-09 01:53:02 -06:00
|
|
|
from ..networking import Request
|
2023-11-20 01:04:04 -07:00
|
|
|
from ..utils import DownloadError, str_or_none, try_get
|
2021-02-09 23:45:20 -07:00
|
|
|
|
|
|
|
|
|
|
|
class NiconicoDmcFD(FileDownloader):
|
|
|
|
""" Downloading niconico douga from DMC with heartbeat """
|
|
|
|
|
|
|
|
def real_download(self, filename, info_dict):
|
2022-04-17 11:18:50 -06:00
|
|
|
from ..extractor.niconico import NiconicoIE
|
2021-02-09 23:45:20 -07:00
|
|
|
|
2024-06-11 17:09:58 -06:00
|
|
|
self.to_screen(f'[{self.FD_NAME}] Downloading from DMC')
|
2021-02-09 23:45:20 -07:00
|
|
|
ie = NiconicoIE(self.ydl)
|
|
|
|
info_dict, heartbeat_info_dict = ie._get_heartbeat_info(info_dict)
|
|
|
|
|
2021-07-31 04:51:01 -06:00
|
|
|
fd = get_suitable_downloader(info_dict, params=self.params)(self.ydl, self.params)
|
2021-02-09 23:45:20 -07:00
|
|
|
|
|
|
|
success = download_complete = False
|
|
|
|
timer = [None]
|
|
|
|
heartbeat_lock = threading.Lock()
|
|
|
|
heartbeat_url = heartbeat_info_dict['url']
|
2021-04-05 01:41:21 -06:00
|
|
|
heartbeat_data = heartbeat_info_dict['data'].encode()
|
2021-02-09 23:45:20 -07:00
|
|
|
heartbeat_interval = heartbeat_info_dict.get('interval', 30)
|
|
|
|
|
2023-07-09 01:53:02 -06:00
|
|
|
request = Request(heartbeat_url, heartbeat_data)
|
2021-09-23 03:06:48 -06:00
|
|
|
|
2021-02-09 23:45:20 -07:00
|
|
|
def heartbeat():
|
|
|
|
try:
|
2021-09-23 03:06:48 -06:00
|
|
|
self.ydl.urlopen(request).read()
|
2021-02-09 23:45:20 -07:00
|
|
|
except Exception:
|
2024-06-11 17:09:58 -06:00
|
|
|
self.to_screen(f'[{self.FD_NAME}] Heartbeat failed')
|
2021-02-09 23:45:20 -07:00
|
|
|
|
|
|
|
with heartbeat_lock:
|
|
|
|
if not download_complete:
|
|
|
|
timer[0] = threading.Timer(heartbeat_interval, heartbeat)
|
|
|
|
timer[0].start()
|
|
|
|
|
2021-04-05 01:41:21 -06:00
|
|
|
heartbeat_info_dict['ping']()
|
|
|
|
self.to_screen('[%s] Heartbeat with %d second interval ...' % (self.FD_NAME, heartbeat_interval))
|
2021-02-09 23:45:20 -07:00
|
|
|
try:
|
|
|
|
heartbeat()
|
2021-04-05 01:41:21 -06:00
|
|
|
if type(fd).__name__ == 'HlsFD':
|
|
|
|
info_dict.update(ie._extract_m3u8_formats(info_dict['url'], info_dict['id'])[0])
|
2021-02-09 23:45:20 -07:00
|
|
|
success = fd.real_download(filename, info_dict)
|
|
|
|
finally:
|
|
|
|
if heartbeat_lock:
|
|
|
|
with heartbeat_lock:
|
|
|
|
timer[0].cancel()
|
|
|
|
download_complete = True
|
2022-05-09 05:54:28 -06:00
|
|
|
return success
|
2023-05-29 03:35:10 -06:00
|
|
|
|
|
|
|
|
|
|
|
class NiconicoLiveFD(FileDownloader):
|
|
|
|
""" Downloads niconico live without being stopped """
|
|
|
|
|
|
|
|
def real_download(self, filename, info_dict):
|
|
|
|
video_id = info_dict['video_id']
|
|
|
|
ws_url = info_dict['url']
|
|
|
|
ws_extractor = info_dict['ws']
|
|
|
|
ws_origin_host = info_dict['origin']
|
|
|
|
live_quality = info_dict.get('live_quality', 'high')
|
|
|
|
live_latency = info_dict.get('live_latency', 'high')
|
|
|
|
dl = FFmpegFD(self.ydl, self.params or {})
|
|
|
|
|
|
|
|
new_info_dict = info_dict.copy()
|
|
|
|
new_info_dict.update({
|
|
|
|
'protocol': 'm3u8',
|
|
|
|
})
|
|
|
|
|
|
|
|
def communicate_ws(reconnect):
|
|
|
|
if reconnect:
|
2023-11-20 01:04:04 -07:00
|
|
|
ws = self.ydl.urlopen(Request(ws_url, headers={'Origin': f'https://{ws_origin_host}'}))
|
2023-05-29 03:35:10 -06:00
|
|
|
if self.ydl.params.get('verbose', False):
|
|
|
|
self.to_screen('[debug] Sending startWatching request')
|
|
|
|
ws.send(json.dumps({
|
|
|
|
'type': 'startWatching',
|
|
|
|
'data': {
|
|
|
|
'stream': {
|
|
|
|
'quality': live_quality,
|
|
|
|
'protocol': 'hls+fmp4',
|
|
|
|
'latency': live_latency,
|
2024-06-11 17:09:58 -06:00
|
|
|
'chasePlay': False,
|
2023-05-29 03:35:10 -06:00
|
|
|
},
|
|
|
|
'room': {
|
|
|
|
'protocol': 'webSocket',
|
2024-06-11 17:09:58 -06:00
|
|
|
'commentable': True,
|
2023-05-29 03:35:10 -06:00
|
|
|
},
|
|
|
|
'reconnect': True,
|
2024-06-11 17:09:58 -06:00
|
|
|
},
|
2023-05-29 03:35:10 -06:00
|
|
|
}))
|
|
|
|
else:
|
|
|
|
ws = ws_extractor
|
|
|
|
with ws:
|
|
|
|
while True:
|
|
|
|
recv = ws.recv()
|
|
|
|
if not recv:
|
|
|
|
continue
|
|
|
|
data = json.loads(recv)
|
|
|
|
if not data or not isinstance(data, dict):
|
|
|
|
continue
|
|
|
|
if data.get('type') == 'ping':
|
|
|
|
# pong back
|
|
|
|
ws.send(r'{"type":"pong"}')
|
|
|
|
ws.send(r'{"type":"keepSeat"}')
|
|
|
|
elif data.get('type') == 'disconnect':
|
|
|
|
self.write_debug(data)
|
|
|
|
return True
|
|
|
|
elif data.get('type') == 'error':
|
|
|
|
self.write_debug(data)
|
|
|
|
message = try_get(data, lambda x: x['body']['code'], str) or recv
|
|
|
|
return DownloadError(message)
|
|
|
|
elif self.ydl.params.get('verbose', False):
|
|
|
|
if len(recv) > 100:
|
|
|
|
recv = recv[:100] + '...'
|
2024-06-11 17:09:58 -06:00
|
|
|
self.to_screen(f'[debug] Server said: {recv}')
|
2023-05-29 03:35:10 -06:00
|
|
|
|
|
|
|
def ws_main():
|
|
|
|
reconnect = False
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
ret = communicate_ws(reconnect)
|
|
|
|
if ret is True:
|
|
|
|
return
|
|
|
|
except BaseException as e:
|
2024-06-11 17:09:58 -06:00
|
|
|
self.to_screen('[{}] {}: Connection error occured, reconnecting after 10 seconds: {}'.format('niconico:live', video_id, str_or_none(e)))
|
2023-05-29 03:35:10 -06:00
|
|
|
time.sleep(10)
|
|
|
|
continue
|
|
|
|
finally:
|
|
|
|
reconnect = True
|
|
|
|
|
|
|
|
thread = threading.Thread(target=ws_main, daemon=True)
|
|
|
|
thread.start()
|
|
|
|
|
|
|
|
return dl.download(filename, new_info_dict)
|