2 from __future__ import unicode_literals
6 from .common import InfoExtractor
23 class DramaFeverBaseIE(InfoExtractor):
24 _LOGIN_URL = 'https://www.dramafever.com/accounts/login/'
25 _NETRC_MACHINE = 'dramafever'
27 _CONSUMER_SECRET = 'DA59dtVXYLxajktV'
29 _consumer_secret = None
31 def _get_consumer_secret(self):
32 mainjs = self._download_webpage(
33 'http://www.dramafever.com/static/51afe95/df2014/scripts/main.js',
34 None, 'Downloading main.js', fatal=False)
36 return self._CONSUMER_SECRET
37 return self._search_regex(
38 r"var\s+cs\s*=\s*'([^']+)'", mainjs,
39 'consumer secret', default=self._CONSUMER_SECRET)
41 def _real_initialize(self):
43 self._consumer_secret = self._get_consumer_secret()
46 (username, password) = self._get_login_info()
55 request = sanitized_Request(
56 self._LOGIN_URL, urlencode_postdata(login_form))
57 response = self._download_webpage(
58 request, None, 'Logging in')
60 if all(logout_pattern not in response
61 for logout_pattern in ['href="/accounts/logout/"', '>Log out<']):
62 error = self._html_search_regex(
63 r'(?s)<h\d[^>]+\bclass="hidden-xs prompt"[^>]*>(.+?)</h\d',
64 response, 'error message', default=None)
66 raise ExtractorError('Unable to login: %s' % error, expected=True)
67 raise ExtractorError('Unable to log in')
70 class DramaFeverIE(DramaFeverBaseIE):
71 IE_NAME = 'dramafever'
72 _VALID_URL = r'https?://(?:www\.)?dramafever\.com/(?:[^/]+/)?drama/(?P<id>[0-9]+/[0-9]+)(?:/|$)'
74 'url': 'https://www.dramafever.com/drama/4274/1/Heirs/',
78 'title': 'Heirs - Episode 1',
79 'description': 'md5:362a24ba18209f6276e032a651c50bc2',
80 'thumbnail': r're:^https?://.*\.jpg',
82 'timestamp': 1381354993,
83 'upload_date': '20131009',
86 'episode': 'Episode 1',
91 'skip_download': True,
94 'url': 'http://www.dramafever.com/drama/4826/4/Mnet_Asian_Music_Awards_2015/?ap=1',
98 'title': 'Mnet Asian Music Awards 2015',
99 'description': 'md5:3ff2ee8fedaef86e076791c909cf2e91',
100 'episode': 'Mnet Asian Music Awards 2015 - Part 3',
102 'thumbnail': r're:^https?://.*\.jpg',
103 'timestamp': 1450213200,
104 'upload_date': '20151215',
109 'skip_download': True,
112 'url': 'https://www.dramafever.com/zh-cn/drama/4972/15/Doctor_Romantic/',
113 'only_matching': True,
116 def _call_api(self, path, video_id, note, fatal=False):
117 return self._download_json(
118 'https://www.dramafever.com/api/5/' + path,
119 video_id, note=note, headers={
120 'x-consumer-key': self._consumer_secret,
123 def _get_subtitles(self, video_id):
125 subs = self._call_api(
126 'video/%s/subtitles/webvtt/' % video_id, video_id,
127 'Downloading subtitles JSON', fatal=False)
128 if not subs or not isinstance(subs, list):
131 if not isinstance(sub, dict):
133 sub_url = sub.get('url')
134 if not sub_url or not isinstance(sub_url, compat_str):
136 subtitles.setdefault(
137 sub.get('code') or sub.get('language') or 'en', []).append({
142 def _real_extract(self, url):
143 video_id = self._match_id(url).replace('/', '.')
145 series_id, episode_number = video_id.split('.')
147 video = self._call_api(
148 'series/%s/episodes/%s/' % (series_id, episode_number), video_id,
149 'Downloading video JSON')
152 download_assets = video.get('download_assets')
153 if download_assets and isinstance(download_assets, dict):
154 for format_id, format_dict in download_assets.items():
155 if not isinstance(format_dict, dict):
157 format_url = format_dict.get('url')
158 if not format_url or not isinstance(format_url, compat_str):
162 'format_id': format_id,
163 'filesize': int_or_none(video.get('filesize')),
166 stream = self._call_api(
167 'video/%s/stream/' % video_id, video_id, 'Downloading stream JSON',
170 stream_url = stream.get('stream_url')
172 formats.extend(self._extract_m3u8_formats(
173 stream_url, video_id, 'mp4', entry_protocol='m3u8_native',
174 m3u8_id='hls', fatal=False))
175 self._sort_formats(formats)
177 title = video.get('title') or 'Episode %s' % episode_number
178 description = video.get('description')
179 thumbnail = video.get('thumbnail')
180 timestamp = unified_timestamp(video.get('release_date'))
181 duration = parse_duration(video.get('duration'))
182 age_limit = parse_age_limit(video.get('tv_rating'))
183 series = video.get('series_title')
184 season_number = int_or_none(video.get('season'))
187 title = '%s - %s' % (series, title)
189 subtitles = self.extract_subtitles(video_id)
194 'description': description,
195 'thumbnail': thumbnail,
196 'duration': duration,
197 'timestamp': timestamp,
198 'age_limit': age_limit,
200 'season_number': season_number,
201 'episode_number': int_or_none(episode_number),
203 'subtitles': subtitles,
207 class DramaFeverSeriesIE(DramaFeverBaseIE):
208 IE_NAME = 'dramafever:series'
209 _VALID_URL = r'https?://(?:www\.)?dramafever\.com/(?:[^/]+/)?drama/(?P<id>[0-9]+)(?:/(?:(?!\d+(?:/|$)).+)?)?$'
211 'url': 'http://www.dramafever.com/drama/4512/Cooking_with_Shin/',
214 'title': 'Cooking with Shin',
215 'description': 'md5:84a3f26e3cdc3fb7f500211b3593b5c1',
219 'url': 'http://www.dramafever.com/drama/124/IRIS/',
223 'description': 'md5:b3a30e587cf20c59bd1c01ec0ee1b862',
225 'playlist_count': 20,
228 _PAGE_SIZE = 60 # max is 60 (see http://api.drama9.com/#get--api-4-episode-series-)
230 def _real_extract(self, url):
231 series_id = self._match_id(url)
233 series = self._download_json(
234 'http://www.dramafever.com/api/4/series/query/?cs=%s&series_id=%s'
235 % (self._consumer_secret, series_id),
236 series_id, 'Downloading series JSON')['series'][series_id]
238 title = clean_html(series['name'])
239 description = clean_html(series.get('description') or series.get('description_short'))
242 for page_num in itertools.count(1):
243 episodes = self._download_json(
244 'http://www.dramafever.com/api/4/episode/series/?cs=%s&series_id=%s&page_size=%d&page_number=%d'
245 % (self._consumer_secret, series_id, self._PAGE_SIZE, page_num),
246 series_id, 'Downloading episodes JSON page #%d' % page_num)
247 for episode in episodes.get('value', []):
248 episode_url = episode.get('episode_url')
251 entries.append(self.url_result(
252 compat_urlparse.urljoin(url, episode_url),
253 'DramaFever', episode.get('guid')))
254 if page_num == episodes['num_pages']:
257 return self.playlist_result(entries, series_id, title, description)