Skip to content

Commit

Permalink
[niconico] use HeartBeat
Browse files Browse the repository at this point in the history
  • Loading branch information
nekojun committed May 19, 2018
1 parent 7550ea5 commit 5a6f5f9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
8 changes: 7 additions & 1 deletion youtube_dl/YoutubeDL.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'):
Expand Down
27 changes: 27 additions & 0 deletions youtube_dl/downloader/httpfd.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions youtube_dl/extractor/niconico.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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):
Expand Down

0 comments on commit 5a6f5f9

Please sign in to comment.