]> jfr.im git - yt-dlp.git/blame - yt_dlp/downloader/niconico.py
Bugfix for 591bb9d3553a4d7b453777c1e28e0948741e3b50
[yt-dlp.git] / yt_dlp / downloader / niconico.py
CommitLineData
fb198a8a 1import threading
2
3from .common import FileDownloader
dbf5416a 4from ..downloader import get_suitable_downloader
fb198a8a 5from ..extractor.niconico import NiconicoIE
ee2b3563 6from ..utils import sanitized_Request
fb198a8a 7
8
9class NiconicoDmcFD(FileDownloader):
10 """ Downloading niconico douga from DMC with heartbeat """
11
fb198a8a 12 def real_download(self, filename, info_dict):
13 self.to_screen('[%s] Downloading from DMC' % self.FD_NAME)
14
15 ie = NiconicoIE(self.ydl)
16 info_dict, heartbeat_info_dict = ie._get_heartbeat_info(info_dict)
17
dbf5416a 18 fd = get_suitable_downloader(info_dict, params=self.params)(self.ydl, self.params)
fb198a8a 19
20 success = download_complete = False
21 timer = [None]
fb198a8a 22 heartbeat_lock = threading.Lock()
23 heartbeat_url = heartbeat_info_dict['url']
2291dbce 24 heartbeat_data = heartbeat_info_dict['data'].encode()
fb198a8a 25 heartbeat_interval = heartbeat_info_dict.get('interval', 30)
fb198a8a 26
ee2b3563
THD
27 request = sanitized_Request(heartbeat_url, heartbeat_data)
28
fb198a8a 29 def heartbeat():
30 try:
ee2b3563 31 self.ydl.urlopen(request).read()
fb198a8a 32 except Exception:
33 self.to_screen('[%s] Heartbeat failed' % self.FD_NAME)
34
35 with heartbeat_lock:
36 if not download_complete:
37 timer[0] = threading.Timer(heartbeat_interval, heartbeat)
38 timer[0].start()
39
2291dbce 40 heartbeat_info_dict['ping']()
41 self.to_screen('[%s] Heartbeat with %d second interval ...' % (self.FD_NAME, heartbeat_interval))
fb198a8a 42 try:
43 heartbeat()
2291dbce 44 if type(fd).__name__ == 'HlsFD':
45 info_dict.update(ie._extract_m3u8_formats(info_dict['url'], info_dict['id'])[0])
fb198a8a 46 success = fd.real_download(filename, info_dict)
47 finally:
48 if heartbeat_lock:
49 with heartbeat_lock:
50 timer[0].cancel()
51 download_complete = True
0f06bcd7 52 return success