]> jfr.im git - yt-dlp.git/blame - yt_dlp/downloader/niconico.py
[cleanup] Misc
[yt-dlp.git] / yt_dlp / downloader / niconico.py
CommitLineData
fb198a8a 1import threading
2
c487cf00 3from . import get_suitable_downloader
fb198a8a 4from .common import FileDownloader
ee2b3563 5from ..utils import sanitized_Request
fb198a8a 6
7
8class NiconicoDmcFD(FileDownloader):
9 """ Downloading niconico douga from DMC with heartbeat """
10
fb198a8a 11 def real_download(self, filename, info_dict):
c487cf00 12 from ..extractor.niconico import NiconicoIE
fb198a8a 13
c487cf00 14 self.to_screen('[%s] Downloading from DMC' % self.FD_NAME)
fb198a8a 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