niconico.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import json
  2. import threading
  3. import time
  4. from . import get_suitable_downloader
  5. from .common import FileDownloader
  6. from .external import FFmpegFD
  7. from ..networking import Request
  8. from ..utils import DownloadError, WebSocketsWrapper, str_or_none, try_get
  9. class NiconicoDmcFD(FileDownloader):
  10. """ Downloading niconico douga from DMC with heartbeat """
  11. def real_download(self, filename, info_dict):
  12. from ..extractor.niconico import NiconicoIE
  13. self.to_screen('[%s] Downloading from DMC' % self.FD_NAME)
  14. ie = NiconicoIE(self.ydl)
  15. info_dict, heartbeat_info_dict = ie._get_heartbeat_info(info_dict)
  16. fd = get_suitable_downloader(info_dict, params=self.params)(self.ydl, self.params)
  17. success = download_complete = False
  18. timer = [None]
  19. heartbeat_lock = threading.Lock()
  20. heartbeat_url = heartbeat_info_dict['url']
  21. heartbeat_data = heartbeat_info_dict['data'].encode()
  22. heartbeat_interval = heartbeat_info_dict.get('interval', 30)
  23. request = Request(heartbeat_url, heartbeat_data)
  24. def heartbeat():
  25. try:
  26. self.ydl.urlopen(request).read()
  27. except Exception:
  28. self.to_screen('[%s] Heartbeat failed' % self.FD_NAME)
  29. with heartbeat_lock:
  30. if not download_complete:
  31. timer[0] = threading.Timer(heartbeat_interval, heartbeat)
  32. timer[0].start()
  33. heartbeat_info_dict['ping']()
  34. self.to_screen('[%s] Heartbeat with %d second interval ...' % (self.FD_NAME, heartbeat_interval))
  35. try:
  36. heartbeat()
  37. if type(fd).__name__ == 'HlsFD':
  38. info_dict.update(ie._extract_m3u8_formats(info_dict['url'], info_dict['id'])[0])
  39. success = fd.real_download(filename, info_dict)
  40. finally:
  41. if heartbeat_lock:
  42. with heartbeat_lock:
  43. timer[0].cancel()
  44. download_complete = True
  45. return success
  46. class NiconicoLiveFD(FileDownloader):
  47. """ Downloads niconico live without being stopped """
  48. def real_download(self, filename, info_dict):
  49. video_id = info_dict['video_id']
  50. ws_url = info_dict['url']
  51. ws_extractor = info_dict['ws']
  52. ws_origin_host = info_dict['origin']
  53. cookies = info_dict.get('cookies')
  54. live_quality = info_dict.get('live_quality', 'high')
  55. live_latency = info_dict.get('live_latency', 'high')
  56. dl = FFmpegFD(self.ydl, self.params or {})
  57. new_info_dict = info_dict.copy()
  58. new_info_dict.update({
  59. 'protocol': 'm3u8',
  60. })
  61. def communicate_ws(reconnect):
  62. if reconnect:
  63. ws = WebSocketsWrapper(ws_url, {
  64. 'Cookies': str_or_none(cookies) or '',
  65. 'Origin': f'https://{ws_origin_host}',
  66. 'Accept': '*/*',
  67. 'User-Agent': self.params['http_headers']['User-Agent'],
  68. })
  69. if self.ydl.params.get('verbose', False):
  70. self.to_screen('[debug] Sending startWatching request')
  71. ws.send(json.dumps({
  72. 'type': 'startWatching',
  73. 'data': {
  74. 'stream': {
  75. 'quality': live_quality,
  76. 'protocol': 'hls+fmp4',
  77. 'latency': live_latency,
  78. 'chasePlay': False
  79. },
  80. 'room': {
  81. 'protocol': 'webSocket',
  82. 'commentable': True
  83. },
  84. 'reconnect': True,
  85. }
  86. }))
  87. else:
  88. ws = ws_extractor
  89. with ws:
  90. while True:
  91. recv = ws.recv()
  92. if not recv:
  93. continue
  94. data = json.loads(recv)
  95. if not data or not isinstance(data, dict):
  96. continue
  97. if data.get('type') == 'ping':
  98. # pong back
  99. ws.send(r'{"type":"pong"}')
  100. ws.send(r'{"type":"keepSeat"}')
  101. elif data.get('type') == 'disconnect':
  102. self.write_debug(data)
  103. return True
  104. elif data.get('type') == 'error':
  105. self.write_debug(data)
  106. message = try_get(data, lambda x: x['body']['code'], str) or recv
  107. return DownloadError(message)
  108. elif self.ydl.params.get('verbose', False):
  109. if len(recv) > 100:
  110. recv = recv[:100] + '...'
  111. self.to_screen('[debug] Server said: %s' % recv)
  112. def ws_main():
  113. reconnect = False
  114. while True:
  115. try:
  116. ret = communicate_ws(reconnect)
  117. if ret is True:
  118. return
  119. except BaseException as e:
  120. self.to_screen('[%s] %s: Connection error occured, reconnecting after 10 seconds: %s' % ('niconico:live', video_id, str_or_none(e)))
  121. time.sleep(10)
  122. continue
  123. finally:
  124. reconnect = True
  125. thread = threading.Thread(target=ws_main, daemon=True)
  126. thread.start()
  127. return dl.download(filename, new_info_dict)