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

Code before:
            'series': try_get(product, lambda x: traverse_obj(x, ('collection', 'title'), expected_type=str)),
            'age_limit': parse_age_limit(try_get(product, lambda x: traverse_obj(x, ('rating', 'name'), expected_type=str))),
        }


class TV5UnisVideoIE(TV5UnisBaseIE):
    IE_NAME = 'tv5unis:video'
    _VALID_URL = r'https?://(?:www\.)?tv5unis\.ca/videos/[^/]+/(?P<id>\d+)'
    _TEST = {
        'url': 'https://www.tv5unis.ca/videos/bande-annonces/144041',
        'md5': '24a247c96119d77fe1bae8b440457dfa',
        'info_dict': {
            'id': '56862325352147149dce0ae139afced6',
            'ext': 'mp4',
            'title': 'Antigone',
            'description': r"re:En aidant son frère .+ dicté par l'amour et la solidarité.",
            'duration': 61,
            'tags': 'count:0',
        },
    }
    _GQL_QUERY_NAME = 'productById'

    @staticmethod
    def _gql_args(groups):
        return f'id: {groups[0]}'


class TV5UnisIE(TV5UnisBaseIE):
    IE_NAME = 'tv5unis'
    _VALID_URL = r'https?://(?:www\.)?tv5unis\.ca/videos/(?P<id>[^/]+)(?:/saisons/(?P<season_number>\d+)/episodes/(?P<episode_number>\d+))?/?(?:[?#&]|$)'
    _TESTS = [{
        'url': 'https://www.tv5unis.ca/videos/watatatow/saisons/11/episodes/1',
        'md5': '43beebd47eefb1c5caf9a47a3fc35589',
        'info_dict': {
            'id': '2c06e4af20f0417b86c2536825287690',
            'ext': 'mp4',
            'title': "L'homme éléphant",
            'description': r're:Paul-André et Jean-Yves, .+ quand elle parle du feu au Spot.',
            'subtitles': {
                'fr': 'count:1',
            },
            'duration': 1440,
            'age_limit': 8,
            'tags': 'count:4',
            'series': 'Watatatow',
            'season': 'Season 11',
            'season_number': 11,
            'episode': 'Episode 1',
            'episode_number': 1,
        },
    }, {

Review comment (refactor):
Final review comment:

`try_get` has been made obsolete by `traverse_obj`. They try to solve the same problem--safely traversing data from the web--but `traverse_obj` is more powerful with better syntax.

I would suggest we rewrite the bulk of this `return` statement with a (much more readable) dict key traversal:

```py
        return {
            'id': media_id,
            '_old_archive_ids': [make_archive_id('LimelightMedia', media_id)],
            'formats': formats,
            'subtitles': subtitles,
            **traverse_obj(product, {
                'title': ('title', {str}),
                'description': ('summary', {str}),
                'tags': ('tags', ..., {str}),
                'duration': ('duration', {int_or_none}),
                'season_number': ('seasonNumber', {int_or_none}),
                'episode_number': ('episodeNumber', {int_or_none}),
                'series': ('collection', 'title', {str}),
                'age_limit': ('rating', 'name', {parse_age_limit}),
            }),
        }
```

`traverse_obj` uses set syntax for "transformations" (e.g. the `{parse_age_limit}` and `{str}` you see in the traversal paths above). If there's a function in the "set" braces (e.g. `{parse_age_limit}`), then the function is called with the current object as its first positional argument (e.g. `parse_age_limit(obj)`). If there's a type in the "set" braces (e.g. `{str}`), then `traverse_obj` will only return values of that type.

NOTE: I've also added `_old_archive_ids` for `--download-archive` backwards compatibility (since the extractor is returning the same `id` value but now with a different extractor key). `make_archive_id` is in utils

Code after:
            raise ExtractorError(join_nonempty(
                'This video is restricted',
                code is not None and f', error code {code}',
                reason and f': {remove_end(reason, ".")}',
                delim=''))

        media_id = video['mediaId']
        formats, subtitles = self._extract_m3u8_formats_and_subtitles(
            video['encodings']['hls']['url'], media_id, 'mp4')

        return {
            'id': media_id,
            '_old_archive_ids': [make_archive_id('LimelightMedia', media_id)],
            'formats': formats,
            'subtitles': subtitles,
            **traverse_obj(product, {
                'title': ('title', {str}),
                'description': ('summary', {str}),
                'tags': ('tags', ..., {str}),
                'duration': ('duration', {int_or_none}),
                'season_number': ('seasonNumber', {int_or_none}),
                'episode_number': ('episodeNumber', {int_or_none}),
                'series': ('collection', 'title', {str}),
                'age_limit': ('rating', 'name', {parse_age_limit}),
            }),
        }


class TV5UnisVideoIE(TV5UnisBaseIE):
    IE_NAME = 'tv5unis:video'
    _VALID_URL = r'https?://(?:www\.)?tv5unis\.ca/videos/[^/?#]+/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.tv5unis.ca/videos/bande-annonces/144041',
        'md5': '24a247c96119d77fe1bae8b440457dfa',
        'info_dict': {
            'id': '56862325352147149dce0ae139afced6',
            '_old_archive_ids': ['limelightmedia 56862325352147149dce0ae139afced6'],
            'ext': 'mp4',
            'title': 'Antigone',
            'description': r"re:En aidant son frère .+ dicté par l'amour et la solidarité.",
            'duration': 61,
        },
    }]
    _GQL_QUERY_NAME = 'productById'

    @staticmethod
    def _gql_args(groups):
        return f'id: {groups[0]}'


class TV5UnisIE(TV5UnisBaseIE):