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