]> jfr.im git - yt-dlp.git/blob - yt_dlp/downloader/niconico.py
Release 2021.03.01
[yt-dlp.git] / yt_dlp / downloader / niconico.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import threading
5
6 from .common import FileDownloader
7 from ..downloader import _get_real_downloader
8 from ..extractor.niconico import NiconicoIE
9 from ..compat import compat_urllib_request
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
23 fd = _get_real_downloader(info_dict, params=self.params)(self.ydl, self.params)
24
25 success = download_complete = False
26 timer = [None]
27
28 heartbeat_lock = threading.Lock()
29 heartbeat_url = heartbeat_info_dict['url']
30 heartbeat_data = heartbeat_info_dict['data']
31 heartbeat_interval = heartbeat_info_dict.get('interval', 30)
32 self.to_screen('[%s] Heartbeat with %s second interval ...' % (self.FD_NAME, heartbeat_interval))
33
34 def heartbeat():
35 try:
36 compat_urllib_request.urlopen(url=heartbeat_url, data=heartbeat_data.encode())
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
45 try:
46 heartbeat()
47 success = fd.real_download(filename, info_dict)
48 finally:
49 if heartbeat_lock:
50 with heartbeat_lock:
51 timer[0].cancel()
52 download_complete = True
53
54 return success