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

Code before:
    _VALID_URL = r'https?://(?:www\.)?instagram\.com/stories/(?P<user>[^/]+)/(?P<id>\d+)'
    IE_NAME = 'instagram:story'

    _TESTS = [{
        'url': 'https://www.instagram.com/stories/highlights/18090946048123978/',
        'info_dict': {
            'id': '18090946048123978',
            'title': 'Rare',
        },
        'playlist_mincount': 50
    }]

    def _real_extract(self, url):
        username, story_id = self._match_valid_url(url).groups()

        story_info, wh = self._download_webpage_handle(url, story_id,
            errnote='Can\'t fetch content (Note that some metadata might be missing)')
        if 'www.instagram.com/accounts/login' in wh.geturl():
            self.raise_login_required('You need to log in to access this content')
        user_id = self._search_regex(r'"user":{"id":"(\d*)"', story_info, 'user id')
        highlight_title = traverse_obj(story_info, ('highlight', 'title'))

        story_info_url = user_id if username != 'highlights' else f'highlight:{story_id}'
        videos = self._download_json(f'https://i.instagram.com/api/v1/feed/reels_media/?reel_ids={story_info_url}', story_id, headers={
            'X-IG-App-ID': 936619743392459,
            'X-ASBD-ID': 198387,
            'X-IG-WWW-Claim': 0,
        })['reels']

        full_name = traverse_obj(videos, ('user', 'full_name'))

        user_info = {}
        if not (username and username != 'highlights' and full_name):
            user_info = self._download_json(
                f'https://i.instagram.com/api/v1/users/{user_id}/info/', story_id, headers={
                    'User-Agent': 'Mozilla/5.0 (Linux; Android 11; SM-A505F Build/RP1A.200720.012; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/96.0.4664.45 Mobile Safari/537.36 Instagram 214.1.0.29.120 Android (30/11; 450dpi; 1080x2122; samsung; SM-A505F; a50; exynos9610; en_US; 333717274)',
                }, note='Downloading user info')

        username = traverse_obj(user_info, ('user', 'username')) or username
        full_name = traverse_obj(user_info, ('user', 'full_name')) or full_name

        highlights = traverse_obj(videos, (f'highlight:{story_id}', 'items'), (str(user_id), 'items'))
        return self.playlist_result([{
            **self._extract_product(highlight),
            'title': f'Story by {username}',
            'uploader': full_name,
            'uploader_id': user_id,
        } for highlight in highlights], playlist_id=story_id, playlist_title=highlight_title)


Review comment (refactor):
highlight_title -> story_title and must come after json extraction
```python
story_title = traverse_obj(videos, ('title'))
if not story_title:
    story_title = f'Story by {traverse_obj(videos, ('username'))}'
                       or
    story_title = f'Story by {videos[user_id]['user']['username']}'
```

Code after:
                data['entry_data']['TagPage'][0]['graphql']['hashtag']['name']
        }


class InstagramStoryIE(InstagramBaseIE):
    _VALID_URL = r'https?://(?:www\.)?instagram\.com/stories/(?P<user>[^/]+)/(?P<id>\d+)'
    IE_NAME = 'instagram:story'

    _TESTS = [{
        'url': 'https://www.instagram.com/stories/highlights/18090946048123978/',
        'info_dict': {
            'id': '18090946048123978',
            'title': 'Rare',
        },
        'playlist_mincount': 50
    }]

    def _real_extract(self, url):
        username, story_id = self._match_valid_url(url).groups()
        story_info = self._download_webpage(url, story_id)
        user_info = self._search_json(r'"user":', story_info, 'user info', story_id, fatal=False)
        if not user_info:
            self.raise_login_required('This content is unreachable')
        user_id = user_info.get('id')

        story_info_url = user_id if username != 'highlights' else f'highlight:{story_id}'
        videos = traverse_obj(self._download_json(
            f'https://i.instagram.com/api/v1/feed/reels_media/?reel_ids={story_info_url}',
            story_id, errnote=False, fatal=False, headers={
                'X-IG-App-ID': 936619743392459,
                'X-ASBD-ID': 198387,
                'X-IG-WWW-Claim': 0,
            }), 'reels')
        if not videos:
            self.raise_login_required('You need to log in to access this content')

        full_name = traverse_obj(videos, (f'highlight:{story_id}', 'user', 'full_name'), (str(user_id), 'user', 'full_name'))
        story_title = traverse_obj(videos, (f'highlight:{story_id}', 'title'))
        if not story_title:
            story_title = f'Story by {username}'

        highlights = traverse_obj(videos, (f'highlight:{story_id}', 'items'), (str(user_id), 'items'))
        info_data = []
        for highlight in highlights:
            highlight_data = self._extract_product(highlight)
            if highlight_data.get('formats'):
                info_data.append({
                    **highlight_data,
                    'uploader': full_name,
                    'uploader_id': user_id,
                })