]>
Commit | Line | Data |
---|---|---|
dcdb292f | 1 | # coding: utf-8 |
f670ef1c | 2 | from __future__ import unicode_literals |
3 | ||
0029071a | 4 | import itertools |
8bd1df3c | 5 | import json |
f670ef1c | 6 | |
02f6ccbc | 7 | from .common import InfoExtractor |
0029071a | 8 | from ..compat import ( |
8bd1df3c | 9 | compat_HTTPError, |
02f6ccbc | 10 | compat_str, |
0029071a S |
11 | compat_urlparse, |
12 | ) | |
13 | from ..utils import ( | |
0029071a | 14 | clean_html, |
02f6ccbc | 15 | ExtractorError, |
8f4c56f3 | 16 | int_or_none, |
02f6ccbc S |
17 | parse_age_limit, |
18 | parse_duration, | |
02f6ccbc | 19 | unified_timestamp, |
0029071a | 20 | ) |
f670ef1c | 21 | |
22 | ||
02f6ccbc | 23 | class DramaFeverBaseIE(InfoExtractor): |
cbcd1a54 S |
24 | _NETRC_MACHINE = 'dramafever' |
25 | ||
1d1dd597 S |
26 | _CONSUMER_SECRET = 'DA59dtVXYLxajktV' |
27 | ||
28 | _consumer_secret = None | |
29 | ||
30 | def _get_consumer_secret(self): | |
31 | mainjs = self._download_webpage( | |
32 | 'http://www.dramafever.com/static/51afe95/df2014/scripts/main.js', | |
33 | None, 'Downloading main.js', fatal=False) | |
34 | if not mainjs: | |
35 | return self._CONSUMER_SECRET | |
36 | return self._search_regex( | |
37 | r"var\s+cs\s*=\s*'([^']+)'", mainjs, | |
38 | 'consumer secret', default=self._CONSUMER_SECRET) | |
39 | ||
cbcd1a54 | 40 | def _real_initialize(self): |
1d1dd597 | 41 | self._consumer_secret = self._get_consumer_secret() |
8bd1df3c | 42 | self._login() |
cbcd1a54 S |
43 | |
44 | def _login(self): | |
45 | (username, password) = self._get_login_info() | |
46 | if username is None: | |
47 | return | |
48 | ||
49 | login_form = { | |
50 | 'username': username, | |
51 | 'password': password, | |
52 | } | |
53 | ||
8bd1df3c S |
54 | try: |
55 | response = self._download_json( | |
56 | 'https://www.dramafever.com/api/users/login', None, 'Logging in', | |
57 | data=json.dumps(login_form).encode('utf-8'), headers={ | |
58 | 'x-consumer-key': self._consumer_secret, | |
59 | }) | |
60 | except ExtractorError as e: | |
61 | if isinstance(e.cause, compat_HTTPError) and e.cause.code in (403, 404): | |
62 | response = self._parse_json( | |
63 | e.cause.read().decode('utf-8'), None) | |
64 | else: | |
65 | raise | |
66 | ||
67 | # Successful login | |
68 | if response.get('result') or response.get('guid') or response.get('user_guid'): | |
69 | return | |
cbcd1a54 | 70 | |
8bd1df3c S |
71 | errors = response.get('errors') |
72 | if errors and isinstance(errors, list): | |
73 | error = errors[0] | |
74 | message = error.get('message') or error['reason'] | |
75 | raise ExtractorError('Unable to login: %s' % message, expected=True) | |
76 | raise ExtractorError('Unable to log in') | |
cbcd1a54 S |
77 | |
78 | ||
79 | class DramaFeverIE(DramaFeverBaseIE): | |
f670ef1c | 80 | IE_NAME = 'dramafever' |
b80e2ebc | 81 | _VALID_URL = r'https?://(?:www\.)?dramafever\.com/(?:[^/]+/)?drama/(?P<id>[0-9]+/[0-9]+)(?:/|$)' |
33cee6c7 | 82 | _TESTS = [{ |
02f6ccbc | 83 | 'url': 'https://www.dramafever.com/drama/4274/1/Heirs/', |
f670ef1c | 84 | 'info_dict': { |
02f6ccbc S |
85 | 'id': '4274.1', |
86 | 'ext': 'wvm', | |
87 | 'title': 'Heirs - Episode 1', | |
88 | 'description': 'md5:362a24ba18209f6276e032a651c50bc2', | |
89 | 'thumbnail': r're:^https?://.*\.jpg', | |
90 | 'duration': 3783, | |
91 | 'timestamp': 1381354993, | |
92 | 'upload_date': '20131009', | |
93 | 'series': 'Heirs', | |
94 | 'season_number': 1, | |
33cee6c7 S |
95 | 'episode': 'Episode 1', |
96 | 'episode_number': 1, | |
3793090b | 97 | }, |
98 | 'params': { | |
99 | # m3u8 download | |
100 | 'skip_download': True, | |
101 | }, | |
33cee6c7 S |
102 | }, { |
103 | 'url': 'http://www.dramafever.com/drama/4826/4/Mnet_Asian_Music_Awards_2015/?ap=1', | |
104 | 'info_dict': { | |
105 | 'id': '4826.4', | |
f76c02c8 | 106 | 'ext': 'flv', |
7d9a1db1 | 107 | 'title': 'Mnet Asian Music Awards 2015', |
33cee6c7 S |
108 | 'description': 'md5:3ff2ee8fedaef86e076791c909cf2e91', |
109 | 'episode': 'Mnet Asian Music Awards 2015 - Part 3', | |
110 | 'episode_number': 4, | |
ec85ded8 | 111 | 'thumbnail': r're:^https?://.*\.jpg', |
33cee6c7 S |
112 | 'timestamp': 1450213200, |
113 | 'upload_date': '20151215', | |
f76c02c8 | 114 | 'duration': 5359, |
33cee6c7 S |
115 | }, |
116 | 'params': { | |
117 | # m3u8 download | |
118 | 'skip_download': True, | |
119 | }, | |
b80e2ebc S |
120 | }, { |
121 | 'url': 'https://www.dramafever.com/zh-cn/drama/4972/15/Doctor_Romantic/', | |
122 | 'only_matching': True, | |
33cee6c7 | 123 | }] |
f670ef1c | 124 | |
02f6ccbc S |
125 | def _call_api(self, path, video_id, note, fatal=False): |
126 | return self._download_json( | |
127 | 'https://www.dramafever.com/api/5/' + path, | |
128 | video_id, note=note, headers={ | |
129 | 'x-consumer-key': self._consumer_secret, | |
130 | }, fatal=fatal) | |
131 | ||
132 | def _get_subtitles(self, video_id): | |
133 | subtitles = {} | |
134 | subs = self._call_api( | |
135 | 'video/%s/subtitles/webvtt/' % video_id, video_id, | |
136 | 'Downloading subtitles JSON', fatal=False) | |
137 | if not subs or not isinstance(subs, list): | |
138 | return subtitles | |
139 | for sub in subs: | |
140 | if not isinstance(sub, dict): | |
141 | continue | |
142 | sub_url = sub.get('url') | |
143 | if not sub_url or not isinstance(sub_url, compat_str): | |
144 | continue | |
145 | subtitles.setdefault( | |
146 | sub.get('code') or sub.get('language') or 'en', []).append({ | |
147 | 'url': sub_url | |
148 | }) | |
149 | return subtitles | |
150 | ||
f670ef1c | 151 | def _real_extract(self, url): |
0029071a | 152 | video_id = self._match_id(url).replace('/', '.') |
f670ef1c | 153 | |
1d1dd597 | 154 | series_id, episode_number = video_id.split('.') |
02f6ccbc S |
155 | |
156 | video = self._call_api( | |
157 | 'series/%s/episodes/%s/' % (series_id, episode_number), video_id, | |
158 | 'Downloading video JSON') | |
159 | ||
160 | formats = [] | |
161 | download_assets = video.get('download_assets') | |
162 | if download_assets and isinstance(download_assets, dict): | |
163 | for format_id, format_dict in download_assets.items(): | |
164 | if not isinstance(format_dict, dict): | |
165 | continue | |
166 | format_url = format_dict.get('url') | |
167 | if not format_url or not isinstance(format_url, compat_str): | |
168 | continue | |
169 | formats.append({ | |
170 | 'url': format_url, | |
171 | 'format_id': format_id, | |
172 | 'filesize': int_or_none(video.get('filesize')), | |
173 | }) | |
174 | ||
175 | stream = self._call_api( | |
176 | 'video/%s/stream/' % video_id, video_id, 'Downloading stream JSON', | |
177 | fatal=False) | |
178 | if stream: | |
179 | stream_url = stream.get('stream_url') | |
180 | if stream_url: | |
181 | formats.extend(self._extract_m3u8_formats( | |
182 | stream_url, video_id, 'mp4', entry_protocol='m3u8_native', | |
183 | m3u8_id='hls', fatal=False)) | |
184 | self._sort_formats(formats) | |
185 | ||
186 | title = video.get('title') or 'Episode %s' % episode_number | |
187 | description = video.get('description') | |
188 | thumbnail = video.get('thumbnail') | |
189 | timestamp = unified_timestamp(video.get('release_date')) | |
190 | duration = parse_duration(video.get('duration')) | |
191 | age_limit = parse_age_limit(video.get('tv_rating')) | |
192 | series = video.get('series_title') | |
193 | season_number = int_or_none(video.get('season')) | |
194 | ||
195 | if series: | |
196 | title = '%s - %s' % (series, title) | |
197 | ||
198 | subtitles = self.extract_subtitles(video_id) | |
199 | ||
200 | return { | |
201 | 'id': video_id, | |
202 | 'title': title, | |
203 | 'description': description, | |
204 | 'thumbnail': thumbnail, | |
205 | 'duration': duration, | |
206 | 'timestamp': timestamp, | |
207 | 'age_limit': age_limit, | |
208 | 'series': series, | |
209 | 'season_number': season_number, | |
210 | 'episode_number': int_or_none(episode_number), | |
211 | 'formats': formats, | |
212 | 'subtitles': subtitles, | |
213 | } | |
f670ef1c | 214 | |
0029071a | 215 | |
cbcd1a54 | 216 | class DramaFeverSeriesIE(DramaFeverBaseIE): |
f670ef1c | 217 | IE_NAME = 'dramafever:series' |
b80e2ebc | 218 | _VALID_URL = r'https?://(?:www\.)?dramafever\.com/(?:[^/]+/)?drama/(?P<id>[0-9]+)(?:/(?:(?!\d+(?:/|$)).+)?)?$' |
f670ef1c | 219 | _TESTS = [{ |
220 | 'url': 'http://www.dramafever.com/drama/4512/Cooking_with_Shin/', | |
221 | 'info_dict': { | |
222 | 'id': '4512', | |
223 | 'title': 'Cooking with Shin', | |
0029071a | 224 | 'description': 'md5:84a3f26e3cdc3fb7f500211b3593b5c1', |
f670ef1c | 225 | }, |
226 | 'playlist_count': 4, | |
227 | }, { | |
228 | 'url': 'http://www.dramafever.com/drama/124/IRIS/', | |
229 | 'info_dict': { | |
230 | 'id': '124', | |
231 | 'title': 'IRIS', | |
0029071a | 232 | 'description': 'md5:b3a30e587cf20c59bd1c01ec0ee1b862', |
f670ef1c | 233 | }, |
234 | 'playlist_count': 20, | |
235 | }] | |
236 | ||
463b2e55 | 237 | _PAGE_SIZE = 60 # max is 60 (see http://api.drama9.com/#get--api-4-episode-series-) |
0029071a | 238 | |
f670ef1c | 239 | def _real_extract(self, url): |
240 | series_id = self._match_id(url) | |
0029071a | 241 | |
0029071a S |
242 | series = self._download_json( |
243 | 'http://www.dramafever.com/api/4/series/query/?cs=%s&series_id=%s' | |
1d1dd597 | 244 | % (self._consumer_secret, series_id), |
0029071a | 245 | series_id, 'Downloading series JSON')['series'][series_id] |
f670ef1c | 246 | |
0029071a S |
247 | title = clean_html(series['name']) |
248 | description = clean_html(series.get('description') or series.get('description_short')) | |
f670ef1c | 249 | |
f670ef1c | 250 | entries = [] |
0029071a S |
251 | for page_num in itertools.count(1): |
252 | episodes = self._download_json( | |
253 | 'http://www.dramafever.com/api/4/episode/series/?cs=%s&series_id=%s&page_size=%d&page_number=%d' | |
1d1dd597 | 254 | % (self._consumer_secret, series_id, self._PAGE_SIZE, page_num), |
0029071a S |
255 | series_id, 'Downloading episodes JSON page #%d' % page_num) |
256 | for episode in episodes.get('value', []): | |
10464af5 S |
257 | episode_url = episode.get('episode_url') |
258 | if not episode_url: | |
259 | continue | |
0029071a | 260 | entries.append(self.url_result( |
10464af5 | 261 | compat_urlparse.urljoin(url, episode_url), |
0029071a S |
262 | 'DramaFever', episode.get('guid'))) |
263 | if page_num == episodes['num_pages']: | |
264 | break | |
265 | ||
f670ef1c | 266 | return self.playlist_result(entries, series_id, title, description) |