[youtube] Fix extraction.
[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 ..utils import ExtractorError
8
9
10 class Sport5IE(InfoExtractor):
11     _VALID_URL = r'https?://(?:www|vod)?\.sport5\.co\.il/.*\b(?:Vi|docID)=(?P<id>\d+)'
12     _TESTS = [
13         {
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': 'ולנסיה-קורדובה 0:3',
19                 'description': 'אלקאסר, גאייה ופגולי סידרו לקבוצה של נונו ניצחון על קורדובה ואת המקום הראשון בליגה',
20                 'duration': 228,
21                 'categories': list,
22             },
23             'skip': 'Blocked outside of Israel',
24         }, {
25             'url': 'http://www.sport5.co.il/articles.aspx?FolderID=3075&docID=176372&lang=HE',
26             'info_dict': {
27                 'id': 's5-SiXxx1-hKh2',
28                 'ext': 'mp4',
29                 'title': 'GOALS_CELTIC_270914.mp4',
30                 'description': '',
31                 'duration': 87,
32                 'categories': list,
33             },
34             'skip': 'Blocked outside of Israel',
35         }
36     ]
37
38     def _real_extract(self, url):
39         mobj = re.match(self._VALID_URL, url)
40         media_id = mobj.group('id')
41
42         webpage = self._download_webpage(url, media_id)
43
44         video_id = self._html_search_regex(r'clipId=([\w-]+)', webpage, 'video id')
45
46         metadata = self._download_xml(
47             'http://sport5-metadata-rr-d.nsacdn.com/vod/vod/%s/HDS/metadata.xml' % video_id,
48             video_id)
49
50         error = metadata.find('./Error')
51         if error is not None:
52             raise ExtractorError(
53                 '%s returned error: %s - %s' % (
54                     self.IE_NAME,
55                     error.find('./Name').text,
56                     error.find('./Description').text),
57                 expected=True)
58
59         title = metadata.find('./Title').text
60         description = metadata.find('./Description').text
61         duration = int(metadata.find('./Duration').text)
62
63         posters_el = metadata.find('./PosterLinks')
64         thumbnails = [{
65             'url': thumbnail.text,
66             'width': int(thumbnail.get('width')),
67             'height': int(thumbnail.get('height')),
68         } for thumbnail in posters_el.findall('./PosterIMG')] if posters_el is not None else []
69
70         categories_el = metadata.find('./Categories')
71         categories = [
72             cat.get('name') for cat in categories_el.findall('./Category')
73         ] if categories_el is not None else []
74
75         formats = [{
76             'url': fmt.text,
77             'ext': 'mp4',
78             'vbr': int(fmt.get('bitrate')),
79             'width': int(fmt.get('width')),
80             'height': int(fmt.get('height')),
81         } for fmt in metadata.findall('./PlaybackLinks/FileURL')]
82         self._sort_formats(formats)
83
84         return {
85             'id': video_id,
86             'title': title,
87             'description': description,
88             'thumbnails': thumbnails,
89             'duration': duration,
90             'categories': categories,
91             'formats': formats,
92         }