- syncid = self._extract_account_syncid(ytcfg, player_response)
- headers = self._generate_api_headers(ytcfg, identity_token, syncid, session_index=session_index)
-
- ytm_streaming_data = {}
- if is_music_url:
- ytm_webpage = None
- sts = self._extract_signature_timestamp(video_id, player_url, ytcfg, fatal=False)
- if sts and not force_mobile_client and 'configs' not in player_skip:
- ytm_webpage = self._download_webpage(
- 'https://music.youtube.com',
- video_id, fatal=False, note='Downloading remix client config')
-
- ytm_cfg = self._extract_ytcfg(video_id, ytm_webpage) or {}
- ytm_client = 'WEB_REMIX'
- if not sts or force_mobile_client:
- # Android client already has signature descrambled
- # See: https://github.com/TeamNewPipe/NewPipeExtractor/issues/562
- if not sts:
- self.report_warning('Falling back to android remix client for player API.')
- ytm_client = 'ANDROID_MUSIC'
- ytm_cfg = {}
-
- ytm_headers = self._generate_api_headers(
- ytm_cfg, identity_token, syncid,
- client=ytm_client, session_index=session_index)
- ytm_query = {'videoId': video_id}
- ytm_query.update(self._generate_player_context(sts))
-
- ytm_player_response = self._extract_response(
- item_id=video_id, ep='player', query=ytm_query,
- ytcfg=ytm_cfg, headers=ytm_headers, fatal=False,
- default_client=ytm_client,
- note='Downloading %sremix player API JSON' % ('android ' if force_mobile_client else ''))
- ytm_streaming_data = try_get(ytm_player_response, lambda x: x['streamingData'], dict) or {}
-
- if not player_response or force_mobile_client:
- sts = self._extract_signature_timestamp(video_id, player_url, ytcfg, fatal=False)
- yt_client = 'WEB'
- ytpcfg = ytcfg
- ytp_headers = headers
- if not sts or force_mobile_client:
- # Android client already has signature descrambled
- # See: https://github.com/TeamNewPipe/NewPipeExtractor/issues/562
- if not sts:
- self.report_warning('Falling back to android client for player API.')
- yt_client = 'ANDROID'
- ytpcfg = {}
- ytp_headers = self._generate_api_headers(ytpcfg, identity_token, syncid,
- client=yt_client, session_index=session_index)
-
- yt_query = {'videoId': video_id}
- yt_query.update(self._generate_player_context(sts))
- player_response = self._extract_response(
- item_id=video_id, ep='player', query=yt_query,
- ytcfg=ytpcfg, headers=ytp_headers, fatal=False,
- default_client=yt_client,
- note='Downloading %splayer API JSON' % ('android ' if force_mobile_client else '')
- ) or player_response
-
- # Age-gate workarounds
- playability_status = player_response.get('playabilityStatus') or {}
- if playability_status.get('reason') in self._AGE_GATE_REASONS:
- gvi_clients = ('ANDROID', 'TVHTML5') if force_mobile_client else ('TVHTML5', 'ANDROID')
- for gvi_client in gvi_clients:
- pr = self._parse_json(try_get(compat_parse_qs(
- self._download_webpage(
- base_url + 'get_video_info', video_id,
- 'Refetching age-gated %s info webpage' % gvi_client.lower(),
- 'unable to download video info webpage', fatal=False,
- query=self._get_video_info_params(video_id, client=gvi_client))),
- lambda x: x['player_response'][0],
- compat_str) or '{}', video_id)
- if pr:
- break
- if not pr:
- self.report_warning('Falling back to embedded-only age-gate workaround.')
- embed_webpage = None
- sts = self._extract_signature_timestamp(video_id, player_url, ytcfg, fatal=False)
- if sts and not force_mobile_client and 'configs' not in player_skip:
- embed_webpage = self._download_webpage(
- 'https://www.youtube.com/embed/%s?html5=1' % video_id,
- video_id=video_id, note='Downloading age-gated embed config')
-
- ytcfg_age = self._extract_ytcfg(video_id, embed_webpage) or {}
- # If we extracted the embed webpage, it'll tell us if we can view the video
- embedded_pr = self._parse_json(
- try_get(ytcfg_age, lambda x: x['PLAYER_VARS']['embedded_player_response'], str) or '{}',
- video_id=video_id)
- embedded_ps_reason = try_get(embedded_pr, lambda x: x['playabilityStatus']['reason'], str) or ''
- if embedded_ps_reason not in self._AGE_GATE_REASONS:
- yt_client = 'WEB_EMBEDDED_PLAYER'
- if not sts or force_mobile_client:
- # Android client already has signature descrambled
- # See: https://github.com/TeamNewPipe/NewPipeExtractor/issues/562
- if not sts:
- self.report_warning(
- 'Falling back to android embedded client for player API (note: some formats may be missing).')
- yt_client = 'ANDROID_EMBEDDED_PLAYER'
- ytcfg_age = {}
-
- ytage_headers = self._generate_api_headers(
- ytcfg_age, identity_token, syncid,
- client=yt_client, session_index=session_index)
- yt_age_query = {'videoId': video_id}
- yt_age_query.update(self._generate_player_context(sts))
- pr = self._extract_response(
- item_id=video_id, ep='player', query=yt_age_query,
- ytcfg=ytcfg_age, headers=ytage_headers, fatal=False,
- default_client=yt_client,
- note='Downloading %sage-gated player API JSON' % ('android ' if force_mobile_client else '')
- ) or {}
-