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