]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/dramafever.py
bcd9fe2a039550d36af3f1a63cb3cf8cc583cb2a
[youtubedl] / youtube_dl / extractor / dramafever.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import itertools
5
6 from .amp import AMPIE
7 from ..compat import (
8 compat_HTTPError,
9 compat_urlparse,
10 )
11 from ..utils import (
12 ExtractorError,
13 clean_html,
14 int_or_none,
15 sanitized_Request,
16 urlencode_postdata
17 )
18
19
20 class DramaFeverBaseIE(AMPIE):
21 _LOGIN_URL = 'https://www.dramafever.com/accounts/login/'
22 _NETRC_MACHINE = 'dramafever'
23
24 _CONSUMER_SECRET = 'DA59dtVXYLxajktV'
25
26 _consumer_secret = None
27
28 def _get_consumer_secret(self):
29 mainjs = self._download_webpage(
30 'http://www.dramafever.com/static/51afe95/df2014/scripts/main.js',
31 None, 'Downloading main.js', fatal=False)
32 if not mainjs:
33 return self._CONSUMER_SECRET
34 return self._search_regex(
35 r"var\s+cs\s*=\s*'([^']+)'", mainjs,
36 'consumer secret', default=self._CONSUMER_SECRET)
37
38 def _real_initialize(self):
39 self._login()
40 self._consumer_secret = self._get_consumer_secret()
41
42 def _login(self):
43 (username, password) = self._get_login_info()
44 if username is None:
45 return
46
47 login_form = {
48 'username': username,
49 'password': password,
50 }
51
52 request = sanitized_Request(
53 self._LOGIN_URL, urlencode_postdata(login_form))
54 response = self._download_webpage(
55 request, None, 'Logging in as %s' % username)
56
57 if all(logout_pattern not in response
58 for logout_pattern in ['href="/accounts/logout/"', '>Log out<']):
59 error = self._html_search_regex(
60 r'(?s)class="hidden-xs prompt"[^>]*>(.+?)<',
61 response, 'error message', default=None)
62 if error:
63 raise ExtractorError('Unable to login: %s' % error, expected=True)
64 raise ExtractorError('Unable to log in')
65
66
67 class DramaFeverIE(DramaFeverBaseIE):
68 IE_NAME = 'dramafever'
69 _VALID_URL = r'https?://(?:www\.)?dramafever\.com/(?:[^/]+/)?drama/(?P<id>[0-9]+/[0-9]+)(?:/|$)'
70 _TESTS = [{
71 'url': 'http://www.dramafever.com/drama/4512/1/Cooking_with_Shin/',
72 'info_dict': {
73 'id': '4512.1',
74 'ext': 'mp4',
75 'title': 'Cooking with Shin 4512.1',
76 'description': 'md5:a8eec7942e1664a6896fcd5e1287bfd0',
77 'episode': 'Episode 1',
78 'episode_number': 1,
79 'thumbnail': r're:^https?://.*\.jpg',
80 'timestamp': 1404336058,
81 'upload_date': '20140702',
82 'duration': 343,
83 },
84 'params': {
85 # m3u8 download
86 'skip_download': True,
87 },
88 }, {
89 'url': 'http://www.dramafever.com/drama/4826/4/Mnet_Asian_Music_Awards_2015/?ap=1',
90 'info_dict': {
91 'id': '4826.4',
92 'ext': 'mp4',
93 'title': 'Mnet Asian Music Awards 2015 4826.4',
94 'description': 'md5:3ff2ee8fedaef86e076791c909cf2e91',
95 'episode': 'Mnet Asian Music Awards 2015 - Part 3',
96 'episode_number': 4,
97 'thumbnail': r're:^https?://.*\.jpg',
98 'timestamp': 1450213200,
99 'upload_date': '20151215',
100 'duration': 5602,
101 },
102 'params': {
103 # m3u8 download
104 'skip_download': True,
105 },
106 }, {
107 'url': 'https://www.dramafever.com/zh-cn/drama/4972/15/Doctor_Romantic/',
108 'only_matching': True,
109 }]
110
111 def _real_extract(self, url):
112 video_id = self._match_id(url).replace('/', '.')
113
114 try:
115 info = self._extract_feed_info(
116 'http://www.dramafever.com/amp/episode/feed.json?guid=%s' % video_id)
117 except ExtractorError as e:
118 if isinstance(e.cause, compat_HTTPError):
119 raise ExtractorError(
120 'Currently unavailable in your country.', expected=True)
121 raise
122
123 series_id, episode_number = video_id.split('.')
124 episode_info = self._download_json(
125 # We only need a single episode info, so restricting page size to one episode
126 # and dealing with page number as with episode number
127 r'http://www.dramafever.com/api/4/episode/series/?cs=%s&series_id=%s&page_number=%s&page_size=1'
128 % (self._consumer_secret, series_id, episode_number),
129 video_id, 'Downloading episode info JSON', fatal=False)
130 if episode_info:
131 value = episode_info.get('value')
132 if isinstance(value, list):
133 for v in value:
134 if v.get('type') == 'Episode':
135 subfile = v.get('subfile') or v.get('new_subfile')
136 if subfile and subfile != 'http://www.dramafever.com/st/':
137 info.setdefault('subtitles', {}).setdefault('English', []).append({
138 'ext': 'srt',
139 'url': subfile,
140 })
141 episode_number = int_or_none(v.get('number'))
142 episode_fallback = 'Episode'
143 if episode_number:
144 episode_fallback += ' %d' % episode_number
145 info['episode'] = v.get('title') or episode_fallback
146 info['episode_number'] = episode_number
147 break
148
149 return info
150
151
152 class DramaFeverSeriesIE(DramaFeverBaseIE):
153 IE_NAME = 'dramafever:series'
154 _VALID_URL = r'https?://(?:www\.)?dramafever\.com/(?:[^/]+/)?drama/(?P<id>[0-9]+)(?:/(?:(?!\d+(?:/|$)).+)?)?$'
155 _TESTS = [{
156 'url': 'http://www.dramafever.com/drama/4512/Cooking_with_Shin/',
157 'info_dict': {
158 'id': '4512',
159 'title': 'Cooking with Shin',
160 'description': 'md5:84a3f26e3cdc3fb7f500211b3593b5c1',
161 },
162 'playlist_count': 4,
163 }, {
164 'url': 'http://www.dramafever.com/drama/124/IRIS/',
165 'info_dict': {
166 'id': '124',
167 'title': 'IRIS',
168 'description': 'md5:b3a30e587cf20c59bd1c01ec0ee1b862',
169 },
170 'playlist_count': 20,
171 }]
172
173 _PAGE_SIZE = 60 # max is 60 (see http://api.drama9.com/#get--api-4-episode-series-)
174
175 def _real_extract(self, url):
176 series_id = self._match_id(url)
177
178 series = self._download_json(
179 'http://www.dramafever.com/api/4/series/query/?cs=%s&series_id=%s'
180 % (self._consumer_secret, series_id),
181 series_id, 'Downloading series JSON')['series'][series_id]
182
183 title = clean_html(series['name'])
184 description = clean_html(series.get('description') or series.get('description_short'))
185
186 entries = []
187 for page_num in itertools.count(1):
188 episodes = self._download_json(
189 'http://www.dramafever.com/api/4/episode/series/?cs=%s&series_id=%s&page_size=%d&page_number=%d'
190 % (self._consumer_secret, series_id, self._PAGE_SIZE, page_num),
191 series_id, 'Downloading episodes JSON page #%d' % page_num)
192 for episode in episodes.get('value', []):
193 episode_url = episode.get('episode_url')
194 if not episode_url:
195 continue
196 entries.append(self.url_result(
197 compat_urlparse.urljoin(url, episode_url),
198 'DramaFever', episode.get('guid')))
199 if page_num == episodes['num_pages']:
200 break
201
202 return self.playlist_result(entries, series_id, title, description)