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

Code before:


class LinkedInIE(LinkedInBaseIE):
    _VALID_URL = r'https?://(?:www\.)?linkedin\.com/posts/.+?(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.linkedin.com/posts/mishalkhawaja_sendinblueviews-toronto-digitalmarketing-ugcPost-6850898786781339649-mM20',
        'info_dict': {
            'id': '6850898786781339649',
            'ext': 'mp4',
            'title': 'Mishal K. on LinkedIn: #sendinblueviews #toronto #digitalmarketing',
            'description': 'md5:be125430bab1c574f16aeb186a4d5b19',
            'creator': 'Mishal K.'
        },
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(url, video_id)

        title = self._html_search_regex(r'<title>([^<]+)</title>', webpage, 'title')
        description = clean_html(get_element_by_class('share-update-card__update-text', webpage))
        sources = self._parse_json(extract_attributes(self._search_regex(r'(<video[^>]+>)', webpage, 'video'))['data-sources'], video_id)
        like_count = int_or_none(get_element_by_class('social-counts-reactions__social-counts-numRections', webpage))
        creator = strip_or_none(clean_html(get_element_by_class('comment__actor-name', webpage)))

        formats = [{
            'url': source['src'],
            'ext': mimetype2ext(source.get('type')),
            'tbr': float_or_none(source.get('data-bitrate'), scale=1000),
        } for source in sources]

        self._sort_formats(formats)

        return {
            'id': video_id,
            'formats': formats,
            'title': title,
            'like_count': like_count,
            'creator': creator,
            'thumbnail': self._og_search_thumbnail(webpage),
            'description': description,
        }


class LinkedInLearningIE(LinkedInLearningBaseIE):
    IE_NAME = 'linkedin:learning'
    _VALID_URL = r'https?://(?:www\.)?linkedin\.com/learning/(?P<course_slug>[^/]+)/(?P<id>[^/?#]+)'
    _TEST = {
        'url': 'https://www.linkedin.com/learning/programming-foundations-fundamentals/welcome?autoplay=true',
        'md5': 'a1d74422ff0d5e66a792deb996693167',
        'info_dict': {

Review comment (refactor):
```suggestion
        title = self._html_search_regex(r'<title>([^<]+)</title>', webpage, 'title')
        description = clean_html(get_element_by_class('share-update-card__update-text', webpage))
        like_count = int_or_none(get_element_by_class('social-counts-reactions__social-counts-numRections', webpage))
        creator = strip_or_none(clean_html(get_element_by_class('comment__actor-name', webpage)))
        
        sources = self._parse_json(extract_attributes(self._search_regex(r'(<video[^>]+>)', webpage, 'video'))['data-sources'], video_id)
        formats = [{
```

Code after:


class LinkedInIE(LinkedInBaseIE):
    _VALID_URL = r'https?://(?:www\.)?linkedin\.com/posts/.+?(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.linkedin.com/posts/mishalkhawaja_sendinblueviews-toronto-digitalmarketing-ugcPost-6850898786781339649-mM20',
        'info_dict': {
            'id': '6850898786781339649',
            'ext': 'mp4',
            'title': 'Mishal K. on LinkedIn: #sendinblueviews #toronto #digitalmarketing',
            'description': 'md5:be125430bab1c574f16aeb186a4d5b19',
            'creator': 'Mishal K.'
        },
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(url, video_id)

        title = self._html_search_regex(r'<title>([^<]+)</title>', webpage, 'title')
        description = clean_html(get_element_by_class('share-update-card__update-text', webpage))
        like_count = int_or_none(get_element_by_class('social-counts-reactions__social-counts-numRections', webpage))
        creator = strip_or_none(clean_html(get_element_by_class('comment__actor-name', webpage)))
        
        sources = self._parse_json(extract_attributes(self._search_regex(r'(<video[^>]+>)', webpage, 'video'))['data-sources'], video_id)
        formats = [{
            'url': source['src'],
            'ext': mimetype2ext(source.get('type')),
            'tbr': float_or_none(source.get('data-bitrate'), scale=1000),
        } for source in sources]

        self._sort_formats(formats)

        return {
            'id': video_id,
            'formats': formats,
            'title': title,
            'like_count': like_count,
            'creator': creator,
            'thumbnail': self._og_search_thumbnail(webpage),
            'description': description,
        }


class LinkedInLearningIE(LinkedInLearningBaseIE):
    IE_NAME = 'linkedin:learning'
    _VALID_URL = r'https?://(?:www\.)?linkedin\.com/learning/(?P<course_slug>[^/]+)/(?P<id>[^/?#]+)'
    _TEST = {
        'url': 'https://www.linkedin.com/learning/programming-foundations-fundamentals/welcome?autoplay=true',
        'md5': 'a1d74422ff0d5e66a792deb996693167',
        'info_dict': {