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

Code before:

            svt_id = try_get(
                data, lambda x: x['statistics']['dataLake']['content']['id'],
                str)

        if not svt_id:
            nextjs_data = self._search_nextjs_data(webpage, video_id, fatal=False)
            svt_id = traverse_obj(nextjs_data, (
                'props', 'urqlState', ..., 'data', {json.loads}, 'detailsPageByPath',
                'video', 'svtId', {str}), get_all=False)
            if not svt_id:
                details = traverse_obj(nextjs_data, (
                    'props', 'urqlState', ..., 'data', {json.loads}, 'detailsPageByPath',
                    'modules', ..., 'details'), get_all=False)
                description = details['description']
                svt_id = traverse_obj(details, (
                    'smartStart', 'item', 'videos', ..., 'svtId', {str}), get_all=False)

        if not svt_id:
            svt_id = self._search_regex(
                (r'<video[^>]+data-video-id=["\']([\da-zA-Z-]+)',
                 r'<[^>]+\bdata-rt=["\']top-area-play-button["\'][^>]+\bhref=["\'][^"\']*video/[\w-]+/[^"\']*\b(?:modalId|id)=([\w-]+)'),
                webpage, 'video id')

        info_dict = self._extract_by_video_id(svt_id, webpage)
        info_dict['description'] = description
        info_dict['thumbnail'] = thumbnail

        return info_dict


class SVTSeriesIE(SVTPlayBaseIE):
    _VALID_URL = r'https?://(?:www\.)?svtplay\.se/(?P<id>[^/?&#]+)(?:.+?\btab=(?P<season_slug>[^&#]+))?'
    _TESTS = [{
        'url': 'https://www.svtplay.se/rederiet',
        'info_dict': {
            'id': '14445680',
            'title': 'Rederiet',
            'description': 'md5:d9fdfff17f5d8f73468176ecd2836039',
        },
        'playlist_mincount': 318,
    }, {
        'url': 'https://www.svtplay.se/rederiet?tab=season-2-14445680',
        'info_dict': {
            'id': 'season-2-14445680',
            'title': 'Rederiet - Säsong 2',
            'description': 'md5:d9fdfff17f5d8f73468176ecd2836039',
        },
        'playlist_mincount': 12,
    }]


Review comment (refactor):
`description` is unbound unless the `if not svt_id: ..; if not svt_id: ...` code path is followed above. And since the tests were both updated with a description, I think it's safe to assume that we can shed all the other code paths.

I am all for simplifying this extractor, since right now it looks to be 1 path of working code bandaged atop of legacy code atop legacy code atop legacy code.

```py
    def _real_extract(self, url):
        mobj = self._match_valid_url(url)
        video_id = mobj.group('id')
        svt_id = mobj.group('svt_id') or mobj.group('modal_id')
        if svt_id:
            return self._extract_by_video_id(svt_id)

        webpage = self._download_webpage(url, video_id)

        data = traverse_obj(self._search_nextjs_data(webpage, video_id), (
            'props', 'urqlState', ..., 'data', {json.loads},
            'detailsPageByPath', {dict}, any, {require('video data')}))
        details = traverse_obj(data, (
            'modules', lambda _, v: v['details']['smartStart']['item']['videos'],
            'details', {dict}, any))
        svt_id = traverse_obj(details, (
            'smartStart', 'item', 'videos',
            # There can be 'AudioDescribed' and 'SignInterpreted' variants; try 'Default' or else get first
            (lambda _, v: v['accessibility'] == 'Default', 0),
            'svtId', {str}, any))
        if not svt_id:
            svt_id = traverse_obj(data, ('video', 'svtId', {str}, {require('SVT ID')}))

        info_dict = self._extract_by_video_id(svt_id, webpage)
        if not info_dict.get('description'):
            info_dict['description'] = traverse_obj(details, ('description', {str}))
        info_dict['thumbnail'] = self._og_search_thumbnail(webpage)

        return info_dict
```

Code after:

        series = self._download_json(
            'https://api.svt.se/contento/graphql', series_slug,
            'Downloading series page', query={
                'query': '''{
  listablesBySlug(slugs: ["%s"]) {
    associatedContent(include: [productionPeriod, season]) {
      items {
        item {
          ... on Episode {
            videoSvtId
          }
        }
      }
      id
      name
    }
    id
    longDescription
    name
    shortDescription
  }
}''' % series_slug,  # noqa: UP031
            })['data']['listablesBySlug'][0]

        season_name = None

        entries = []
        for season in series['associatedContent']:
            if not isinstance(season, dict):
                continue
            if season_id:
                if season.get('id') != season_id:
                    continue
                season_name = season.get('name')
            items = season.get('items')
            if not isinstance(items, list):
                continue
            for item in items:
                video = item.get('item') or {}
                content_id = video.get('videoSvtId')
                if not content_id or not isinstance(content_id, str):
                    continue
                entries.append(self.url_result(
                    'svt:' + content_id, SVTPlayIE.ie_key(), content_id))

        title = series.get('name')
        season_name = season_name or season_id

        if title and season_name:
            title = f'{title} - {season_name}'