]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/dramafever.py
debian/control: Prefer mplayer to mplayer2 in Recommends.
[youtubedl] / youtube_dl / extractor / dramafever.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import itertools
5
6 from .common import InfoExtractor
7 from ..compat import (
8 compat_HTTPError,
9 compat_urllib_parse,
10 compat_urllib_request,
11 compat_urlparse,
12 )
13 from ..utils import (
14 ExtractorError,
15 clean_html,
16 determine_ext,
17 int_or_none,
18 parse_iso8601,
19 )
20
21
22 class DramaFeverBaseIE(InfoExtractor):
23 _LOGIN_URL = 'https://www.dramafever.com/accounts/login/'
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._login()
42 self._consumer_secret = self._get_consumer_secret()
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 request = compat_urllib_request.Request(
55 self._LOGIN_URL, compat_urllib_parse.urlencode(login_form).encode('utf-8'))
56 response = self._download_webpage(
57 request, None, 'Logging in as %s' % username)
58
59 if all(logout_pattern not in response
60 for logout_pattern in ['href="/accounts/logout/"', '>Log out<']):
61 error = self._html_search_regex(
62 r'(?s)class="hidden-xs prompt"[^>]*>(.+?)<',
63 response, 'error message', default=None)
64 if error:
65 raise ExtractorError('Unable to login: %s' % error, expected=True)
66 raise ExtractorError('Unable to log in')
67
68
69 class DramaFeverIE(DramaFeverBaseIE):
70 IE_NAME = 'dramafever'
71 _VALID_URL = r'https?://(?:www\.)?dramafever\.com/drama/(?P<id>[0-9]+/[0-9]+)(?:/|$)'
72 _TEST = {
73 'url': 'http://www.dramafever.com/drama/4512/1/Cooking_with_Shin/',
74 'info_dict': {
75 'id': '4512.1',
76 'ext': 'flv',
77 'title': 'Cooking with Shin 4512.1',
78 'description': 'md5:a8eec7942e1664a6896fcd5e1287bfd0',
79 'thumbnail': 're:^https?://.*\.jpg',
80 'timestamp': 1404336058,
81 'upload_date': '20140702',
82 'duration': 343,
83 }
84 }
85
86 def _real_extract(self, url):
87 video_id = self._match_id(url).replace('/', '.')
88
89 try:
90 feed = self._download_json(
91 'http://www.dramafever.com/amp/episode/feed.json?guid=%s' % video_id,
92 video_id, 'Downloading episode JSON')['channel']['item']
93 except ExtractorError as e:
94 if isinstance(e.cause, compat_HTTPError):
95 raise ExtractorError(
96 'Currently unavailable in your country.', expected=True)
97 raise
98
99 media_group = feed.get('media-group', {})
100
101 formats = []
102 for media_content in media_group['media-content']:
103 src = media_content.get('@attributes', {}).get('url')
104 if not src:
105 continue
106 ext = determine_ext(src)
107 if ext == 'f4m':
108 formats.extend(self._extract_f4m_formats(
109 src, video_id, f4m_id='hds'))
110 elif ext == 'm3u8':
111 formats.extend(self._extract_m3u8_formats(
112 src, video_id, 'mp4', m3u8_id='hls'))
113 else:
114 formats.append({
115 'url': src,
116 })
117 self._sort_formats(formats)
118
119 title = media_group.get('media-title')
120 description = media_group.get('media-description')
121 duration = int_or_none(media_group['media-content'][0].get('@attributes', {}).get('duration'))
122 thumbnail = self._proto_relative_url(
123 media_group.get('media-thumbnail', {}).get('@attributes', {}).get('url'))
124 timestamp = parse_iso8601(feed.get('pubDate'), ' ')
125
126 subtitles = {}
127 for media_subtitle in media_group.get('media-subTitle', []):
128 lang = media_subtitle.get('@attributes', {}).get('lang')
129 href = media_subtitle.get('@attributes', {}).get('href')
130 if not lang or not href:
131 continue
132 subtitles[lang] = [{
133 'ext': 'ttml',
134 'url': href,
135 }]
136
137 series_id, episode_number = video_id.split('.')
138 episode_info = self._download_json(
139 # We only need a single episode info, so restricting page size to one episode
140 # and dealing with page number as with episode number
141 r'http://www.dramafever.com/api/4/episode/series/?cs=%s&series_id=%s&page_number=%s&page_size=1'
142 % (self._consumer_secret, series_id, episode_number),
143 video_id, 'Downloading episode info JSON', fatal=False)
144 if episode_info:
145 value = episode_info.get('value')
146 if value:
147 subfile = value[0].get('subfile') or value[0].get('new_subfile')
148 if subfile and subfile != 'http://www.dramafever.com/st/':
149 subtitles.setdefault('English', []).append({
150 'ext': 'srt',
151 'url': subfile,
152 })
153
154 return {
155 'id': video_id,
156 'title': title,
157 'description': description,
158 'thumbnail': thumbnail,
159 'timestamp': timestamp,
160 'duration': duration,
161 'formats': formats,
162 'subtitles': subtitles,
163 }
164
165
166 class DramaFeverSeriesIE(DramaFeverBaseIE):
167 IE_NAME = 'dramafever:series'
168 _VALID_URL = r'https?://(?:www\.)?dramafever\.com/drama/(?P<id>[0-9]+)(?:/(?:(?!\d+(?:/|$)).+)?)?$'
169 _TESTS = [{
170 'url': 'http://www.dramafever.com/drama/4512/Cooking_with_Shin/',
171 'info_dict': {
172 'id': '4512',
173 'title': 'Cooking with Shin',
174 'description': 'md5:84a3f26e3cdc3fb7f500211b3593b5c1',
175 },
176 'playlist_count': 4,
177 }, {
178 'url': 'http://www.dramafever.com/drama/124/IRIS/',
179 'info_dict': {
180 'id': '124',
181 'title': 'IRIS',
182 'description': 'md5:b3a30e587cf20c59bd1c01ec0ee1b862',
183 },
184 'playlist_count': 20,
185 }]
186
187 _PAGE_SIZE = 60 # max is 60 (see http://api.drama9.com/#get--api-4-episode-series-)
188
189 def _real_extract(self, url):
190 series_id = self._match_id(url)
191
192 series = self._download_json(
193 'http://www.dramafever.com/api/4/series/query/?cs=%s&series_id=%s'
194 % (self._consumer_secret, series_id),
195 series_id, 'Downloading series JSON')['series'][series_id]
196
197 title = clean_html(series['name'])
198 description = clean_html(series.get('description') or series.get('description_short'))
199
200 entries = []
201 for page_num in itertools.count(1):
202 episodes = self._download_json(
203 'http://www.dramafever.com/api/4/episode/series/?cs=%s&series_id=%s&page_size=%d&page_number=%d'
204 % (self._consumer_secret, series_id, self._PAGE_SIZE, page_num),
205 series_id, 'Downloading episodes JSON page #%d' % page_num)
206 for episode in episodes.get('value', []):
207 episode_url = episode.get('episode_url')
208 if not episode_url:
209 continue
210 entries.append(self.url_result(
211 compat_urlparse.urljoin(url, episode_url),
212 'DramaFever', episode.get('guid')))
213 if page_num == episodes['num_pages']:
214 break
215
216 return self.playlist_result(entries, series_id, title, description)