diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index 046e03247..3f380aeff 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -96,6 +96,7 @@ from .extractor.openload import PhantomJSwrapper from .downloader import get_suitable_downloader from .downloader.rtmp import rtmpdump_version +from .downloader.httpfd import HttpHB from .postprocessor import ( FFmpegFixupM3u8PP, FFmpegFixupM4aPP, @@ -1839,7 +1840,12 @@ def ensure_dir_exists(path): if not self.params.get('skip_download', False): try: def dl(name, info): - fd = get_suitable_downloader(info, self.params)(self, self.params) + if 'heartbeat_url' in info: + print("use HTTPHB") + fd = HttpHB(self, self.params) + else: + fd = get_suitable_downloader(info, self.params)(self, self.params) + for ph in self._progress_hooks: fd.add_progress_hook(ph) if self.params.get('verbose'): diff --git a/youtube_dl/downloader/httpfd.py b/youtube_dl/downloader/httpfd.py new file mode 100644 index 000000000..15be74178 --- /dev/null +++ b/youtube_dl/downloader/httpfd.py @@ -0,0 +1,27 @@ +from .http import HttpFD + +import urllib.request as compat_urllib_request +import threading + + +class HttpHB(HttpFD): + def real_download(self, filename, info_dict): + result = False + if 'heartbeat_url'in info_dict: + def heart_beat(): + try: + data = info_dict['heartbeat_data'].encode("utf-8") + compat_urllib_request.urlopen(url=info_dict['heartbeat_url'], data=data) + print('heart beat!') + except Exception as ex: + print('heart beat fail: ' + ex.message) + pass + + if not result: + timer = threading.Timer(25, heart_beat) + timer.start() + + heart_beat() + + result = super(HttpHB, self).real_download(filename, info_dict) + return result diff --git a/youtube_dl/extractor/niconico.py b/youtube_dl/extractor/niconico.py index df7f528be..e1756567f 100644 --- a/youtube_dl/extractor/niconico.py +++ b/youtube_dl/extractor/niconico.py @@ -254,6 +254,10 @@ def yesno(boolean): } })) + # get heart-beat data + api_url = session_api_endpoint['url'] + '/' + session_response['data']['session']['id'] + '?_format=json&_method=PUT' + data = json.dumps(session_response['data']) + resolution = video_quality.get('resolution', {}) return { @@ -264,6 +268,8 @@ def yesno(boolean): 'vbr': float_or_none(video_quality.get('bitrate'), 1000), 'height': resolution.get('height'), 'width': resolution.get('width'), + 'heartbeat_url': api_url, # pay attention here + 'heartbeat_data': data, } def _real_extract(self, url):