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

Code before:
            'description': '楚威王二年，太史令唐昧夜觀星象，發現霸星即將現世。王后得知霸星的預言後，想盡辦法不讓孩子順利出生，幸得莒姬相護化解危機。沒想到眾人期待下出生的霸星卻是位公主，楚威王對此失望至極。楚王后命人將女嬰丟棄河中，居然奇蹟似的被少司命像攔下，楚威王認為此女非同凡響，為她取名芈月。',
        },
        'skip': 'No longer exists',
    }]

    def _extract_playlist(self, playlist_data, content_type):
        all_episodes = [
            self.url_result(smuggle_url(
                self._URL_TEMPLATE % (content_type, episode['content_id']),
                {'force_noplaylist': True}))  # To prevent infinite recursion
            for episode in traverse_obj(playlist_data, ('seasons', ..., 'episodes', lambda _, v: v['content_id']))]

        return self.playlist_result(all_episodes, playlist_data['content_id'], playlist_data.get('title'))

    def _real_extract(self, url):
        url, smuggled_data = unsmuggle_url(url, {})

        video_id = self._match_id(url)

        webpage = self._download_webpage(url, video_id)

        vod_data = self._search_nextjs_data(webpage, video_id, default={})

        program_info = traverse_obj(vod_data, ('props', 'pageProps', 'programInformation', {dict})) or {}
        playlist_data = traverse_obj(vod_data, ('props', 'pageProps', 'seriesTree'))
        if playlist_data is not None and self._yes_playlist(program_info.get('series_id'), video_id, smuggled_data):
            return self._extract_playlist(playlist_data, program_info.get('content_type'))

        asset_id = traverse_obj(program_info, ('assets', 0, 'asset_id', {str}))
        if asset_id:  # This is a live stream
            media_type = 'vod'
        else:  # This is a VOD
            asset_id = program_info['content_id']
            media_type = program_info['content_type']
        puid = try_call(lambda: self._get_cookies('https://www.litv.tv/')['PUID'].value)
        if puid:
            endpoint = 'get-urls'
        else:
            puid = str(uuid.uuid4())
            endpoint = 'get-urls-no-auth'
        video_data = self._download_json(
            f'https://www.litv.tv/api/{endpoint}', video_id,
            data=json.dumps({'AssetId': asset_id, 'MediaType': media_type, 'puid': puid}).encode(),
            headers={'Content-Type': 'application/json'})

        if error := traverse_obj(video_data, ('error', {dict})):
            error_msg = traverse_obj(error, ('message', {str}))
            if error_msg and 'OutsideRegionError' in error_msg:
                self.raise_geo_restricted('This video is available in Taiwan only')
            elif error_msg:
                raise ExtractorError(f'{self.IE_NAME} said: {error_msg}', expected=True)

Review comment (suggestion):
```suggestion
        vod_data = self._search_nextjs_data(webpage, video_id)['props']['pageProps']
        program_info = traverse_obj(vod_data, ('programInformation', {dict})) or {}
        playlist_data = traverse_obj(vod_data, ('seriesTree', {dict}))
        if playlist_data and self._yes_playlist(program_info.get('series_id'), video_id, smuggled_data):
```

Code after:

    def _extract_playlist(self, playlist_data, content_type):
        all_episodes = [
            self.url_result(smuggle_url(
                self._URL_TEMPLATE % (content_type, episode['content_id']),
                {'force_noplaylist': True}))  # To prevent infinite recursion
            for episode in traverse_obj(playlist_data, ('seasons', ..., 'episodes', lambda _, v: v['content_id']))]

        return self.playlist_result(all_episodes, playlist_data['content_id'], playlist_data.get('title'))

    def _real_extract(self, url):
        url, smuggled_data = unsmuggle_url(url, {})
        video_id = self._match_id(url)
        webpage = self._download_webpage(url, video_id)
        vod_data = self._search_nextjs_data(webpage, video_id)['props']['pageProps']

        program_info = traverse_obj(vod_data, ('programInformation', {dict})) or {}
        playlist_data = traverse_obj(vod_data, ('seriesTree'))
        if playlist_data and self._yes_playlist(program_info.get('series_id'), video_id, smuggled_data):
            return self._extract_playlist(playlist_data, program_info.get('content_type'))

        asset_id = traverse_obj(program_info, ('assets', 0, 'asset_id', {str}))
        if asset_id:  # This is a VOD
            media_type = 'vod'
        else:  # This is a live stream
            asset_id = program_info['content_id']
            media_type = program_info['content_type']
        puid = try_call(lambda: self._get_cookies('https://www.litv.tv/')['PUID'].value)
        if puid:
            endpoint = 'get-urls'
        else:
            puid = str(uuid.uuid4())
            endpoint = 'get-urls-no-auth'
        video_data = self._download_json(
            f'https://www.litv.tv/api/{endpoint}', video_id,
            data=json.dumps({'AssetId': asset_id, 'MediaType': media_type, 'puid': puid}).encode(),
            headers={'Content-Type': 'application/json'})

        if error := traverse_obj(video_data, ('error', {dict})):
            error_msg = traverse_obj(error, ('message', {str}))
            if error_msg and 'OutsideRegionError' in error_msg:
                self.raise_geo_restricted('This video is available in Taiwan only')
            elif error_msg:
                raise ExtractorError(f'{self.IE_NAME} said: {error_msg}', expected=True)
            raise ExtractorError(f'Unexpected error from {self.IE_NAME}')

        formats = self._extract_m3u8_formats(
            video_data['result']['AssetURLs'][0], video_id, ext='mp4', m3u8_id='hls')
        for a_format in formats:
            # LiTV HLS segments doesn't like compressions
            a_format.setdefault('http_headers', {})['Accept-Encoding'] = 'identity'