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