A feature-rich command-line audio/video downloader
[ie/instagram] Avoid unnecessary API call (#17127)
bashonly161dd9fd05d1
diff --git a/yt_dlp/extractor/instagram.py b/yt_dlp/extractor/instagram.pyindex 38f8aa1148..b44e5e0824 100644--- a/yt_dlp/extractor/instagram.py+++ b/yt_dlp/extractor/instagram.py@@ -7,6 +7,7 @@ import urllib.parsefrom .common import InfoExtractorfrom ..networking.exceptions import HTTPError+from ..networking.impersonate import ImpersonateTargetfrom ..utils import (ExtractorError,bug_reports_message,@@ -57,6 +58,10 @@ class InstagramBaseIE(InfoExtractor):'.instagram.com',)+ @functools.cached_property+ def _can_impersonate(self):+ return self._downloader._impersonate_target_available(ImpersonateTarget())+@propertydef _is_logged_in(self):return bool(self._get_cookies(self._BASE_URL).get(self._AUTH_COOKIE_NAME))@@ -413,7 +418,8 @@ class InstagramIE(InstagramBaseIE):self.write_debug('Found Instagram account cookies')returnif not self._lsd_token:- webpage = self._download_webpage(self._BASE_URL, None, 'Setting up session', impersonate=True)+ webpage = self._download_webpage(+ self._BASE_URL, None, 'Setting up session', impersonate=self._can_impersonate)eqmc = self._search_json(r'<script\b[^>]*\bid="__eqmc"[^>]*>', webpage, 'eqmc JSON', None, default={})self._lsd_token = (@@ -429,7 +435,8 @@ class InstagramIE(InstagramBaseIE):return self._extract_product(self._download_json(f'{self._API_BASE_URL}/media/{media_id}/info/', video_id,'Downloading video info', 'Video info extraction failed',- impersonate=self._is_web_app, headers=self._api_headers)['items'][0])+ impersonate=self._can_impersonate and self._is_web_app,+ headers=self._api_headers)['items'][0])except ExtractorError as e:if not (isinstance(e.cause, HTTPError) and self._is_login_redirect(e.cause.response.url)):raise@@ -444,7 +451,7 @@ class InstagramIE(InstagramBaseIE):api_check = self._download_json(f'{self._API_BASE_URL}/web/get_ruling_for_content/', video_id,'Checking post accessibility', errnote=False, fatal=False,- impersonate=True, headers=self._api_headers,+ impersonate=self._can_impersonate, headers=self._api_headers,query={'content_type': 'MEDIA', 'target_id': media_id}) or {}csrf_token = self._get_cookies('https://www.instagram.com').get('csrftoken')@@ -472,7 +479,7 @@ class InstagramIE(InstagramBaseIE):'server_timestamps': 'true','variables': json.dumps({'media_id': media_id}, separators=(',', ':')),'doc_id': '27130156389949648',- }))+ })) if self._can_impersonate else Nonemedia = traverse_obj(response, ('data', 'xig_polaris_media', {dict}))product_info = traverse_obj(media, ('if_not_gated_logged_out', {dict}))@@ -490,7 +497,7 @@ class InstagramIE(InstagramBaseIE):'This content is only available for registered users who follow this account')webpage, urlh = self._download_webpage_handle(- f'https://www.instagram.com/p/{video_id}', video_id)+ f'https://www.instagram.com/p/{video_id}', video_id, impersonate=self._can_impersonate)if self._is_login_redirect(urlh.url):self.raise_login_required('The webpage request was redirected to the login page. '@@ -710,7 +717,8 @@ class InstagramStoryIE(InstagramBaseIE):if username == 'highlights' and not story_id: # story id is only mandatory for highlightsraise ExtractorError('Input URL is missing a highlight ID', expected=True)display_id = story_id or username- story_info = self._download_webpage(url, display_id, impersonate=self._is_web_app)+ story_info = self._download_webpage(+ url, display_id, impersonate=self._can_impersonate and self._is_web_app)user_info = self._search_json(r'"user":', story_info, 'user info', display_id, fatal=False)if not user_info:self.raise_login_required('This content is unreachable')@@ -725,8 +733,8 @@ class InstagramStoryIE(InstagramBaseIE):videos = traverse_obj(self._download_json(f'{self._API_BASE_URL}/feed/reels_media/?reel_ids={story_info_url}',- display_id, errnote=False, fatal=False, impersonate=self._is_web_app,- headers=self._api_headers), 'reels')+ display_id, errnote=False, fatal=False, headers=self._api_headers,+ impersonate=self._can_impersonate and self._is_web_app), 'reels')if not videos:self.raise_login_required('You need to log in to access this content')user_info = traverse_obj(videos, (user_id, 'user', {dict})) or {}