]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/instagram.py
[ie/matchtv] Fix extractor (#10190)
[yt-dlp.git] / yt_dlp / extractor / instagram.py
CommitLineData
315ab3d5 1import hashlib
e3e606de 2import itertools
27b1c73f 3import json
59fc531f 4import re
ab2ffab2 5import time
59fc531f
JMF
6
7from .common import InfoExtractor
3d2623a8 8from ..networking.exceptions import HTTPError
e1ec9330 9from ..utils import (
238d42cf 10 ExtractorError,
e3e606de
PD
11 decode_base_n,
12 encode_base_n,
50eaea9f 13 filter_dict,
cce889b9 14 float_or_none,
e3e606de 15 format_field,
c4096e8a 16 get_element_by_attribute,
e1ec9330 17 int_or_none,
87696e78 18 lowercase_escape,
013322a9 19 str_or_none,
4e260d1a 20 str_to_int,
eb56d132 21 traverse_obj,
3052a30d 22 url_or_none,
ab2ffab2 23 urlencode_postdata,
e1ec9330 24)
59fc531f 25
e3e606de
PD
26_ENCODING_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
27
28
add96eb9 29def _pk_to_id(media_id):
e3e606de 30 """Source: https://stackoverflow.com/questions/24437823/getting-instagram-post-url-from-media-id"""
add96eb9 31 return encode_base_n(int(media_id.split('_')[0]), table=_ENCODING_CHARS)
e3e606de
PD
32
33
34def _id_to_pk(shortcode):
35 """Covert a shortcode to a numeric value"""
36 return decode_base_n(shortcode[:11], table=_ENCODING_CHARS)
37
0de668af 38
8dcf65c9 39class InstagramBaseIE(InfoExtractor):
ab2ffab2 40 _NETRC_MACHINE = 'instagram'
8dcf65c9 41 _IS_LOGGED_IN = False
42
7d3b98be 43 _API_BASE_URL = 'https://i.instagram.com/api/v1'
44 _LOGIN_URL = 'https://www.instagram.com/accounts/login'
45 _API_HEADERS = {
46 'X-IG-App-ID': '936619743392459',
47 'X-ASBD-ID': '198387',
48 'X-IG-WWW-Claim': '0',
49 'Origin': 'https://www.instagram.com',
50 'Accept': '*/*',
51 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36',
52 }
53
52efa4b3 54 def _perform_login(self, username, password):
55 if self._IS_LOGGED_IN:
8dcf65c9 56 return
57
58 login_webpage = self._download_webpage(
7d3b98be 59 self._LOGIN_URL, None, note='Downloading login webpage', errnote='Failed to download login webpage')
8dcf65c9 60
7d3b98be 61 shared_data = self._parse_json(self._search_regex(
62 r'window\._sharedData\s*=\s*({.+?});', login_webpage, 'shared data', default='{}'), None)
63
64 login = self._download_json(
65 f'{self._LOGIN_URL}/ajax/', None, note='Logging in', headers={
66 **self._API_HEADERS,
67 'X-Requested-With': 'XMLHttpRequest',
68 'X-CSRFToken': shared_data['config']['csrf_token'],
69 'X-Instagram-AJAX': shared_data['rollout_hash'],
70 'Referer': 'https://www.instagram.com/',
71 }, data=urlencode_postdata({
72 'enc_password': f'#PWD_INSTAGRAM_BROWSER:0:{int(time.time())}:{password}',
73 'username': username,
74 'queryParams': '{}',
75 'optIntoOneTap': 'false',
76 'stopDeletionNonce': '',
77 'trustedDeviceRecords': '{}',
78 }))
8dcf65c9 79
80 if not login.get('authenticated'):
81 if login.get('message'):
82 raise ExtractorError(f'Unable to login: {login["message"]}')
d298d33f
M
83 elif login.get('user'):
84 raise ExtractorError('Unable to login: Sorry, your password was incorrect. Please double-check your password.', expected=True)
85 elif login.get('user') is False:
86 raise ExtractorError('Unable to login: The username you entered doesn\'t belong to an account. Please check your username and try again.', expected=True)
8dcf65c9 87 raise ExtractorError('Unable to login')
88 InstagramBaseIE._IS_LOGGED_IN = True
89
eb56d132 90 def _get_count(self, media, kind, *keys):
91 return traverse_obj(
92 media, (kind, 'count'), *((f'edge_media_{key}', 'count') for key in keys),
93 expected_type=int_or_none)
94
95 def _get_dimension(self, name, media, webpage=None):
96 return (
97 traverse_obj(media, ('dimensions', name), expected_type=int_or_none)
98 or int_or_none(self._html_search_meta(
99 (f'og:video:{name}', f'video:{name}'), webpage or '', default=None)))
100
101 def _extract_nodes(self, nodes, is_direct=False):
102 for idx, node in enumerate(nodes, start=1):
103 if node.get('__typename') != 'GraphVideo' and node.get('is_video') is not True:
104 continue
105
106 video_id = node.get('shortcode')
107
108 if is_direct:
109 info = {
110 'id': video_id or node['id'],
111 'url': node.get('video_url'),
112 'width': self._get_dimension('width', node),
113 'height': self._get_dimension('height', node),
114 'http_headers': {
115 'Referer': 'https://www.instagram.com/',
add96eb9 116 },
eb56d132 117 }
118 elif not video_id:
119 continue
120 else:
121 info = {
122 '_type': 'url',
123 'ie_key': 'Instagram',
124 'id': video_id,
125 'url': f'https://instagram.com/p/{video_id}',
126 }
127
128 yield {
129 **info,
130 'title': node.get('title') or (f'Video {idx}' if is_direct else None),
131 'description': traverse_obj(
132 node, ('edge_media_to_caption', 'edges', 0, 'node', 'text'), expected_type=str),
133 'thumbnail': traverse_obj(
134 node, 'display_url', 'thumbnail_src', 'display_src', expected_type=url_or_none),
135 'duration': float_or_none(node.get('video_duration')),
136 'timestamp': int_or_none(node.get('taken_at_timestamp')),
137 'view_count': int_or_none(node.get('video_view_count')),
138 'comment_count': self._get_count(node, 'comments', 'preview_comment', 'to_comment', 'to_parent_comment'),
139 'like_count': self._get_count(node, 'likes', 'preview_like'),
140 }
141
013322a9 142 def _extract_product_media(self, product_media):
7d3b98be 143 media_id = product_media.get('code') or _pk_to_id(product_media.get('pk'))
013322a9
M
144 vcodec = product_media.get('video_codec')
145 dash_manifest_raw = product_media.get('video_dash_manifest')
146 videos_list = product_media.get('video_versions')
147 if not (dash_manifest_raw or videos_list):
c5332d7f 148 return {}
013322a9
M
149
150 formats = [{
add96eb9 151 'format_id': fmt.get('id'),
152 'url': fmt.get('url'),
153 'width': fmt.get('width'),
154 'height': fmt.get('height'),
013322a9 155 'vcodec': vcodec,
add96eb9 156 } for fmt in videos_list or []]
013322a9
M
157 if dash_manifest_raw:
158 formats.extend(self._parse_mpd_formats(self._parse_xml(dash_manifest_raw, media_id), mpd_id='dash'))
013322a9
M
159
160 thumbnails = [{
161 'url': thumbnail.get('url'),
162 'width': thumbnail.get('width'),
add96eb9 163 'height': thumbnail.get('height'),
013322a9
M
164 } for thumbnail in traverse_obj(product_media, ('image_versions2', 'candidates')) or []]
165 return {
166 'id': media_id,
167 'duration': float_or_none(product_media.get('video_duration')),
168 'formats': formats,
add96eb9 169 'thumbnails': thumbnails,
013322a9
M
170 }
171
172 def _extract_product(self, product_info):
173 if isinstance(product_info, list):
174 product_info = product_info[0]
175
176 user_info = product_info.get('user') or {}
177 info_dict = {
2e767548 178 'id': _pk_to_id(traverse_obj(product_info, 'pk', 'id', expected_type=str_or_none)[:19]),
013322a9
M
179 'title': product_info.get('title') or f'Video by {user_info.get("username")}',
180 'description': traverse_obj(product_info, ('caption', 'text'), expected_type=str_or_none),
181 'timestamp': int_or_none(product_info.get('taken_at')),
182 'channel': user_info.get('username'),
183 'uploader': user_info.get('full_name'),
184 'uploader_id': str_or_none(user_info.get('pk')),
185 'view_count': int_or_none(product_info.get('view_count')),
186 'like_count': int_or_none(product_info.get('like_count')),
187 'comment_count': int_or_none(product_info.get('comment_count')),
2e767548 188 '__post_extractor': self.extract_comments(_pk_to_id(product_info.get('pk'))),
013322a9
M
189 'http_headers': {
190 'Referer': 'https://www.instagram.com/',
add96eb9 191 },
013322a9
M
192 }
193 carousel_media = product_info.get('carousel_media')
194 if carousel_media:
195 return {
196 '_type': 'playlist',
197 **info_dict,
198 'title': f'Post by {user_info.get("username")}',
199 'entries': [{
200 **info_dict,
201 **self._extract_product_media(product_media),
202 } for product_media in carousel_media],
203 }
204
205 return {
206 **info_dict,
add96eb9 207 **self._extract_product_media(product_info),
013322a9
M
208 }
209
2e767548
PD
210 def _get_comments(self, video_id):
211 comments_info = self._download_json(
212 f'{self._API_BASE_URL}/media/{_id_to_pk(video_id)}/comments/?can_support_threading=true&permalink_enabled=false', video_id,
213 fatal=False, errnote='Comments extraction failed', note='Downloading comments info', headers=self._API_HEADERS) or {}
214
215 comment_data = traverse_obj(comments_info, ('edge_media_to_parent_comment', 'edges'), 'comments')
216 for comment_dict in comment_data or []:
217 yield {
218 'author': traverse_obj(comment_dict, ('node', 'owner', 'username'), ('user', 'username')),
219 'author_id': traverse_obj(comment_dict, ('node', 'owner', 'id'), ('user', 'pk')),
220 'author_thumbnail': traverse_obj(comment_dict, ('node', 'owner', 'profile_pic_url'), ('user', 'profile_pic_url'), expected_type=url_or_none),
221 'id': traverse_obj(comment_dict, ('node', 'id'), 'pk'),
222 'text': traverse_obj(comment_dict, ('node', 'text'), 'text'),
223 'like_count': traverse_obj(comment_dict, ('node', 'edge_liked_by', 'count'), 'comment_like_count', expected_type=int_or_none),
224 'timestamp': traverse_obj(comment_dict, ('node', 'created_at'), 'created_at', expected_type=int_or_none),
225 }
226
8dcf65c9 227
fb2d1ee6 228class InstagramIOSIE(InfoExtractor):
c586f9e8 229 IE_DESC = 'IOS instagram:// URL'
fb2d1ee6 230 _VALID_URL = r'instagram://media\?id=(?P<id>[\d_]+)'
231 _TESTS = [{
232 'url': 'instagram://media?id=482584233761418119',
233 'md5': '0d2da106a9d2631273e192b372806516',
234 'info_dict': {
235 'id': 'aye83DjauH',
236 'ext': 'mp4',
237 'title': 'Video by naomipq',
238 'description': 'md5:1f17f0ab29bd6fe2bfad705f58de3cb8',
239 'thumbnail': r're:^https?://.*\.jpg',
240 'duration': 0,
241 'timestamp': 1371748545,
242 'upload_date': '20130620',
243 'uploader_id': 'naomipq',
244 'uploader': 'B E A U T Y F O R A S H E S',
245 'like_count': int,
246 'comment_count': int,
247 'comments': list,
248 },
add96eb9 249 'add_ie': ['Instagram'],
fb2d1ee6 250 }]
251
fb2d1ee6 252 def _real_extract(self, url):
e3e606de
PD
253 video_id = _pk_to_id(self._match_id(url))
254 return self.url_result(f'http://instagram.com/tv/{video_id}', InstagramIE, video_id)
fb2d1ee6 255
256
8dcf65c9 257class InstagramIE(InstagramBaseIE):
06cb0638 258 _VALID_URL = r'(?P<url>https?://(?:www\.)?instagram\.com(?:/[^/]+)?/(?:p|tv|reels?(?!/audio/))/(?P<id>[^/?#&]+))'
bfd973ec 259 _EMBED_REGEX = [r'<iframe[^>]+src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?instagram\.com/p/[^/]+/embed.*?)\1']
4479600d 260 _TESTS = [{
fc6e75dd 261 'url': 'https://instagram.com/p/aye83DjauH/?foo=bar#abc',
0de668af
JMF
262 'md5': '0d2da106a9d2631273e192b372806516',
263 'info_dict': {
264 'id': 'aye83DjauH',
265 'ext': 'mp4',
0de668af
JMF
266 'title': 'Video by naomipq',
267 'description': 'md5:1f17f0ab29bd6fe2bfad705f58de3cb8',
ec85ded8 268 'thumbnail': r're:^https?://.*\.jpg',
2e767548 269 'duration': 8.747,
98960c91
S
270 'timestamp': 1371748545,
271 'upload_date': '20130620',
013322a9 272 'uploader_id': '2815873',
29f7c58a 273 'uploader': 'B E A U T Y F O R A S H E S',
013322a9 274 'channel': 'naomipq',
98960c91
S
275 'like_count': int,
276 'comment_count': int,
a56e74e2 277 'comments': list,
98960c91 278 },
2e767548
PD
279 'expected_warnings': [
280 'General metadata extraction failed',
281 'Main webpage is locked behind the login page',
282 ],
fb4b3458 283 }, {
2e767548
PD
284 # reel
285 'url': 'https://www.instagram.com/reel/Chunk8-jurw/',
286 'md5': 'f6d8277f74515fa3ff9f5791426e42b1',
fb4b3458 287 'info_dict': {
2e767548 288 'id': 'Chunk8-jurw',
fb4b3458 289 'ext': 'mp4',
2e767548
PD
290 'title': 'Video by instagram',
291 'description': 'md5:c9cde483606ed6f80fbe9283a6a2b290',
ec85ded8 292 'thumbnail': r're:^https?://.*\.jpg',
2e767548
PD
293 'duration': 5.016,
294 'timestamp': 1661529231,
295 'upload_date': '20220826',
296 'uploader_id': '25025320',
297 'uploader': 'Instagram',
298 'channel': 'instagram',
98960c91
S
299 'like_count': int,
300 'comment_count': int,
a56e74e2 301 'comments': list,
fb4b3458 302 },
2e767548
PD
303 'expected_warnings': [
304 'General metadata extraction failed',
305 'Main webpage is locked behind the login page',
306 ],
ada77fa5
S
307 }, {
308 # multi video post
309 'url': 'https://www.instagram.com/p/BQ0eAlwhDrw/',
310 'playlist': [{
311 'info_dict': {
312 'id': 'BQ0dSaohpPW',
313 'ext': 'mp4',
314 'title': 'Video 1',
2e767548
PD
315 'thumbnail': r're:^https?://.*\.jpg',
316 'view_count': int,
ada77fa5
S
317 },
318 }, {
319 'info_dict': {
320 'id': 'BQ0dTpOhuHT',
321 'ext': 'mp4',
322 'title': 'Video 2',
2e767548
PD
323 'thumbnail': r're:^https?://.*\.jpg',
324 'view_count': int,
ada77fa5
S
325 },
326 }, {
327 'info_dict': {
328 'id': 'BQ0dT7RBFeF',
329 'ext': 'mp4',
330 'title': 'Video 3',
2e767548
PD
331 'thumbnail': r're:^https?://.*\.jpg',
332 'view_count': int,
ada77fa5
S
333 },
334 }],
335 'info_dict': {
336 'id': 'BQ0eAlwhDrw',
337 'title': 'Post by instagram',
338 'description': 'md5:0f9203fc6a2ce4d228da5754bcf54957',
339 },
2e767548
PD
340 'expected_warnings': [
341 'General metadata extraction failed',
342 'Main webpage is locked behind the login page',
343 ],
cce889b9 344 }, {
345 # IGTV
346 'url': 'https://www.instagram.com/tv/BkfuX9UB-eK/',
347 'info_dict': {
348 'id': 'BkfuX9UB-eK',
349 'ext': 'mp4',
350 'title': 'Fingerboarding Tricks with @cass.fb',
351 'thumbnail': r're:^https?://.*\.jpg',
352 'duration': 53.83,
353 'timestamp': 1530032919,
354 'upload_date': '20180626',
013322a9 355 'uploader_id': '25025320',
cce889b9 356 'uploader': 'Instagram',
013322a9 357 'channel': 'instagram',
cce889b9 358 'like_count': int,
359 'comment_count': int,
360 'comments': list,
361 'description': 'Meet Cass Hirst (@cass.fb), a fingerboarding pro who can perform tiny ollies and kickflips while blindfolded.',
2e767548
PD
362 },
363 'expected_warnings': [
364 'General metadata extraction failed',
365 'Main webpage is locked behind the login page',
366 ],
4479600d
S
367 }, {
368 'url': 'https://instagram.com/p/-Cmh1cukG2/',
369 'only_matching': True,
0dafea02
S
370 }, {
371 'url': 'http://instagram.com/p/9o6LshA7zy/embed/',
372 'only_matching': True,
edb2820c
RA
373 }, {
374 'url': 'https://www.instagram.com/tv/aye83DjauH/',
375 'only_matching': True,
29f7c58a 376 }, {
377 'url': 'https://www.instagram.com/reel/CDUMkliABpa/',
378 'only_matching': True,
8b688881 379 }, {
380 'url': 'https://www.instagram.com/marvelskies.fc/reel/CWqAgUZgCku/',
381 'only_matching': True,
06cb0638
AP
382 }, {
383 'url': 'https://www.instagram.com/reels/Cop84x6u7CP/',
384 'only_matching': True,
4479600d 385 }]
59fc531f 386
bfd973ec 387 @classmethod
388 def _extract_embed_urls(cls, url, webpage):
389 res = tuple(super()._extract_embed_urls(url, webpage))
390 if res:
391 return res
c4096e8a 392
bfd973ec 393 mobj = re.search(r'<a[^>]+href=([\'"])(?P<link>[^\'"]+)\1',
394 get_element_by_attribute('class', 'instagram-media', webpage) or '')
c4096e8a 395 if mobj:
bfd973ec 396 return [mobj.group('link')]
c4096e8a 397
59fc531f 398 def _real_extract(self, url):
eb56d132 399 video_id, url = self._match_valid_url(url).group('id', 'url')
7d3b98be 400 media, webpage = {}, ''
401
2e767548
PD
402 if self._get_cookies(url).get('sessionid'):
403 info = traverse_obj(self._download_json(
404 f'{self._API_BASE_URL}/media/{_id_to_pk(video_id)}/info/', video_id,
405 fatal=False, errnote='Video info extraction failed',
406 note='Downloading video info', headers=self._API_HEADERS), ('items', 0))
407 if info:
408 media.update(info)
409 return self._extract_product(media)
410
7d3b98be 411 api_check = self._download_json(
412 f'{self._API_BASE_URL}/web/get_ruling_for_content/?content_type=MEDIA&target_id={_id_to_pk(video_id)}',
413 video_id, headers=self._API_HEADERS, fatal=False, note='Setting up session', errnote=False) or {}
414 csrf_token = self._get_cookies('https://www.instagram.com').get('csrftoken')
415
416 if not csrf_token:
417 self.report_warning('No csrf token set by Instagram API', video_id)
7d3b98be 418 else:
2e767548
PD
419 csrf_token = csrf_token.value if api_check.get('status') == 'ok' else None
420 if not csrf_token:
421 self.report_warning('Instagram API is not granting access', video_id)
422
423 variables = {
424 'shortcode': video_id,
425 'child_comment_count': 3,
426 'fetch_comment_count': 40,
427 'parent_comment_count': 24,
428 'has_threaded_comments': True,
429 }
430 general_info = self._download_json(
431 'https://www.instagram.com/graphql/query/', video_id, fatal=False, errnote=False,
432 headers={
433 **self._API_HEADERS,
434 'X-CSRFToken': csrf_token or '',
435 'X-Requested-With': 'XMLHttpRequest',
436 'Referer': url,
437 }, query={
438 'query_hash': '9f8827793ef34641b2fb195d4d41151c',
439 'variables': json.dumps(variables, separators=(',', ':')),
440 })
441 media.update(traverse_obj(general_info, ('data', 'shortcode_media')) or {})
442
443 if not general_info:
7d3b98be 444 self.report_warning('General metadata extraction failed (some metadata might be missing).', video_id)
445 webpage, urlh = self._download_webpage_handle(url, video_id)
446 shared_data = self._search_json(
8a3da4c6 447 r'window\._sharedData\s*=', webpage, 'shared data', video_id, fatal=False) or {}
7d3b98be 448
3d2623a8 449 if shared_data and self._LOGIN_URL not in urlh.url:
7d3b98be 450 media.update(traverse_obj(
451 shared_data, ('entry_data', 'PostPage', 0, 'graphql', 'shortcode_media'),
452 ('entry_data', 'PostPage', 0, 'media'), expected_type=dict) or {})
453 else:
2e767548 454 self.report_warning('Main webpage is locked behind the login page. Retrying with embed webpage (some metadata might be missing).')
7d3b98be 455 webpage = self._download_webpage(
456 f'{url}/embed/', video_id, note='Downloading embed webpage', fatal=False)
457 additional_data = self._search_json(
304ad45a 458 r'window\.__additionalDataLoaded\s*\(\s*[^,]+,', webpage, 'additional data', video_id, fatal=False)
2e767548 459 if not additional_data and not media:
8a3da4c6 460 self.raise_login_required('Requested content is not available, rate-limit reached or login required')
7d3b98be 461
462 product_item = traverse_obj(additional_data, ('items', 0), expected_type=dict)
463 if product_item:
464 media.update(product_item)
465 return self._extract_product(media)
466
467 media.update(traverse_obj(
468 additional_data, ('graphql', 'shortcode_media'), 'shortcode_media', expected_type=dict) or {})
eb56d132 469
013322a9
M
470 username = traverse_obj(media, ('owner', 'username')) or self._search_regex(
471 r'"owner"\s*:\s*{\s*"username"\s*:\s*"(.+?)"', webpage, 'username', fatal=False)
eb56d132 472
473 description = (
474 traverse_obj(media, ('edge_media_to_caption', 'edges', 0, 'node', 'text'), expected_type=str)
475 or media.get('caption'))
476 if not description:
477 description = self._search_regex(
478 r'"caption"\s*:\s*"(.+?)"', webpage, 'description', default=None)
479 if description is not None:
480 description = lowercase_escape(description)
98960c91 481
eb56d132 482 video_url = media.get('video_url')
98960c91 483 if not video_url:
eb56d132 484 nodes = traverse_obj(media, ('edge_sidecar_to_children', 'edges', ..., 'node'), expected_type=dict) or []
485 if nodes:
486 return self.playlist_result(
487 self._extract_nodes(nodes, True), video_id,
a70635b8 488 format_field(username, None, 'Post by %s'), description)
eb56d132 489
98960c91
S
490 video_url = self._og_search_video_url(webpage, secure=False)
491
16097822
DR
492 formats = [{
493 'url': video_url,
eb56d132 494 'width': self._get_dimension('width', media, webpage),
495 'height': self._get_dimension('height', media, webpage),
16097822 496 }]
eb56d132 497 dash = traverse_obj(media, ('dash_info', 'video_dash_manifest'))
cd9ea410 498 if dash:
499 formats.extend(self._parse_mpd_formats(self._parse_xml(dash, video_id), mpd_id='dash'))
16097822 500
4e260d1a 501 comment_data = traverse_obj(media, ('edge_media_to_parent_comment', 'edges'))
eb56d132 502 comments = [{
503 'author': traverse_obj(comment_dict, ('node', 'owner', 'username')),
504 'author_id': traverse_obj(comment_dict, ('node', 'owner', 'id')),
505 'id': traverse_obj(comment_dict, ('node', 'id')),
506 'text': traverse_obj(comment_dict, ('node', 'text')),
507 'timestamp': traverse_obj(comment_dict, ('node', 'created_at'), expected_type=int_or_none),
4e260d1a 508 } for comment_dict in comment_data] if comment_data else None
eb56d132 509
510 display_resources = (
511 media.get('display_resources')
512 or [{'src': media.get(key)} for key in ('display_src', 'display_url')]
513 or [{'src': self._og_search_thumbnail(webpage)}])
514 thumbnails = [{
515 'url': thumbnail['src'],
516 'width': thumbnail.get('config_width'),
517 'height': thumbnail.get('config_height'),
518 } for thumbnail in display_resources if thumbnail.get('src')]
59fc531f 519
0de668af
JMF
520 return {
521 'id': video_id,
16097822 522 'formats': formats,
add96eb9 523 'title': media.get('title') or f'Video by {username}',
98960c91 524 'description': description,
eb56d132 525 'duration': float_or_none(media.get('video_duration')),
526 'timestamp': traverse_obj(media, 'taken_at_timestamp', 'date', expected_type=int_or_none),
013322a9 527 'uploader_id': traverse_obj(media, ('owner', 'id')),
eb56d132 528 'uploader': traverse_obj(media, ('owner', 'full_name')),
013322a9 529 'channel': username,
4e260d1a
M
530 'like_count': self._get_count(media, 'likes', 'preview_like') or str_to_int(self._search_regex(
531 r'data-log-event="likeCountClick"[^>]*>[^\d]*([\d,\.]+)', webpage, 'like count', fatal=False)),
eb56d132 532 'comment_count': self._get_count(media, 'comments', 'preview_comment', 'to_comment', 'to_parent_comment'),
a56e74e2 533 'comments': comments,
eb56d132 534 'thumbnails': thumbnails,
3dd39c5f
S
535 'http_headers': {
536 'Referer': 'https://www.instagram.com/',
add96eb9 537 },
0de668af 538 }
ea38e55f
PH
539
540
8dcf65c9 541class InstagramPlaylistBaseIE(InstagramBaseIE):
31fbedc0 542 _gis_tmpl = None # used to cache GIS request type
ea38e55f 543
31fbedc0 544 def _parse_graphql(self, webpage, item_id):
545 # Reads a webpage and returns its GraphQL data.
546 return self._parse_json(
547 self._search_regex(
548 r'sharedData\s*=\s*({.+?})\s*;\s*[<\n]', webpage, 'data'),
549 item_id)
238d42cf 550
31fbedc0 551 def _extract_graphql(self, data, url):
552 # Parses GraphQL queries containing videos and generates a playlist.
31fbedc0 553 uploader_id = self._match_id(url)
dd9aea8c
S
554 csrf_token = data['config']['csrf_token']
555 rhx_gis = data.get('rhx_gis') or '3c7ca9dcefcf966d11dacf1f151335e8'
556
cba5d1b6
S
557 cursor = ''
558 for page_num in itertools.count(1):
31fbedc0 559 variables = {
9b3036bd 560 'first': 12,
dd9aea8c 561 'after': cursor,
31fbedc0 562 }
563 variables.update(self._query_vars_for(data))
564 variables = json.dumps(variables)
238d42cf
S
565
566 if self._gis_tmpl:
567 gis_tmpls = [self._gis_tmpl]
568 else:
569 gis_tmpls = [
add96eb9 570 f'{rhx_gis}',
238d42cf 571 '',
add96eb9 572 f'{rhx_gis}:{csrf_token}',
573 '{}:{}:{}'.format(rhx_gis, csrf_token, self.get_param('http_headers')['User-Agent']),
238d42cf
S
574 ]
575
31fbedc0 576 # try all of the ways to generate a GIS query, and not only use the
577 # first one that works, but cache it for future requests
238d42cf
S
578 for gis_tmpl in gis_tmpls:
579 try:
31fbedc0 580 json_data = self._download_json(
238d42cf 581 'https://www.instagram.com/graphql/query/', uploader_id,
add96eb9 582 f'Downloading JSON page {page_num}', headers={
238d42cf
S
583 'X-Requested-With': 'XMLHttpRequest',
584 'X-Instagram-GIS': hashlib.md5(
add96eb9 585 (f'{gis_tmpl}:{variables}').encode()).hexdigest(),
238d42cf 586 }, query={
31fbedc0 587 'query_hash': self._QUERY_HASH,
238d42cf 588 'variables': variables,
31fbedc0 589 })
590 media = self._parse_timeline_from(json_data)
238d42cf
S
591 self._gis_tmpl = gis_tmpl
592 break
593 except ExtractorError as e:
31fbedc0 594 # if it's an error caused by a bad query, and there are
595 # more GIS templates to try, ignore it and keep trying
3d2623a8 596 if isinstance(e.cause, HTTPError) and e.cause.status == 403:
238d42cf
S
597 if gis_tmpl != gis_tmpls[-1]:
598 continue
599 raise
cba5d1b6 600
eb56d132 601 nodes = traverse_obj(media, ('edges', ..., 'node'), expected_type=dict) or []
602 if not nodes:
cba5d1b6 603 break
eb56d132 604 yield from self._extract_nodes(nodes)
cba5d1b6 605
eb56d132 606 has_next_page = traverse_obj(media, ('page_info', 'has_next_page'))
607 cursor = traverse_obj(media, ('page_info', 'end_cursor'), expected_type=str)
608 if not has_next_page or not cursor:
cba5d1b6 609 break
5fc12b95
S
610
611 def _real_extract(self, url):
31fbedc0 612 user_or_tag = self._match_id(url)
613 webpage = self._download_webpage(url, user_or_tag)
614 data = self._parse_graphql(webpage, user_or_tag)
dd9aea8c 615
31fbedc0 616 self._set_cookie('instagram.com', 'ig_pr', '1')
dd9aea8c 617
5fc12b95 618 return self.playlist_result(
31fbedc0 619 self._extract_graphql(data, url), user_or_tag, user_or_tag)
620
621
8dcf65c9 622class InstagramUserIE(InstagramPlaylistBaseIE):
df773c3d 623 _WORKING = False
31fbedc0 624 _VALID_URL = r'https?://(?:www\.)?instagram\.com/(?P<id>[^/]{2,})/?(?:$|[?#])'
625 IE_DESC = 'Instagram user profile'
626 IE_NAME = 'instagram:user'
8dcf65c9 627 _TESTS = [{
31fbedc0 628 'url': 'https://instagram.com/porsche',
629 'info_dict': {
630 'id': 'porsche',
631 'title': 'porsche',
632 },
633 'playlist_count': 5,
634 'params': {
635 'extract_flat': True,
636 'skip_download': True,
637 'playlistend': 5,
add96eb9 638 },
8dcf65c9 639 }]
31fbedc0 640
add96eb9 641 _QUERY_HASH = ('42323d64886122307be10013ad2dcc44',)
31fbedc0 642
643 @staticmethod
644 def _parse_timeline_from(data):
645 # extracts the media timeline data from a GraphQL result
646 return data['data']['user']['edge_owner_to_timeline_media']
647
648 @staticmethod
649 def _query_vars_for(data):
650 # returns a dictionary of variables to add to the timeline query based
651 # on the GraphQL of the original page
652 return {
add96eb9 653 'id': data['entry_data']['ProfilePage'][0]['graphql']['user']['id'],
31fbedc0 654 }
655
656
8dcf65c9 657class InstagramTagIE(InstagramPlaylistBaseIE):
31fbedc0 658 _VALID_URL = r'https?://(?:www\.)?instagram\.com/explore/tags/(?P<id>[^/]+)'
f304da8a 659 IE_DESC = 'Instagram hashtag search URLs'
31fbedc0 660 IE_NAME = 'instagram:tag'
8dcf65c9 661 _TESTS = [{
31fbedc0 662 'url': 'https://instagram.com/explore/tags/lolcats',
663 'info_dict': {
664 'id': 'lolcats',
665 'title': 'lolcats',
666 },
667 'playlist_count': 50,
668 'params': {
669 'extract_flat': True,
670 'skip_download': True,
671 'playlistend': 50,
add96eb9 672 },
8dcf65c9 673 }]
31fbedc0 674
add96eb9 675 _QUERY_HASH = ('f92f56d47dc7a55b606908374b43a314',)
31fbedc0 676
677 @staticmethod
678 def _parse_timeline_from(data):
679 # extracts the media timeline data from a GraphQL result
680 return data['data']['hashtag']['edge_hashtag_to_media']
681
682 @staticmethod
683 def _query_vars_for(data):
684 # returns a dictionary of variables to add to the timeline query based
685 # on the GraphQL of the original page
686 return {
687 'tag_name':
add96eb9 688 data['entry_data']['TagPage'][0]['graphql']['hashtag']['name'],
31fbedc0 689 }
dd5e60b1 690
691
692class InstagramStoryIE(InstagramBaseIE):
693 _VALID_URL = r'https?://(?:www\.)?instagram\.com/stories/(?P<user>[^/]+)/(?P<id>\d+)'
694 IE_NAME = 'instagram:story'
695
696 _TESTS = [{
697 'url': 'https://www.instagram.com/stories/highlights/18090946048123978/',
698 'info_dict': {
699 'id': '18090946048123978',
700 'title': 'Rare',
701 },
add96eb9 702 'playlist_mincount': 50,
dd5e60b1 703 }]
704
705 def _real_extract(self, url):
706 username, story_id = self._match_valid_url(url).groups()
e3e606de
PD
707 story_info = self._download_webpage(url, story_id)
708 user_info = self._search_json(r'"user":', story_info, 'user info', story_id, fatal=False)
709 if not user_info:
710 self.raise_login_required('This content is unreachable')
dd5e60b1 711
50eaea9f 712 user_id = traverse_obj(user_info, 'pk', 'id', expected_type=str)
dd5e60b1 713 story_info_url = user_id if username != 'highlights' else f'highlight:{story_id}'
50eaea9f 714 if not story_info_url: # user id is only mandatory for non-highlights
715 raise ExtractorError('Unable to extract user id')
716
e3e606de 717 videos = traverse_obj(self._download_json(
7d3b98be 718 f'{self._API_BASE_URL}/feed/reels_media/?reel_ids={story_info_url}',
719 story_id, errnote=False, fatal=False, headers=self._API_HEADERS), 'reels')
e3e606de
PD
720 if not videos:
721 self.raise_login_required('You need to log in to access this content')
f7085283 722
50eaea9f 723 full_name = traverse_obj(videos, (f'highlight:{story_id}', 'user', 'full_name'), (user_id, 'user', 'full_name'))
e3e606de
PD
724 story_title = traverse_obj(videos, (f'highlight:{story_id}', 'title'))
725 if not story_title:
726 story_title = f'Story by {username}'
f7085283 727
50eaea9f 728 highlights = traverse_obj(videos, (f'highlight:{story_id}', 'items'), (user_id, 'items'))
e3e606de
PD
729 info_data = []
730 for highlight in highlights:
731 highlight_data = self._extract_product(highlight)
732 if highlight_data.get('formats'):
733 info_data.append({
e3e606de
PD
734 'uploader': full_name,
735 'uploader_id': user_id,
50eaea9f 736 **filter_dict(highlight_data),
e3e606de
PD
737 })
738 return self.playlist_result(info_data, playlist_id=story_id, playlist_title=story_title)