logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

niconico.py (2315B)


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