[adobetv] Support embeddable videos (closes #6039)
[youtube-dl] / youtube_dl / extractor / adobetv.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor
4 from ..utils import (
5     parse_duration,
6     unified_strdate,
7     str_to_int,
8     float_or_none,
9     ISO639Utils,
10 )
11
12
13 class AdobeTVIE(InfoExtractor):
14     _VALID_URL = r'https?://tv\.adobe\.com/watch/[^/]+/(?P<id>[^/]+)'
15
16     _TEST = {
17         'url': 'http://tv.adobe.com/watch/the-complete-picture-with-julieanne-kost/quick-tip-how-to-draw-a-circle-around-an-object-in-photoshop/',
18         'md5': '9bc5727bcdd55251f35ad311ca74fa1e',
19         'info_dict': {
20             'id': 'quick-tip-how-to-draw-a-circle-around-an-object-in-photoshop',
21             'ext': 'mp4',
22             'title': 'Quick Tip - How to Draw a Circle Around an Object in Photoshop',
23             'description': 'md5:99ec318dc909d7ba2a1f2b038f7d2311',
24             'thumbnail': 're:https?://.*\.jpg$',
25             'upload_date': '20110914',
26             'duration': 60,
27             'view_count': int,
28         },
29     }
30
31     def _real_extract(self, url):
32         video_id = self._match_id(url)
33         webpage = self._download_webpage(url, video_id)
34
35         player = self._parse_json(
36             self._search_regex(r'html5player:\s*({.+?})\s*\n', webpage, 'player'),
37             video_id)
38
39         title = player.get('title') or self._search_regex(
40             r'data-title="([^"]+)"', webpage, 'title')
41         description = self._og_search_description(webpage)
42         thumbnail = self._og_search_thumbnail(webpage)
43
44         upload_date = unified_strdate(
45             self._html_search_meta('datepublished', webpage, 'upload date'))
46
47         duration = parse_duration(
48             self._html_search_meta('duration', webpage, 'duration') or
49             self._search_regex(
50                 r'Runtime:\s*(\d{2}:\d{2}:\d{2})',
51                 webpage, 'duration', fatal=False))
52
53         view_count = str_to_int(self._search_regex(
54             r'<div class="views">\s*Views?:\s*([\d,.]+)\s*</div>',
55             webpage, 'view count'))
56
57         formats = [{
58             'url': source['src'],
59             'format_id': source.get('quality') or source['src'].split('-')[-1].split('.')[0] or None,
60             'tbr': source.get('bitrate'),
61         } for source in player['sources']]
62         self._sort_formats(formats)
63
64         return {
65             'id': video_id,
66             'title': title,
67             'description': description,
68             'thumbnail': thumbnail,
69             'upload_date': upload_date,
70             'duration': duration,
71             'view_count': view_count,
72             'formats': formats,
73         }
74
75
76 class AdobeTVVideoIE(InfoExtractor):
77     _VALID_URL = r'https?://video\.tv\.adobe\.com/v/(?P<id>\d+)'
78
79     _TEST = {
80         'url': 'https://video.tv.adobe.com/v/2456/',
81         'md5': '43662b577c018ad707a63766462b1e87',
82         'info_dict': {
83             'id': '2456',
84             'ext': 'mp4',
85             'title': 'New experience with Acrobat DC',
86             'description': 'New experience with Acrobat DC',
87             'duration': 248.667,
88         },
89     }
90
91     def _real_extract(self, url):
92         video_id = self._match_id(url)
93
94         webpage = self._download_webpage(url, video_id)
95
96         player_params = self._parse_json(self._search_regex(
97             r'var\s+bridge\s*=\s*([^;]+);', webpage, 'player parameters'),
98             video_id)
99
100         formats = [{
101             'url': source['src'],
102             'width': source.get('width'),
103             'height': source.get('height'),
104             'tbr': source.get('bitrate'),
105         } for source in player_params['sources']]
106
107         # For both metadata and downloaded files the duration varies among
108         # formats. I just pick the max one
109         duration = max(filter(None, [
110             float_or_none(source.get('duration'), scale=1000)
111             for source in player_params['sources']]))
112
113         subtitles = {}
114         for translation in player_params.get('translations', []):
115             lang_id = translation.get('language_w3c') or ISO639Utils.long2short(translation['language_medium'])
116             if lang_id not in subtitles:
117                 subtitles[lang_id] = []
118             subtitles[lang_id].append({
119                 'url': translation['vttPath'],
120                 'ext': 'vtt',
121             })
122
123         return {
124             'id': video_id,
125             'formats': formats,
126             'title': player_params['title'],
127             'description': self._og_search_description(webpage),
128             'duration': duration,
129             'subtitles': subtitles,
130         }