[adobetv] use a variable for api base url
[youtube-dl] / youtube_dl / extractor / adobetv.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     parse_duration,
8     unified_strdate,
9     str_to_int,
10     int_or_none,
11     float_or_none,
12     ISO639Utils,
13     determine_ext,
14 )
15
16
17 class AdobeTVBaseIE(InfoExtractor):
18     _API_BASE_URL = 'http://tv.adobe.com/api/v4/'
19
20
21 class AdobeTVIE(AdobeTVBaseIE):
22     _VALID_URL = r'https?://tv\.adobe\.com/(?:(?P<language>fr|de|es|jp)/)?watch/(?P<show_urlname>[^/]+)/(?P<id>[^/]+)'
23
24     _TEST = {
25         'url': 'http://tv.adobe.com/watch/the-complete-picture-with-julieanne-kost/quick-tip-how-to-draw-a-circle-around-an-object-in-photoshop/',
26         'md5': '9bc5727bcdd55251f35ad311ca74fa1e',
27         'info_dict': {
28             'id': '10981',
29             'ext': 'mp4',
30             'title': 'Quick Tip - How to Draw a Circle Around an Object in Photoshop',
31             'description': 'md5:99ec318dc909d7ba2a1f2b038f7d2311',
32             'thumbnail': 're:https?://.*\.jpg$',
33             'upload_date': '20110914',
34             'duration': 60,
35             'view_count': int,
36         },
37     }
38
39     def _real_extract(self, url):
40         language, show_urlname, urlname = re.match(self._VALID_URL, url).groups()
41         if not language:
42             language = 'en'
43
44         video_data = self._download_json(
45             self._API_BASE_URL + 'episode/get/?language=%s&show_urlname=%s&urlname=%s&disclosure=standard' % (language, show_urlname, urlname),
46             urlname)['data'][0]
47
48         formats = [{
49             'url': source['url'],
50             'format_id': source.get('quality_level') or source['url'].split('-')[-1].split('.')[0] or None,
51             'width': int_or_none(source.get('width')),
52             'height': int_or_none(source.get('height')),
53             'tbr': int_or_none(source.get('video_data_rate')),
54         } for source in video_data['videos']]
55         self._sort_formats(formats)
56
57         return {
58             'id': str(video_data['id']),
59             'title': video_data['title'],
60             'description': video_data.get('description'),
61             'thumbnail': video_data.get('thumbnail'),
62             'upload_date': unified_strdate(video_data.get('start_date')),
63             'duration': parse_duration(video_data.get('duration')),
64             'view_count': str_to_int(video_data.get('playcount')),
65             'formats': formats,
66         }
67
68
69 class AdobeTVPlaylistBaseIE(AdobeTVBaseIE):
70     def _parse_page_data(self, page_data):
71         return [self.url_result(self._get_element_url(element_data)) for element_data in page_data]
72
73     def _extract_playlist_entries(self, url, display_id):
74         page = self._download_json(url, display_id)
75         entries = self._parse_page_data(page['data'])
76         for page_num in range(2, page['paging']['pages'] + 1):
77             entries.extend(self._parse_page_data(
78                 self._download_json(url + '&page=%d' % page_num, display_id)['data']))
79         return entries
80
81
82 class AdobeTVShowIE(AdobeTVPlaylistBaseIE):
83     _VALID_URL = r'https?://tv\.adobe\.com/(?:(?P<language>fr|de|es|jp)/)?show/(?P<id>[^/]+)'
84
85     _TEST = {
86         'url': 'http://tv.adobe.com/show/the-complete-picture-with-julieanne-kost',
87         'info_dict': {
88             'id': '36',
89             'title': 'The Complete Picture with Julieanne Kost',
90             'description': 'md5:fa50867102dcd1aa0ddf2ab039311b27',
91         },
92         'playlist_mincount': 136,
93     }
94
95     def _get_element_url(self, element_data):
96         return element_data['urls'][0]
97
98     def _real_extract(self, url):
99         language, show_urlname = re.match(self._VALID_URL, url).groups()
100         if not language:
101             language = 'en'
102         query = 'language=%s&show_urlname=%s' % (language, show_urlname)
103
104         show_data = self._download_json(self._API_BASE_URL + 'show/get/?%s' % query, show_urlname)['data'][0]
105
106         return self.playlist_result(
107             self._extract_playlist_entries(self._API_BASE_URL + 'episode/?%s' % query, show_urlname),
108             str(show_data['id']),
109             show_data['show_name'],
110             show_data['show_description'])
111
112
113 class AdobeTVChannelIE(AdobeTVPlaylistBaseIE):
114     _VALID_URL = r'https?://tv\.adobe\.com/(?:(?P<language>fr|de|es|jp)/)?channel/(?P<id>[^/]+)(?:/(?P<category_urlname>[^/]+))?'
115
116     _TEST = {
117         'url': 'http://tv.adobe.com/channel/development',
118         'info_dict': {
119             'id': 'development',
120         },
121         'playlist_mincount': 96,
122     }
123
124     def _get_element_url(self, element_data):
125         return element_data['url']
126
127     def _real_extract(self, url):
128         language, channel_urlname, category_urlname = re.match(self._VALID_URL, url).groups()
129         if not language:
130             language = 'en'
131         query = 'language=%s&channel_urlname=%s' % (language, channel_urlname)
132         if category_urlname:
133             query += '&category_urlname=%s' % category_urlname
134
135         return self.playlist_result(
136             self._extract_playlist_entries(self._API_BASE_URL + 'show/?%s' % query, channel_urlname),
137             channel_urlname)
138
139
140 class AdobeTVVideoIE(InfoExtractor):
141     _VALID_URL = r'https?://video\.tv\.adobe\.com/v/(?P<id>\d+)'
142
143     _TEST = {
144         # From https://helpx.adobe.com/acrobat/how-to/new-experience-acrobat-dc.html?set=acrobat--get-started--essential-beginners
145         'url': 'https://video.tv.adobe.com/v/2456/',
146         'md5': '43662b577c018ad707a63766462b1e87',
147         'info_dict': {
148             'id': '2456',
149             'ext': 'mp4',
150             'title': 'New experience with Acrobat DC',
151             'description': 'New experience with Acrobat DC',
152             'duration': 248.667,
153         },
154     }
155
156     def _real_extract(self, url):
157         video_id = self._match_id(url)
158         video_data = self._download_json(url + '?format=json', video_id)
159
160         formats = [{
161             'format_id': '%s-%s' % (determine_ext(source['src']), source.get('height')),
162             'url': source['src'],
163             'width': int_or_none(source.get('width')),
164             'height': int_or_none(source.get('height')),
165             'tbr': int_or_none(source.get('bitrate')),
166         } for source in video_data['sources']]
167         self._sort_formats(formats)
168
169         # For both metadata and downloaded files the duration varies among
170         # formats. I just pick the max one
171         duration = max(filter(None, [
172             float_or_none(source.get('duration'), scale=1000)
173             for source in video_data['sources']]))
174
175         subtitles = {}
176         for translation in video_data.get('translations', []):
177             lang_id = translation.get('language_w3c') or ISO639Utils.long2short(translation['language_medium'])
178             if lang_id not in subtitles:
179                 subtitles[lang_id] = []
180             subtitles[lang_id].append({
181                 'url': translation['vttPath'],
182                 'ext': 'vtt',
183             })
184
185         return {
186             'id': video_id,
187             'formats': formats,
188             'title': video_data['title'],
189             'description': video_data.get('description'),
190             'thumbnail': video_data['video'].get('poster'),
191             'duration': duration,
192             'subtitles': subtitles,
193         }