[dramafever:series] Rollback _PAGE_SIZE to max possible
[youtube-dl] / 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_urlparse,
10 )
11 from ..utils import (
12     ExtractorError,
13     clean_html,
14     determine_ext,
15     int_or_none,
16     parse_iso8601,
17 )
18
19
20 class DramaFeverIE(InfoExtractor):
21     IE_NAME = 'dramafever'
22     _VALID_URL = r'https?://(?:www\.)?dramafever\.com/drama/(?P<id>[0-9]+/[0-9]+)'
23     _TEST = {
24         'url': 'http://www.dramafever.com/drama/4512/1/Cooking_with_Shin/',
25         'info_dict': {
26             'id': '4512.1',
27             'ext': 'flv',
28             'title': 'Cooking with Shin 4512.1',
29             'description': 'md5:a8eec7942e1664a6896fcd5e1287bfd0',
30             'thumbnail': 're:^https?://.*\.jpg',
31             'timestamp': 1404336058,
32             'upload_date': '20140702',
33             'duration': 343,
34         }
35     }
36
37     def _real_extract(self, url):
38         video_id = self._match_id(url).replace('/', '.')
39
40         try:
41             feed = self._download_json(
42                 'http://www.dramafever.com/amp/episode/feed.json?guid=%s' % video_id,
43                 video_id, 'Downloading episode JSON')['channel']['item']
44         except ExtractorError as e:
45             if isinstance(e.cause, compat_HTTPError):
46                 raise ExtractorError(
47                     'Currently unavailable in your country.', expected=True)
48             raise
49
50         media_group = feed.get('media-group', {})
51
52         formats = []
53         for media_content in media_group['media-content']:
54             src = media_content.get('@attributes', {}).get('url')
55             if not src:
56                 continue
57             ext = determine_ext(src)
58             if ext == 'f4m':
59                 formats.extend(self._extract_f4m_formats(
60                     src, video_id, f4m_id='hds'))
61             elif ext == 'm3u8':
62                 formats.extend(self._extract_m3u8_formats(
63                     src, video_id, 'mp4', m3u8_id='hls'))
64             else:
65                 formats.append({
66                     'url': src,
67                 })
68         self._sort_formats(formats)
69
70         title = media_group.get('media-title')
71         description = media_group.get('media-description')
72         duration = int_or_none(media_group['media-content'][0].get('@attributes', {}).get('duration'))
73         thumbnail = self._proto_relative_url(
74             media_group.get('media-thumbnail', {}).get('@attributes', {}).get('url'))
75         timestamp = parse_iso8601(feed.get('pubDate'), ' ')
76
77         subtitles = {}
78         for media_subtitle in media_group.get('media-subTitle', []):
79             lang = media_subtitle.get('@attributes', {}).get('lang')
80             href = media_subtitle.get('@attributes', {}).get('href')
81             if not lang or not href:
82                 continue
83             subtitles[lang] = [{
84                 'ext': 'ttml',
85                 'url': href,
86             }]
87
88         return {
89             'id': video_id,
90             'title': title,
91             'description': description,
92             'thumbnail': thumbnail,
93             'timestamp': timestamp,
94             'duration': duration,
95             'formats': formats,
96             'subtitles': subtitles,
97         }
98
99
100 class DramaFeverSeriesIE(InfoExtractor):
101     IE_NAME = 'dramafever:series'
102     _VALID_URL = r'https?://(?:www\.)?dramafever\.com/drama/(?P<id>[0-9]+)(?:/(?:(?!\d+(?:/|$)).+)?)?$'
103     _TESTS = [{
104         'url': 'http://www.dramafever.com/drama/4512/Cooking_with_Shin/',
105         'info_dict': {
106             'id': '4512',
107             'title': 'Cooking with Shin',
108             'description': 'md5:84a3f26e3cdc3fb7f500211b3593b5c1',
109         },
110         'playlist_count': 4,
111     }, {
112         'url': 'http://www.dramafever.com/drama/124/IRIS/',
113         'info_dict': {
114             'id': '124',
115             'title': 'IRIS',
116             'description': 'md5:b3a30e587cf20c59bd1c01ec0ee1b862',
117         },
118         'playlist_count': 20,
119     }]
120
121     _CONSUMER_SECRET = 'DA59dtVXYLxajktV'
122     _PAGE_SIZE = 60  # max is 60 (see http://api.drama9.com/#get--api-4-episode-series-)
123
124     def _get_consumer_secret(self, video_id):
125         mainjs = self._download_webpage(
126             'http://www.dramafever.com/static/51afe95/df2014/scripts/main.js',
127             video_id, 'Downloading main.js', fatal=False)
128         if not mainjs:
129             return self._CONSUMER_SECRET
130         return self._search_regex(
131             r"var\s+cs\s*=\s*'([^']+)'", mainjs,
132             'consumer secret', default=self._CONSUMER_SECRET)
133
134     def _real_extract(self, url):
135         series_id = self._match_id(url)
136
137         consumer_secret = self._get_consumer_secret(series_id)
138
139         series = self._download_json(
140             'http://www.dramafever.com/api/4/series/query/?cs=%s&series_id=%s'
141             % (consumer_secret, series_id),
142             series_id, 'Downloading series JSON')['series'][series_id]
143
144         title = clean_html(series['name'])
145         description = clean_html(series.get('description') or series.get('description_short'))
146
147         entries = []
148         for page_num in itertools.count(1):
149             episodes = self._download_json(
150                 'http://www.dramafever.com/api/4/episode/series/?cs=%s&series_id=%s&page_size=%d&page_number=%d'
151                 % (consumer_secret, series_id, self._PAGE_SIZE, page_num),
152                 series_id, 'Downloading episodes JSON page #%d' % page_num)
153             for episode in episodes.get('value', []):
154                 entries.append(self.url_result(
155                     compat_urlparse.urljoin(url, episode['episode_url']),
156                     'DramaFever', episode.get('guid')))
157             if page_num == episodes['num_pages']:
158                 break
159
160         return self.playlist_result(entries, series_id, title, description)