2020-08-04 22:02:23 +00:00
|
|
|
from __future__ import division, unicode_literals
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
from .fragment import FragmentFD
|
2021-01-31 07:48:06 +00:00
|
|
|
from ..compat import compat_urllib_error
|
2021-02-15 09:57:21 +00:00
|
|
|
from ..utils import (
|
|
|
|
try_get,
|
|
|
|
RegexNotFoundError,
|
|
|
|
)
|
2021-01-31 07:48:06 +00:00
|
|
|
from ..extractor.youtube import YoutubeBaseInfoExtractor as YT_BaseIE
|
2020-08-04 22:02:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class YoutubeLiveChatReplayFD(FragmentFD):
|
|
|
|
""" Downloads YouTube live chat replays fragment by fragment """
|
|
|
|
|
|
|
|
FD_NAME = 'youtube_live_chat_replay'
|
|
|
|
|
|
|
|
def real_download(self, filename, info_dict):
|
|
|
|
video_id = info_dict['video_id']
|
|
|
|
self.to_screen('[%s] Downloading live chat' % self.FD_NAME)
|
|
|
|
|
2021-01-31 07:48:06 +00:00
|
|
|
fragment_retries = self.params.get('fragment_retries', 0)
|
2020-08-04 22:02:23 +00:00
|
|
|
test = self.params.get('test', False)
|
|
|
|
|
|
|
|
ctx = {
|
|
|
|
'filename': filename,
|
|
|
|
'live': True,
|
|
|
|
'total_frags': None,
|
|
|
|
}
|
|
|
|
|
2021-02-15 09:57:21 +00:00
|
|
|
ie = YT_BaseIE(self.ydl)
|
2020-08-04 22:02:23 +00:00
|
|
|
|
2021-02-15 09:57:21 +00:00
|
|
|
def dl_fragment(url, data=None, headers=None):
|
|
|
|
http_headers = info_dict.get('http_headers', {})
|
|
|
|
if headers:
|
|
|
|
http_headers = http_headers.copy()
|
|
|
|
http_headers.update(headers)
|
|
|
|
return self._download_fragment(ctx, url, info_dict, http_headers, data)
|
2020-08-04 22:02:23 +00:00
|
|
|
|
2021-02-15 09:57:21 +00:00
|
|
|
def download_and_parse_fragment(url, frag_index, request_data):
|
2021-01-31 07:48:06 +00:00
|
|
|
count = 0
|
|
|
|
while count <= fragment_retries:
|
|
|
|
try:
|
2021-02-15 09:57:21 +00:00
|
|
|
success, raw_fragment = dl_fragment(url, request_data, {'content-type': 'application/json'})
|
2021-01-31 07:48:06 +00:00
|
|
|
if not success:
|
|
|
|
return False, None, None
|
2021-02-15 09:57:21 +00:00
|
|
|
try:
|
|
|
|
data = ie._extract_yt_initial_data(video_id, raw_fragment.decode('utf-8', 'replace'))
|
|
|
|
except RegexNotFoundError:
|
|
|
|
data = None
|
2021-02-07 09:52:36 +00:00
|
|
|
if not data:
|
2021-02-15 09:57:21 +00:00
|
|
|
data = json.loads(raw_fragment)
|
2021-01-31 07:48:06 +00:00
|
|
|
live_chat_continuation = try_get(
|
|
|
|
data,
|
|
|
|
lambda x: x['continuationContents']['liveChatContinuation'], dict) or {}
|
|
|
|
offset = continuation_id = None
|
|
|
|
processed_fragment = bytearray()
|
|
|
|
for action in live_chat_continuation.get('actions', []):
|
|
|
|
if 'replayChatItemAction' in action:
|
|
|
|
replay_chat_item_action = action['replayChatItemAction']
|
|
|
|
offset = int(replay_chat_item_action['videoOffsetTimeMsec'])
|
|
|
|
processed_fragment.extend(
|
|
|
|
json.dumps(action, ensure_ascii=False).encode('utf-8') + b'\n')
|
|
|
|
if offset is not None:
|
|
|
|
continuation_id = try_get(
|
|
|
|
live_chat_continuation,
|
|
|
|
lambda x: x['continuations'][0]['liveChatReplayContinuationData']['continuation'])
|
|
|
|
self._append_fragment(ctx, processed_fragment)
|
|
|
|
|
|
|
|
return True, continuation_id, offset
|
|
|
|
except compat_urllib_error.HTTPError as err:
|
|
|
|
count += 1
|
|
|
|
if count <= fragment_retries:
|
|
|
|
self.report_retry_fragment(err, frag_index, count, fragment_retries)
|
|
|
|
if count > fragment_retries:
|
|
|
|
self.report_error('giving up after %s fragment retries' % fragment_retries)
|
|
|
|
return False, None, None
|
|
|
|
|
2020-08-04 22:02:23 +00:00
|
|
|
self._prepare_and_start_frag_download(ctx)
|
|
|
|
|
2021-03-23 12:23:36 +00:00
|
|
|
success, raw_fragment = dl_fragment(info_dict['url'])
|
2020-08-04 22:02:23 +00:00
|
|
|
if not success:
|
|
|
|
return False
|
2021-02-15 09:57:21 +00:00
|
|
|
try:
|
|
|
|
data = ie._extract_yt_initial_data(video_id, raw_fragment.decode('utf-8', 'replace'))
|
|
|
|
except RegexNotFoundError:
|
|
|
|
return False
|
2021-01-31 07:48:06 +00:00
|
|
|
continuation_id = try_get(
|
|
|
|
data,
|
|
|
|
lambda x: x['contents']['twoColumnWatchNextResults']['conversationBar']['liveChatRenderer']['continuations'][0]['reloadContinuationData']['continuation'])
|
2020-08-04 22:02:23 +00:00
|
|
|
# no data yet but required to call _append_fragment
|
|
|
|
self._append_fragment(ctx, b'')
|
|
|
|
|
2021-02-15 09:57:21 +00:00
|
|
|
ytcfg = ie._extract_ytcfg(video_id, raw_fragment.decode('utf-8', 'replace'))
|
|
|
|
|
|
|
|
if not ytcfg:
|
|
|
|
return False
|
|
|
|
api_key = try_get(ytcfg, lambda x: x['INNERTUBE_API_KEY'])
|
|
|
|
innertube_context = try_get(ytcfg, lambda x: x['INNERTUBE_CONTEXT'])
|
|
|
|
if not api_key or not innertube_context:
|
|
|
|
return False
|
|
|
|
url = 'https://www.youtube.com/youtubei/v1/live_chat/get_live_chat_replay?key=' + api_key
|
|
|
|
|
2021-01-31 07:48:06 +00:00
|
|
|
frag_index = offset = 0
|
2020-08-04 22:02:23 +00:00
|
|
|
while continuation_id is not None:
|
2021-01-31 07:48:06 +00:00
|
|
|
frag_index += 1
|
2021-02-15 09:57:21 +00:00
|
|
|
request_data = {
|
|
|
|
'context': innertube_context,
|
|
|
|
'continuation': continuation_id,
|
|
|
|
}
|
|
|
|
if frag_index > 1:
|
|
|
|
request_data['currentPlayerState'] = {'playerOffsetMs': str(max(offset - 5000, 0))}
|
|
|
|
success, continuation_id, offset = download_and_parse_fragment(
|
|
|
|
url, frag_index, json.dumps(request_data, ensure_ascii=False).encode('utf-8') + b'\n')
|
2021-01-31 07:48:06 +00:00
|
|
|
if not success:
|
|
|
|
return False
|
|
|
|
if test:
|
2020-08-04 22:02:23 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
self._finish_frag_download(ctx)
|
|
|
|
return True
|