c345e0274192383790f32eeb1db1f3e013870238
[ytdl] / youtube_dl / extractor / dispeak.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     int_or_none,
8     parse_duration,
9     remove_end,
10     xpath_element,
11     xpath_text,
12 )
13
14
15 class DigitallySpeakingIE(InfoExtractor):
16     _VALID_URL = r'https?://(?:s?evt\.dispeak|events\.digitallyspeaking)\.com/(?:[^/]+/)+xml/(?P<id>[^.]+)\.xml'
17
18     _TESTS = [{
19         # From http://gdcvault.com/play/1023460/Tenacious-Design-and-The-Interface
20         'url': 'http://evt.dispeak.com/ubm/gdc/sf16/xml/840376_BQRC.xml',
21         'md5': 'a8efb6c31ed06ca8739294960b2dbabd',
22         'info_dict': {
23             'id': '840376_BQRC',
24             'ext': 'mp4',
25             'title': 'Tenacious Design and The Interface of \'Destiny\'',
26         },
27     }, {
28         # From http://www.gdcvault.com/play/1014631/Classic-Game-Postmortem-PAC
29         'url': 'http://events.digitallyspeaking.com/gdc/sf11/xml/12396_1299111843500GMPX.xml',
30         'only_matching': True,
31     }, {
32         # From http://www.gdcvault.com/play/1013700/Advanced-Material
33         'url': 'http://sevt.dispeak.com/ubm/gdc/eur10/xml/11256_1282118587281VNIT.xml',
34         'only_matching': True,
35     }]
36
37     def _parse_mp4(self, metadata):
38         video_formats = []
39         video_root = None
40
41         mp4_video = xpath_text(metadata, './mp4video', default=None)
42         if mp4_video is not None:
43             mobj = re.match(r'(?P<root>https?://.*?/).*', mp4_video)
44             video_root = mobj.group('root')
45         if video_root is None:
46             http_host = xpath_text(metadata, 'httpHost', default=None)
47             if http_host:
48                 video_root = 'http://%s/' % http_host
49         if video_root is None:
50             # Hard-coded in http://evt.dispeak.com/ubm/gdc/sf16/custom/player2.js
51             # Works for GPUTechConf, too
52             video_root = 'http://s3-2u.digitallyspeaking.com/'
53
54         formats = metadata.findall('./MBRVideos/MBRVideo')
55         if not formats:
56             return None
57         for a_format in formats:
58             stream_name = xpath_text(a_format, 'streamName', fatal=True)
59             video_path = re.match(r'mp4\:(?P<path>.*)', stream_name).group('path')
60             url = video_root + video_path
61             bitrate = xpath_text(a_format, 'bitrate')
62             tbr = int_or_none(bitrate)
63             vbr = int_or_none(self._search_regex(
64                 r'-(\d+)\.mp4', video_path, 'vbr', default=None))
65             abr = tbr - vbr if tbr and vbr else None
66             video_formats.append({
67                 'format_id': bitrate,
68                 'url': url,
69                 'tbr': tbr,
70                 'vbr': vbr,
71                 'abr': abr,
72             })
73         return video_formats
74
75     def _parse_flv(self, metadata):
76         formats = []
77         akamai_url = xpath_text(metadata, './akamaiHost', fatal=True)
78         audios = metadata.findall('./audios/audio')
79         for audio in audios:
80             formats.append({
81                 'url': 'rtmp://%s/ondemand?ovpfv=1.1' % akamai_url,
82                 'play_path': remove_end(audio.get('url'), '.flv'),
83                 'ext': 'flv',
84                 'vcodec': 'none',
85                 'format_id': audio.get('code'),
86             })
87         slide_video_path = xpath_text(metadata, './slideVideo', fatal=True)
88         formats.append({
89             'url': 'rtmp://%s/ondemand?ovpfv=1.1' % akamai_url,
90             'play_path': remove_end(slide_video_path, '.flv'),
91             'ext': 'flv',
92             'format_note': 'slide deck video',
93             'quality': -2,
94             'preference': -2,
95             'format_id': 'slides',
96         })
97         speaker_video_path = xpath_text(metadata, './speakerVideo', fatal=True)
98         formats.append({
99             'url': 'rtmp://%s/ondemand?ovpfv=1.1' % akamai_url,
100             'play_path': remove_end(speaker_video_path, '.flv'),
101             'ext': 'flv',
102             'format_note': 'speaker video',
103             'quality': -1,
104             'preference': -1,
105             'format_id': 'speaker',
106         })
107         return formats
108
109     def _real_extract(self, url):
110         video_id = self._match_id(url)
111
112         xml_description = self._download_xml(url, video_id)
113         metadata = xpath_element(xml_description, 'metadata')
114
115         video_formats = self._parse_mp4(metadata)
116         if video_formats is None:
117             video_formats = self._parse_flv(metadata)
118
119         return {
120             'id': video_id,
121             'formats': video_formats,
122             'title': xpath_text(metadata, 'title', fatal=True),
123             'duration': parse_duration(xpath_text(metadata, 'endTime')),
124             'creator': xpath_text(metadata, 'speaker'),
125         }