[Srf] Add new extractor (fixes #981)
[youtube-dl] / youtube_dl / extractor / srf.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 from .common import InfoExtractor
6 from ..utils import (
7     determine_ext,
8     parse_iso8601,
9     xpath_text,
10 )
11
12
13 class SrfIE(InfoExtractor):
14     _VALID_URL = r'http://www\.srf\.ch/play(?:er)?/tv/[^/]+/video/(?P<display_id>[^?]+)\?id=(?P<id>[0-9a-f\-]{36})'
15     _TESTS = [{
16         'url': 'http://www.srf.ch/play/tv/10vor10/video/snowden-beantragt-asyl-in-russland?id=28e1a57d-5b76-4399-8ab3-9097f071e6c5',
17         'md5': '4cd93523723beff51bb4bee974ee238d',
18         'info_dict': {
19             'id': '28e1a57d-5b76-4399-8ab3-9097f071e6c5',
20             'display_id': 'snowden-beantragt-asyl-in-russland',
21             'ext': 'm4v',
22             'upload_date': '20130701',
23             'title': 'Snowden beantragt Asyl in Russland',
24             'timestamp': 1372713995,
25         }
26     }, {
27         # No Speichern (Save) button
28         'url': 'http://www.srf.ch/play/tv/top-gear/video/jaguar-xk120-shadow-und-tornado-dampflokomotive?id=677f5829-e473-4823-ac83-a1087fe97faa',
29         'info_dict': {
30             'id': '677f5829-e473-4823-ac83-a1087fe97faa',
31             'display_id': 'jaguar-xk120-shadow-und-tornado-dampflokomotive',
32             'ext': 'mp4',
33             'upload_date': '20130710',
34             'title': 'Jaguar XK120, Shadow und Tornado-Dampflokomotive',
35             'timestamp': 1373493600,
36         },
37         'params': {
38             # Require ffmpeg/avconv
39             'skip_download': True,
40         }
41     }, {
42         'url': 'http://www.srf.ch/player/tv/10vor10/video/snowden-beantragt-asyl-in-russland?id=28e1a57d-5b76-4399-8ab3-9097f071e6c5',
43         'only_matching': True,
44     }]
45
46     def _real_extract(self, url):
47         video_id = self._match_id(url)
48
49         video_data = self._download_xml(
50             'http://il.srgssr.ch/integrationlayer/1.0/ue/srf/video/play/%s.xml' % video_id,
51             video_id)
52
53         display_id = re.match(self._VALID_URL, url).group('display_id')
54         title = xpath_text(
55             video_data, './AssetMetadatas/AssetMetadata/title', fatal=True)
56         thumbnails = [{
57             'url': s.text
58         } for s in video_data.findall('.//ImageRepresentation/url')]
59         timestamp = parse_iso8601(xpath_text(video_data, './createdDate'))
60         # The <duration> field in XML is different from the exact duration, skipping
61
62         formats = []
63         for item in video_data.findall('./Playlists/Playlist') + video_data.findall('./Downloads/Download'):
64             url_node = item.find('url')
65             quality = url_node.attrib['quality']
66             full_url = url_node.text
67             original_ext = determine_ext(full_url)
68             if original_ext == 'f4m':
69                 full_url += '?hdcore=3.4.0'  # Without this, you get a 403 error
70             formats.append({
71                 'url': full_url,
72                 'ext': 'mp4' if original_ext == 'm3u8' else original_ext,
73                 'format_id': '%s-%s' % (quality, item.attrib['protocol']),
74                 'preference': 0 if 'HD' in quality else -1,
75             })
76
77         self._sort_formats(formats)
78
79         return {
80             'id': video_id,
81             'display_id': display_id,
82             'formats': formats,
83             'title': title,
84             'thumbnails': thumbnails,
85             'timestamp': timestamp,
86         }