]> jfr.im git - yt-dlp.git/blob - yt_dlp/downloader/niconico.py
5e9dda03d5008e780230b667ac1f5d3ea9926e7e
[yt-dlp.git] / yt_dlp / downloader / niconico.py
1 import threading
2
3 from .common import FileDownloader
4 from ..downloader import get_suitable_downloader
5 from ..extractor.niconico import NiconicoIE
6 from ..utils import sanitized_Request
7
8
9 class NiconicoDmcFD(FileDownloader):
10 """ Downloading niconico douga from DMC with heartbeat """
11
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
18 fd = get_suitable_downloader(info_dict, params=self.params)(self.ydl, self.params)
19
20 success = download_complete = False
21 timer = [None]
22 heartbeat_lock = threading.Lock()
23 heartbeat_url = heartbeat_info_dict['url']
24 heartbeat_data = heartbeat_info_dict['data'].encode()
25 heartbeat_interval = heartbeat_info_dict.get('interval', 30)
26
27 request = sanitized_Request(heartbeat_url, heartbeat_data)
28
29 def heartbeat():
30 try:
31 self.ydl.urlopen(request).read()
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
40 heartbeat_info_dict['ping']()
41 self.to_screen('[%s] Heartbeat with %d second interval ...' % (self.FD_NAME, heartbeat_interval))
42 try:
43 heartbeat()
44 if type(fd).__name__ == 'HlsFD':
45 info_dict.update(ie._extract_m3u8_formats(info_dict['url'], info_dict['id'])[0])
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
52 return success