[ie/instagram] Avoid unnecessary API call (#17127)

Fix 8b8e3e3cb4d3ba0dedf7b1fd00ce68f07da7e588

Authored by: bashonly
This commit is contained in:
bashonly 2026-07-03 13:28:53 -05:00 committed by GitHub
parent ac4c955ea9
commit 161dd9fd05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

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