[adobetv] extract AdobeTVVideo info from json directly
[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 AdobeTVVideoIE(InfoExtractor):
66     _VALID_URL = r'https?://video\.tv\.adobe\.com/v/(?P<id>\d+)'
67
68     _TEST = {
69         # From https://helpx.adobe.com/acrobat/how-to/new-experience-acrobat-dc.html?set=acrobat--get-started--essential-beginners
70         'url': 'https://video.tv.adobe.com/v/2456/',
71         'md5': '43662b577c018ad707a63766462b1e87',
72         'info_dict': {
73             'id': '2456',
74             'ext': 'mp4',
75             'title': 'New experience with Acrobat DC',
76             'description': 'New experience with Acrobat DC',
77             'duration': 248.667,
78         },
79     }
80
81     def _real_extract(self, url):
82         video_id = self._match_id(url)
83         video_data = self._download_json(url + '?format=json', video_id)
84
85         formats = [{
86             'format_id': '%s-%s' % (determine_ext(source['src']), source.get('height')),
87             'url': source['src'],
88             'width': int_or_none(source.get('width')),
89             'height': int_or_none(source.get('height')),
90             'tbr': int_or_none(source.get('bitrate')),
91         } for source in video_data['sources']]
92         self._sort_formats(formats)
93
94         # For both metadata and downloaded files the duration varies among
95         # formats. I just pick the max one
96         duration = max(filter(None, [
97             float_or_none(source.get('duration'), scale=1000)
98             for source in video_data['sources']]))
99
100         subtitles = {}
101         for translation in video_data.get('translations', []):
102             lang_id = translation.get('language_w3c') or ISO639Utils.long2short(translation['language_medium'])
103             if lang_id not in subtitles:
104                 subtitles[lang_id] = []
105             subtitles[lang_id].append({
106                 'url': translation['vttPath'],
107                 'ext': 'vtt',
108             })
109
110         return {
111             'id': video_id,
112             'formats': formats,
113             'title': video_data['title'],
114             'description': video_data.get('description'),
115             'thumbnail': video_data['video'].get('poster'),
116             'duration': duration,
117             'subtitles': subtitles,
118         }