]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/dramafever.py
Prepare to release.
[youtubedl] / youtube_dl / extractor / dramafever.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import itertools
5 import json
6
7 from .common import InfoExtractor
8 from ..compat import (
9 compat_HTTPError,
10 compat_str,
11 compat_urlparse,
12 )
13 from ..utils import (
14 clean_html,
15 ExtractorError,
16 int_or_none,
17 parse_age_limit,
18 parse_duration,
19 unified_timestamp,
20 )
21
22
23 class DramaFeverBaseIE(InfoExtractor):
24 _NETRC_MACHINE = 'dramafever'
25
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
40 def _real_initialize(self):
41 self._consumer_secret = self._get_consumer_secret()
42 self._login()
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
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
70
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')
77
78
79 class DramaFeverIE(DramaFeverBaseIE):
80 IE_NAME = 'dramafever'
81 _VALID_URL = r'https?://(?:www\.)?dramafever\.com/(?:[^/]+/)?drama/(?P<id>[0-9]+/[0-9]+)(?:/|$)'
82 _TESTS = [{
83 'url': 'https://www.dramafever.com/drama/4274/1/Heirs/',
84 'info_dict': {
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,
95 'episode': 'Episode 1',
96 'episode_number': 1,
97 },
98 'params': {
99 # m3u8 download
100 'skip_download': True,
101 },
102 }, {
103 'url': 'http://www.dramafever.com/drama/4826/4/Mnet_Asian_Music_Awards_2015/?ap=1',
104 'info_dict': {
105 'id': '4826.4',
106 'ext': 'flv',
107 'title': 'Mnet Asian Music Awards 2015',
108 'description': 'md5:3ff2ee8fedaef86e076791c909cf2e91',
109 'episode': 'Mnet Asian Music Awards 2015 - Part 3',
110 'episode_number': 4,
111 'thumbnail': r're:^https?://.*\.jpg',
112 'timestamp': 1450213200,
113 'upload_date': '20151215',
114 'duration': 5359,
115 },
116 'params': {
117 # m3u8 download
118 'skip_download': True,
119 },
120 }, {
121 'url': 'https://www.dramafever.com/zh-cn/drama/4972/15/Doctor_Romantic/',
122 'only_matching': True,
123 }]
124
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
151 def _real_extract(self, url):
152 video_id = self._match_id(url).replace('/', '.')
153
154 series_id, episode_number = video_id.split('.')
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 }
214
215
216 class DramaFeverSeriesIE(DramaFeverBaseIE):
217 IE_NAME = 'dramafever:series'
218 _VALID_URL = r'https?://(?:www\.)?dramafever\.com/(?:[^/]+/)?drama/(?P<id>[0-9]+)(?:/(?:(?!\d+(?:/|$)).+)?)?$'
219 _TESTS = [{
220 'url': 'http://www.dramafever.com/drama/4512/Cooking_with_Shin/',
221 'info_dict': {
222 'id': '4512',
223 'title': 'Cooking with Shin',
224 'description': 'md5:84a3f26e3cdc3fb7f500211b3593b5c1',
225 },
226 'playlist_count': 4,
227 }, {
228 'url': 'http://www.dramafever.com/drama/124/IRIS/',
229 'info_dict': {
230 'id': '124',
231 'title': 'IRIS',
232 'description': 'md5:b3a30e587cf20c59bd1c01ec0ee1b862',
233 },
234 'playlist_count': 20,
235 }]
236
237 _PAGE_SIZE = 60 # max is 60 (see http://api.drama9.com/#get--api-4-episode-series-)
238
239 def _real_extract(self, url):
240 series_id = self._match_id(url)
241
242 series = self._download_json(
243 'http://www.dramafever.com/api/4/series/query/?cs=%s&series_id=%s'
244 % (self._consumer_secret, series_id),
245 series_id, 'Downloading series JSON')['series'][series_id]
246
247 title = clean_html(series['name'])
248 description = clean_html(series.get('description') or series.get('description_short'))
249
250 entries = []
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'
254 % (self._consumer_secret, series_id, self._PAGE_SIZE, page_num),
255 series_id, 'Downloading episodes JSON page #%d' % page_num)
256 for episode in episodes.get('value', []):
257 episode_url = episode.get('episode_url')
258 if not episode_url:
259 continue
260 entries.append(self.url_result(
261 compat_urlparse.urljoin(url, episode_url),
262 'DramaFever', episode.get('guid')))
263 if page_num == episodes['num_pages']:
264 break
265
266 return self.playlist_result(entries, series_id, title, description)