Merge pull request #9110 from remitamine/parse_duration
[youtube-dl] / 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 DigitalSpeakingIE(InfoExtractor):
16     _VALID_URL = r'http://(?:evt\.dispeak|events\.digitallyspeaking)\.com/([^/]+/)+xml/(?P<id>[^.]+).xml'
17
18     _TESTS = [{
19         # From http://evt.dispeak.com/ubm/gdc/sf16/xml/840376_BQRC.xml
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
33     def _parse_mp4(self, metadata):
34         video_formats = []
35         video_root = None
36
37         mp4_video = xpath_text(metadata, './mp4video', default=None)
38         if mp4_video is not None:
39             mobj = re.match(r'(?P<root>https?://.*?/).*', mp4_video)
40             video_root = mobj.group('root')
41         if video_root is None:
42             http_host = xpath_text(metadata, 'httpHost', default=None)
43             if http_host:
44                 video_root = 'http://%s/' % http_host
45         if video_root is None:
46             # Hard-coded in http://evt.dispeak.com/ubm/gdc/sf16/custom/player2.js
47             # Works for GPUTechConf, too
48             video_root = 'http://s3-2u.digitallyspeaking.com/'
49
50         formats = metadata.findall('./MBRVideos/MBRVideo')
51         if not formats:
52             return None
53         for a_format in formats:
54             stream_name = xpath_text(a_format, 'streamName', fatal=True)
55             video_path = re.match(r'mp4\:(?P<path>.*)', stream_name).group('path')
56             url = video_root + video_path
57             vbr = xpath_text(a_format, 'bitrate')
58             video_formats.append({
59                 'url': url,
60                 'vbr': int_or_none(vbr),
61             })
62         return video_formats
63
64     def _parse_flv(self, metadata):
65         formats = []
66         akamai_url = xpath_text(metadata, './akamaiHost', fatal=True)
67         audios = metadata.findall('./audios/audio')
68         for audio in audios:
69             formats.append({
70                 'url': 'rtmp://%s/ondemand?ovpfv=1.1' % akamai_url,
71                 'play_path': remove_end(audio.get('url'), '.flv'),
72                 'ext': 'flv',
73                 'vcodec': 'none',
74                 'format_id': audio.get('code'),
75             })
76         slide_video_path = xpath_text(metadata, './slideVideo', fatal=True)
77         formats.append({
78             'url': 'rtmp://%s/ondemand?ovpfv=1.1' % akamai_url,
79             'play_path': remove_end(slide_video_path, '.flv'),
80             'ext': 'flv',
81             'format_note': 'slide deck video',
82             'quality': -2,
83             'preference': -2,
84             'format_id': 'slides',
85         })
86         speaker_video_path = xpath_text(metadata, './speakerVideo', fatal=True)
87         formats.append({
88             'url': 'rtmp://%s/ondemand?ovpfv=1.1' % akamai_url,
89             'play_path': remove_end(speaker_video_path, '.flv'),
90             'ext': 'flv',
91             'format_note': 'speaker video',
92             'quality': -1,
93             'preference': -1,
94             'format_id': 'speaker',
95         })
96         return formats
97
98     def _real_extract(self, url):
99         video_id = self._match_id(url)
100
101         xml_description = self._download_xml(url, video_id)
102         metadata = xpath_element(xml_description, 'metadata')
103
104         video_formats = self._parse_mp4(metadata)
105         if video_formats is None:
106             video_formats = self._parse_flv(metadata)
107
108         return {
109             'id': video_id,
110             'formats': video_formats,
111             'title': xpath_text(metadata, 'title', fatal=True),
112             'duration': parse_duration(xpath_text(metadata, 'endTime')),
113             'creator': xpath_text(metadata, 'speaker'),
114         }