]>
Commit | Line | Data |
---|---|---|
3b01a9fb | 1 | import json |
3b01a9fb YCH |
2 | |
3 | from .common import InfoExtractor | |
4 | from ..utils import ( | |
5 | ExtractorError, | |
6 | int_or_none, | |
f40ee5e9 | 7 | traverse_obj, |
3b01a9fb YCH |
8 | smuggle_url, |
9 | unsmuggle_url, | |
10 | ) | |
11 | ||
12 | ||
13 | class LiTVIE(InfoExtractor): | |
92519402 | 14 | _VALID_URL = r'https?://(?:www\.)?litv\.tv/(?:vod|promo)/[^/]+/(?:content\.do)?\?.*?\b(?:content_)?id=(?P<id>[^&]+)' |
3b01a9fb YCH |
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', | |
39e1c4f0 | 27 | 'md5': '969e343d9244778cb29acec608e53640', |
3b01a9fb YCH |
28 | 'info_dict': { |
29 | 'id': 'VOD00041610', | |
30 | 'ext': 'mp4', | |
31 | 'title': '花千骨第1集', | |
ec85ded8 | 32 | 'thumbnail': r're:https?://.*\.jpg$', |
3b01a9fb YCH |
33 | 'description': 'md5:c7017aa144c87467c4fb2909c4b05d6f', |
34 | 'episode_number': 1, | |
35 | }, | |
36 | 'params': { | |
37 | 'noplaylist': True, | |
39e1c4f0 YCH |
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': '楚威王二年,太史令唐昧夜觀星象,發現霸星即將現世。王后得知霸星的預言後,想盡辦法不讓孩子順利出生,幸得莒姬相護化解危機。沒想到眾人期待下出生的霸星卻是位公主,楚威王對此失望至極。楚王后命人將女嬰丟棄河中,居然奇蹟似的被少司命像攔下,楚威王認為此女非同凡響,為她取名芈月。', | |
3b01a9fb YCH |
48 | }, |
49 | 'skip': 'Georestricted to Taiwan', | |
50 | }] | |
51 | ||
9dde0e04 YCH |
52 | def _extract_playlist(self, season_list, video_id, program_info, prompt=True): |
53 | episode_title = program_info['title'] | |
3b01a9fb YCH |
54 | content_id = season_list['contentId'] |
55 | ||
3b01a9fb YCH |
56 | all_episodes = [ |
57 | self.url_result(smuggle_url( | |
9dde0e04 | 58 | self._URL_TEMPLATE % (program_info['contentType'], episode['contentId']), |
3b01a9fb YCH |
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): | |
88f23a18 | 65 | url, smuggled_data = unsmuggle_url(url, {}) |
3b01a9fb YCH |
66 | |
67 | video_id = self._match_id(url) | |
68 | ||
3b01a9fb YCH |
69 | webpage = self._download_webpage(url, video_id) |
70 | ||
9dde0e04 | 71 | program_info = self._parse_json(self._search_regex( |
ec85ded8 | 72 | r'var\s+programInfo\s*=\s*([^;]+)', webpage, 'VOD data', default='{}'), |
3b01a9fb YCH |
73 | video_id) |
74 | ||
9dde0e04 | 75 | season_list = list(program_info.get('seasonList', {}).values()) |
f40ee5e9 | 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) | |
3b01a9fb YCH |
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 | |
9dde0e04 YCH |
84 | if 'assetId' not in program_info: |
85 | program_info = self._download_json( | |
39e1c4f0 YCH |
86 | 'https://www.litv.tv/vod/ajax/getProgramInfo', video_id, |
87 | query={'contentId': video_id}, | |
88 | headers={'Accept': 'application/json'}) | |
3b01a9fb YCH |
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 = { | |
9dde0e04 YCH |
94 | 'assetId': program_info['assetId'], |
95 | 'watchDevices': program_info['watchDevices'], | |
96 | 'contentType': program_info['contentType'], | |
3b01a9fb YCH |
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( | |
39e1c4f0 YCH |
112 | video_data['fullpath'], video_id, ext='mp4', |
113 | entry_protocol='m3u8_native', m3u8_id='hls') | |
3b01a9fb YCH |
114 | for a_format in formats: |
115 | # LiTV HLS segments doesn't like compressions | |
116 | a_format.setdefault('http_headers', {})['Youtubedl-no-compression'] = True | |
117 | ||
9dde0e04 YCH |
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')) | |
3b01a9fb YCH |
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 | } |