Merge branch 'sport5' of https://github.com/lenaten/youtube-dl into lenaten-sport5
[youtube-dl] / youtube_dl / extractor / sport5.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from youtube_dl.utils import compat_str, compat_urlretrieve
8
9
10
11 class Sport5IE(InfoExtractor):
12     _VALID_URL = r'http://.*sport5\.co\.il'
13     _TESTS = [{
14             'url': 'http://vod.sport5.co.il/?Vc=147&Vi=176331&Page=1',
15             'info_dict': {
16                 'id': 's5-Y59xx1-GUh2',
17                 'ext': 'mp4',
18                 'title': 'md5:4a2a5eba7e7dc88fdc446cbca8a41c79',
19             }
20         }, {
21             'url': 'http://www.sport5.co.il/articles.aspx?FolderID=3075&docID=176372&lang=HE',
22             'info_dict': {
23                 'id': 's5-SiXxx1-hKh2',
24                 'ext': 'mp4',
25                 'title': 'md5:5cb1c6bfc0f16086e59f6683013f8e02',
26             }
27         }
28     ]
29
30     def _real_extract(self, url):
31         mobj = re.match(self._VALID_URL, url)
32
33         webpage = self._download_webpage(url, '')
34
35         media_id = self._html_search_regex('clipId=(s5-\w+-\w+)', webpage, 'media id')
36
37         xml = self._download_xml(
38             'http://sport5-metadata-rr-d.nsacdn.com/vod/vod/%s/HDS/metadata.xml' % media_id,
39             media_id, 'Downloading media XML')
40
41         title = xml.find('./Title').text
42         duration = xml.find('./Duration').text
43         description = xml.find('./Description').text
44         thumbnail = xml.find('./PosterLinks/PosterIMG').text
45         player_url = xml.find('./PlaybackLinks/PlayerUrl').text
46         file_els = xml.findall('./PlaybackLinks/FileURL')
47
48         formats = []
49
50         for file_el in file_els:
51             bitrate = file_el.attrib.get('bitrate')
52             width = int(file_el.attrib.get('width'))
53             height = int(file_el.attrib.get('height'))
54             formats.append({
55                 'url': compat_str(file_el.text),
56                 'ext': 'mp4',
57                 'height': height,
58                 'width': width
59             })
60
61         self._sort_formats(formats)
62
63         return {
64             'id': media_id,
65             'title': title,
66             'thumbnail': thumbnail,
67             'duration': duration,
68             'formats': formats,
69             'player_url': player_url,
70         }