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