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

Code before:
        self._set_cookie('steampowered.com', 'birthtime', '944006401')
        self._set_cookie('steampowered.com', 'lastagecheckage', '1-0-2000')

        webpage = self._download_webpage(video_url, playlist_id)

        if re.search('<div[^>]+>Please enter your birth date to continue:</div>', webpage) is not None:
            video_url = self._AGECHECK_TEMPLATE % playlist_id
            self.report_age_confirmation()
            webpage = self._download_webpage(video_url, playlist_id)

        videos = re.findall(r'(<div[^>]+id=[\'"]highlight_movie_(\d+)[\'"][^>]+>)', webpage)
        entries = []
        playlist_title = get_element_by_class('apphub_AppName', webpage)
        for movie, movie_id in videos:
            if not movie:
                continue
            movie = extract_attributes(movie)
            if not movie_id:
                continue
            entry = {
                'id': movie_id,
                'title': f'{playlist_title} video {movie_id}',
            }
            formats = []
            if movie:
                data = self._parse_json(movie['data-props'], video_id=movie_id)

                entry['thumbnail'] = data.get('screenshot')

                for dash_url in data.get('dashManifests', []):
                    formats.extend(self._extract_mpd_formats(
                        dash_url, movie_id, mpd_id='dash', fatal=False))

                hls_url = data.get('hlsManifest')
                if hls_url:
                    formats.extend(self._extract_m3u8_formats(
                        hls_url, movie_id, entry_protocol='m3u8', m3u8_id='hls', fatal=False))

            entry['formats'] = formats
            entries.append(entry)
        embedded_videos = re.findall(r'(<iframe[^>]+>)', webpage)
        for evideos in embedded_videos:
            evideos = extract_attributes(evideos).get('src')
            video_id = self._search_regex(r'youtube\.com/embed/([0-9A-Za-z_-]{11})', evideos, 'youtube_video_id', default=None)
            if video_id:
                entries.append({
                    '_type': 'url_transparent',
                    'id': video_id,
                    'url': video_id,
                    'ie_key': 'Youtube',
                })

Review comment (refactor):
can be simplified as below
```diff
@@ -1,11 +1,19 @@
+import json
 import re

 from .common import InfoExtractor
 from ..utils import (
     ExtractorError,
+    clean_html,
     extract_attributes,
-    get_element_by_class,
     str_or_none,
+    url_or_none,
+)
+from ..utils.traversal import (
+    find_element,
+    find_elements,
+    traverse_obj,
+    trim_str,
 )


```
```diff
@@ -81,36 +67,31 @@ def _real_extract(self, url):
             self.report_age_confirmation()
             webpage = self._download_webpage(video_url, playlist_id)

-        videos = re.findall(r'(<div[^>]+id=[\'"]highlight_movie_(\d+)[\'"][^>]+>)', webpage)
+        app_name = traverse_obj(webpage, ({find_element(cls='apphub_AppName')}, {clean_html}))
+
         entries = []
-        playlist_title = get_element_by_class('apphub_AppName', webpage)
-        for movie, movie_id in videos:
-            if not movie:
-                continue
-            movie = extract_attributes(movie)
-            if not movie_id:
-                continue
-            entry = {
-                'id': movie_id,
-                'title': f'{playlist_title} video {movie_id}',
-            }
+        for data_prop in traverse_obj(webpage, (
+            {find_elements(cls='highlight_player_item highlight_movie', html=True)},
+            ..., {extract_attributes}, 'data-props', {json.loads}, {dict},
+        )):
             formats = []
-            if movie:
-                data = self._parse_json(movie['data-props'], video_id=movie_id)
-
-                entry['thumbnail'] = data.get('screenshot')
-
-                for dash_url in data.get('dashManifests', []):
-                    formats.extend(self._extract_mpd_formats(
-                        dash_url, movie_id, mpd_id='dash', fatal=False))
-
-                hls_url = data.get('hlsManifest')
-                if hls_url:
-                    formats.extend(self._extract_m3u8_formats(
-                        hls_url, movie_id, entry_protocol='m3u8', m3u8_id='hls', fatal=False))
+            if hls_manifest := traverse_obj(data_prop, ('hlsManifest', {url_or_none})):
+                formats.extend(self._extract_m3u8_formats(
+                    hls_manifest, playlist_id, 'mp4', m3u8_id='hls', fatal=False))
+            for dash_manifest in traverse_obj(data_prop, (
+                'dashManifests', ..., {url_or_none}, filter, all, filter,
+            )):
+                formats.extend(self._extract_mpd_formats(
+                    dash_manifest, playlist_id, mpd_id='dash', fatal=False))
+
+            movie_id = traverse_obj(data_prop, ('id', {trim_str(start='highlight_movie_')}))
+            entries.append({
+                'id': movie_id,
+                'title': f'{app_name} video {movie_id}',
+                'formats': formats,
+                'thumbnail': traverse_obj(data_prop, ('screenshot', {url_or_none})),
+            })

-            entry['formats'] = formats
-            entries.append(entry)
         embedded_videos = re.findall(r'(<iframe[^>]+>)', webpage)
         for evideos in embedded_videos:
             evideos = extract_attributes(evideos).get('src')
```

```diff
@@ -125,7 +106,7 @@ def _real_extract(self, url):
         if not entries:
             raise ExtractorError('Could not find any videos')

-        return self.playlist_result(entries, playlist_id, playlist_title)
+        return self.playlist_result(entries, playlist_id, app_name)


 class SteamCommunityBroadcastIE(InfoExtractor):
 ```

Code after:
            ..., {extract_attributes}, 'data-props', {json.loads}, {dict},
        )):
            formats = []
            if hls_manifest := traverse_obj(data_prop, ('hlsManifest', {url_or_none})):
                formats.extend(self._extract_m3u8_formats(
                    hls_manifest, playlist_id, 'mp4', m3u8_id='hls', fatal=False))

            for dash_manifest in traverse_obj(data_prop, ('dashManifests', ..., {url_or_none})):
                formats.extend(self._extract_mpd_formats(
                    dash_manifest, playlist_id, mpd_id='dash', fatal=False))

            movie_id = traverse_obj(data_prop, ('id', {trim_str(start='highlight_movie_')}))
            entries.append({
                'id': movie_id,
                'title': f'{app_name} video {movie_id}',
                'formats': formats,
                'thumbnail': traverse_obj(data_prop, ('screenshot', {url_or_none})),
            })

        embedded_videos = re.findall(r'(<iframe[^>]+>)', webpage)
        for evideos in embedded_videos:
            evideos = extract_attributes(evideos).get('src')
            video_id = self._search_regex(r'youtube\.com/embed/([0-9A-Za-z_-]{11})', evideos, 'youtube_video_id', default=None)
            if video_id:
                entries.append({
                    '_type': 'url_transparent',
                    'id': video_id,
                    'url': video_id,
                    'ie_key': 'Youtube',
                })
        if not entries:
            raise ExtractorError('Could not find any videos')

        return self.playlist_result(entries, playlist_id, app_name)


class SteamCommunityBroadcastIE(InfoExtractor):
    _VALID_URL = r'https?://steamcommunity\.(?:com)/broadcast/watch/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://steamcommunity.com/broadcast/watch/76561199073851486',
        'info_dict': {
            'id': '76561199073851486',
            'title': r're:Steam Community :: pepperm!nt :: Broadcast 2022-06-26 \d{2}:\d{2}',
            'ext': 'mp4',
            'uploader_id': '1113585758',
            'uploader': 'pepperm!nt',
            'live_status': 'is_live',
        },
        'skip': 'Stream has ended',
    }]