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