]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/litv.py
[extractor/youtube] Improve nsig function name extraction
[yt-dlp.git] / yt_dlp / extractor / litv.py
1 import json
2
3 from .common import InfoExtractor
4 from ..utils import (
5 ExtractorError,
6 int_or_none,
7 smuggle_url,
8 traverse_obj,
9 unsmuggle_url,
10 )
11
12
13 class LiTVIE(InfoExtractor):
14 _VALID_URL = r'https?://(?:www\.)?litv\.tv/(?:vod|promo)/[^/]+/(?:content\.do)?\?.*?\b(?:content_)?id=(?P<id>[^&]+)'
15
16 _URL_TEMPLATE = 'https://www.litv.tv/vod/%s/content.do?id=%s'
17
18 _TESTS = [{
19 'url': 'https://www.litv.tv/vod/drama/content.do?brc_id=root&id=VOD00041610&isUHEnabled=true&autoPlay=1',
20 'info_dict': {
21 'id': 'VOD00041606',
22 'title': '花千骨',
23 },
24 'playlist_count': 50,
25 }, {
26 'url': 'https://www.litv.tv/vod/drama/content.do?brc_id=root&id=VOD00041610&isUHEnabled=true&autoPlay=1',
27 'md5': '969e343d9244778cb29acec608e53640',
28 'info_dict': {
29 'id': 'VOD00041610',
30 'ext': 'mp4',
31 'title': '花千骨第1集',
32 'thumbnail': r're:https?://.*\.jpg$',
33 'description': 'md5:c7017aa144c87467c4fb2909c4b05d6f',
34 'episode_number': 1,
35 },
36 'params': {
37 'noplaylist': True,
38 },
39 'skip': 'Georestricted to Taiwan',
40 }, {
41 'url': 'https://www.litv.tv/promo/miyuezhuan/?content_id=VOD00044841&',
42 'md5': '88322ea132f848d6e3e18b32a832b918',
43 'info_dict': {
44 'id': 'VOD00044841',
45 'ext': 'mp4',
46 'title': '芈月傳第1集 霸星芈月降世楚國',
47 'description': '楚威王二年,太史令唐昧夜觀星象,發現霸星即將現世。王后得知霸星的預言後,想盡辦法不讓孩子順利出生,幸得莒姬相護化解危機。沒想到眾人期待下出生的霸星卻是位公主,楚威王對此失望至極。楚王后命人將女嬰丟棄河中,居然奇蹟似的被少司命像攔下,楚威王認為此女非同凡響,為她取名芈月。',
48 },
49 'skip': 'Georestricted to Taiwan',
50 }]
51
52 def _extract_playlist(self, season_list, video_id, program_info, prompt=True):
53 episode_title = program_info['title']
54 content_id = season_list['contentId']
55
56 all_episodes = [
57 self.url_result(smuggle_url(
58 self._URL_TEMPLATE % (program_info['contentType'], episode['contentId']),
59 {'force_noplaylist': True})) # To prevent infinite recursion
60 for episode in season_list['episode']]
61
62 return self.playlist_result(all_episodes, content_id, episode_title)
63
64 def _real_extract(self, url):
65 url, smuggled_data = unsmuggle_url(url, {})
66
67 video_id = self._match_id(url)
68
69 webpage = self._download_webpage(url, video_id)
70
71 program_info = self._parse_json(self._search_regex(
72 r'var\s+programInfo\s*=\s*([^;]+)', webpage, 'VOD data', default='{}'),
73 video_id)
74
75 season_list = list(program_info.get('seasonList', {}).values())
76 playlist_id = traverse_obj(season_list, 0, 'contentId')
77 if self._yes_playlist(playlist_id, video_id, smuggled_data):
78 return self._extract_playlist(season_list[0], video_id, program_info)
79
80 # In browsers `getMainUrl` request is always issued. Usually this
81 # endpoint gives the same result as the data embedded in the webpage.
82 # If georestricted, there are no embedded data, so an extra request is
83 # necessary to get the error code
84 if 'assetId' not in program_info:
85 program_info = self._download_json(
86 'https://www.litv.tv/vod/ajax/getProgramInfo', video_id,
87 query={'contentId': video_id},
88 headers={'Accept': 'application/json'})
89 video_data = self._parse_json(self._search_regex(
90 r'uiHlsUrl\s*=\s*testBackendData\(([^;]+)\);',
91 webpage, 'video data', default='{}'), video_id)
92 if not video_data:
93 payload = {
94 'assetId': program_info['assetId'],
95 'watchDevices': program_info['watchDevices'],
96 'contentType': program_info['contentType'],
97 }
98 video_data = self._download_json(
99 'https://www.litv.tv/vod/getMainUrl', video_id,
100 data=json.dumps(payload).encode('utf-8'),
101 headers={'Content-Type': 'application/json'})
102
103 if not video_data.get('fullpath'):
104 error_msg = video_data.get('errorMessage')
105 if error_msg == 'vod.error.outsideregionerror':
106 self.raise_geo_restricted('This video is available in Taiwan only')
107 if error_msg:
108 raise ExtractorError('%s said: %s' % (self.IE_NAME, error_msg), expected=True)
109 raise ExtractorError('Unexpected result from %s' % self.IE_NAME)
110
111 formats = self._extract_m3u8_formats(
112 video_data['fullpath'], video_id, ext='mp4',
113 entry_protocol='m3u8_native', m3u8_id='hls')
114 for a_format in formats:
115 # LiTV HLS segments doesn't like compressions
116 a_format.setdefault('http_headers', {})['Accept-Encoding'] = 'identity'
117
118 title = program_info['title'] + program_info.get('secondaryMark', '')
119 description = program_info.get('description')
120 thumbnail = program_info.get('imageFile')
121 categories = [item['name'] for item in program_info.get('category', [])]
122 episode = int_or_none(program_info.get('episode'))
123
124 return {
125 'id': video_id,
126 'formats': formats,
127 'title': title,
128 'description': description,
129 'thumbnail': thumbnail,
130 'categories': categories,
131 'episode_number': episode,
132 }