Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/instagram.py (Python)

Code before:
            'description':
            traverse_obj(info, ('caption', 'text'), expected_type=str),
            'duration':
            float_or_none(info.get('video_duration')),
            'timestamp':
            traverse_obj(info, 'taken_at', expected_type=int_or_none),
            'uploader_id':
            str(traverse_obj(info, ('user', 'pk'), expected_type=int)),
            'uploader':
            traverse_obj(info, ('user', 'full_name'), expected_type=str),
            'channel':
            username,
            'like_count':
            traverse_obj(info, 'like_count', expected_type=int_or_none),
            'comment_count':
            traverse_obj(info, 'comment_count', expected_type=int_or_none),
            'thumbnails': [{
                'url': thumbnail['url'],
                'width': thumbnail.get('width'),
                'height': thumbnail.get('height'),
            } for thumbnail in info.get('image_versions2', {}).get(
                'candidates', []) if thumbnail.get('url')],
            'http_headers': {
                'Referer': 'https://www.instagram.com/',
            }
        }

    def _real_extract(self, url):
        video_id, url = self._match_valid_url(url).group('id', 'url')
        webpage, urlh = self._download_webpage_handle(url, video_id)
        if not self._cookies_passed:
            if 'www.instagram.com/accounts/login' in urlh.geturl():
                self.report_warning(
                    'Main webpage is locked behind the login page. '
                    'Retrying with embed webpage (Note that some metadata might '
                    'be missing)')
                webpage = self._download_webpage(
                    f'https://www.instagram.com/p/{video_id}/embed/',
                    video_id,
                    note='Downloading embed webpage')

            shared_data = self._parse_json(
                self._search_regex(
                    r'window\._sharedData\s*=\s*({.+?});',
                    webpage, 'shared data', default='{}'),
                video_id, fatal=False)
            media = traverse_obj(
                shared_data,
                ('entry_data', 'PostPage', 0, 'graphql', 'shortcode_media'),
                ('entry_data', 'PostPage', 0, 'media'),
                expected_type=dict)

Review comment (refactor):
please give an example of when
```
return self._extract_product(info)
```
wouldn't work, as it'd dedupe most of the code and also solves the multi-video issue

Code after:

        username = traverse_obj(media, ('owner', 'username')) or self._search_regex(
            r'"owner"\s*:\s*{\s*"username"\s*:\s*"(.+?)"', webpage, 'username', fatal=False)

        description = (
            traverse_obj(media, ('edge_media_to_caption', 'edges', 0, 'node', 'text'), expected_type=str)
            or media.get('caption'))
        if not description:
            description = self._search_regex(
                r'"caption"\s*:\s*"(.+?)"', webpage, 'description', default=None)
            if description is not None:
                description = lowercase_escape(description)

        video_url = media.get('video_url')
        if not video_url:
            nodes = traverse_obj(media, ('edge_sidecar_to_children', 'edges', ..., 'node'), expected_type=dict) or []
            if nodes:
                return self.playlist_result(
                    self._extract_nodes(nodes, True), video_id,
                    format_field(username, None, 'Post by %s'), description)

            video_url = self._og_search_video_url(webpage, secure=False)

        formats = [{
            'url': video_url,
            'width': self._get_dimension('width', media, webpage),
            'height': self._get_dimension('height', media, webpage),
        }]
        dash = traverse_obj(media, ('dash_info', 'video_dash_manifest'))
        if dash:
            formats.extend(self._parse_mpd_formats(self._parse_xml(dash, video_id), mpd_id='dash'))
        self._sort_formats(formats)

        comment_data = traverse_obj(media, ('edge_media_to_parent_comment', 'edges'))
        comments = [{
            'author': traverse_obj(comment_dict, ('node', 'owner', 'username')),
            'author_id': traverse_obj(comment_dict, ('node', 'owner', 'id')),
            'id': traverse_obj(comment_dict, ('node', 'id')),
            'text': traverse_obj(comment_dict, ('node', 'text')),
            'timestamp': traverse_obj(comment_dict, ('node', 'created_at'), expected_type=int_or_none),
        } for comment_dict in comment_data] if comment_data else None

        display_resources = (
            media.get('display_resources')
            or [{'src': media.get(key)} for key in ('display_src', 'display_url')]
            or [{'src': self._og_search_thumbnail(webpage)}])
        thumbnails = [{
            'url': thumbnail['src'],
            'width': thumbnail.get('config_width'),
            'height': thumbnail.get('config_height'),
        } for thumbnail in display_resources if thumbnail.get('src')]