]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/dramafever.py
95883a037f537de47a0ee9f3f18d913eb9ec1647
[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 remove_end,
16 sanitized_Request,
17 urlencode_postdata
18 )
19
20
21 class DramaFeverBaseIE(AMPIE):
22 _LOGIN_URL = 'https://www.dramafever.com/accounts/login/'
23 _NETRC_MACHINE = 'dramafever'
24 _GEO_COUNTRIES = ['US', 'CA']
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 = sanitized_Request(
55 self._LOGIN_URL, urlencode_postdata(login_form))
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)<h\d[^>]+\bclass="hidden-xs prompt"[^>]*>(.+?)</h\d',
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 _TESTS = [{
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',
78 'description': 'md5:a8eec7942e1664a6896fcd5e1287bfd0',
79 'episode': 'Episode 1',
80 'episode_number': 1,
81 'thumbnail': r're:^https?://.*\.jpg',
82 'timestamp': 1404336058,
83 'upload_date': '20140702',
84 'duration': 344,
85 },
86 'params': {
87 # m3u8 download
88 'skip_download': True,
89 },
90 }, {
91 'url': 'http://www.dramafever.com/drama/4826/4/Mnet_Asian_Music_Awards_2015/?ap=1',
92 'info_dict': {
93 'id': '4826.4',
94 'ext': 'flv',
95 'title': 'Mnet Asian Music Awards 2015',
96 'description': 'md5:3ff2ee8fedaef86e076791c909cf2e91',
97 'episode': 'Mnet Asian Music Awards 2015 - Part 3',
98 'episode_number': 4,
99 'thumbnail': r're:^https?://.*\.jpg',
100 'timestamp': 1450213200,
101 'upload_date': '20151215',
102 'duration': 5359,
103 },
104 'params': {
105 # m3u8 download
106 'skip_download': True,
107 },
108 }, {
109 'url': 'https://www.dramafever.com/zh-cn/drama/4972/15/Doctor_Romantic/',
110 'only_matching': True,
111 }]
112
113 def _real_extract(self, url):
114 video_id = self._match_id(url).replace('/', '.')
115
116 try:
117 info = self._extract_feed_info(
118 'http://www.dramafever.com/amp/episode/feed.json?guid=%s' % video_id)
119 except ExtractorError as e:
120 if isinstance(e.cause, compat_HTTPError):
121 self.raise_geo_restricted(
122 msg='Currently unavailable in your country',
123 countries=self._GEO_COUNTRIES)
124 raise
125
126 # title is postfixed with video id for some reason, removing
127 if info.get('title'):
128 info['title'] = remove_end(info['title'], video_id).strip()
129
130 series_id, episode_number = video_id.split('.')
131 episode_info = self._download_json(
132 # We only need a single episode info, so restricting page size to one episode
133 # and dealing with page number as with episode number
134 r'http://www.dramafever.com/api/4/episode/series/?cs=%s&series_id=%s&page_number=%s&page_size=1'
135 % (self._consumer_secret, series_id, episode_number),
136 video_id, 'Downloading episode info JSON', fatal=False)
137 if episode_info:
138 value = episode_info.get('value')
139 if isinstance(value, list):
140 for v in value:
141 if v.get('type') == 'Episode':
142 subfile = v.get('subfile') or v.get('new_subfile')
143 if subfile and subfile != 'http://www.dramafever.com/st/':
144 info.setdefault('subtitles', {}).setdefault('English', []).append({
145 'ext': 'srt',
146 'url': subfile,
147 })
148 episode_number = int_or_none(v.get('number'))
149 episode_fallback = 'Episode'
150 if episode_number:
151 episode_fallback += ' %d' % episode_number
152 info['episode'] = v.get('title') or episode_fallback
153 info['episode_number'] = episode_number
154 break
155
156 return info
157
158
159 class DramaFeverSeriesIE(DramaFeverBaseIE):
160 IE_NAME = 'dramafever:series'
161 _VALID_URL = r'https?://(?:www\.)?dramafever\.com/(?:[^/]+/)?drama/(?P<id>[0-9]+)(?:/(?:(?!\d+(?:/|$)).+)?)?$'
162 _TESTS = [{
163 'url': 'http://www.dramafever.com/drama/4512/Cooking_with_Shin/',
164 'info_dict': {
165 'id': '4512',
166 'title': 'Cooking with Shin',
167 'description': 'md5:84a3f26e3cdc3fb7f500211b3593b5c1',
168 },
169 'playlist_count': 4,
170 }, {
171 'url': 'http://www.dramafever.com/drama/124/IRIS/',
172 'info_dict': {
173 'id': '124',
174 'title': 'IRIS',
175 'description': 'md5:b3a30e587cf20c59bd1c01ec0ee1b862',
176 },
177 'playlist_count': 20,
178 }]
179
180 _PAGE_SIZE = 60 # max is 60 (see http://api.drama9.com/#get--api-4-episode-series-)
181
182 def _real_extract(self, url):
183 series_id = self._match_id(url)
184
185 series = self._download_json(
186 'http://www.dramafever.com/api/4/series/query/?cs=%s&series_id=%s'
187 % (self._consumer_secret, series_id),
188 series_id, 'Downloading series JSON')['series'][series_id]
189
190 title = clean_html(series['name'])
191 description = clean_html(series.get('description') or series.get('description_short'))
192
193 entries = []
194 for page_num in itertools.count(1):
195 episodes = self._download_json(
196 'http://www.dramafever.com/api/4/episode/series/?cs=%s&series_id=%s&page_size=%d&page_number=%d'
197 % (self._consumer_secret, series_id, self._PAGE_SIZE, page_num),
198 series_id, 'Downloading episodes JSON page #%d' % page_num)
199 for episode in episodes.get('value', []):
200 episode_url = episode.get('episode_url')
201 if not episode_url:
202 continue
203 entries.append(self.url_result(
204 compat_urlparse.urljoin(url, episode_url),
205 'DramaFever', episode.get('guid')))
206 if page_num == episodes['num_pages']:
207 break
208
209 return self.playlist_result(entries, series_id, title, description)