]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/srf.py
Merge pull request #7659 from remitamine/audimedia
[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'https?://(?:www\.srf\.ch/play(?:er)?/tv/[^/]+/video/(?P<display_id>[^?]+)\?id=|tp\.srgssr\.ch/p/flash\?urn=urn:srf:ais:video:)(?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         'md5': 'd97e236e80d1d24729e5d0953d276a4f',
30         'info_dict': {
31             'id': '677f5829-e473-4823-ac83-a1087fe97faa',
32             'display_id': 'jaguar-xk120-shadow-und-tornado-dampflokomotive',
33             'ext': 'flv',
34             'upload_date': '20130710',
35             'title': 'Jaguar XK120, Shadow und Tornado-Dampflokomotive',
36             'timestamp': 1373493600,
37         },
38     }, {
39         'url': 'http://www.srf.ch/player/tv/10vor10/video/snowden-beantragt-asyl-in-russland?id=28e1a57d-5b76-4399-8ab3-9097f071e6c5',
40         'only_matching': True,
41     }, {
42         'url': 'https://tp.srgssr.ch/p/flash?urn=urn:srf:ais:video: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         display_id = re.match(self._VALID_URL, url).group('display_id') or video_id
49
50         video_data = self._download_xml(
51             'http://il.srgssr.ch/integrationlayer/1.0/ue/srf/video/play/%s.xml' % video_id,
52             display_id)
53
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             for url_node in item.findall('url'):
65                 quality = url_node.attrib['quality']
66                 full_url = url_node.text
67                 original_ext = determine_ext(full_url)
68                 format_id = '%s-%s' % (quality, item.attrib['protocol'])
69                 if original_ext == 'f4m':
70                     formats.extend(self._extract_f4m_formats(
71                         full_url + '?hdcore=3.4.0', display_id, f4m_id=format_id))
72                 elif original_ext == 'm3u8':
73                     formats.extend(self._extract_m3u8_formats(
74                         full_url, display_id, 'mp4', m3u8_id=format_id))
75                 else:
76                     formats.append({
77                         'url': full_url,
78                         'ext': original_ext,
79                         'format_id': format_id,
80                         'quality': 0 if 'HD' in quality else -1,
81                         'preference': 1,
82                     })
83
84         self._sort_formats(formats)
85
86         subtitles = {}
87         subtitles_data = video_data.find('Subtitles')
88         if subtitles_data is not None:
89             subtitles_list = [{
90                 'url': sub.text,
91                 'ext': determine_ext(sub.text),
92             } for sub in subtitles_data]
93             if subtitles_list:
94                 subtitles['de'] = subtitles_list
95
96         return {
97             'id': video_id,
98             'display_id': display_id,
99             'formats': formats,
100             'title': title,
101             'thumbnails': thumbnails,
102             'timestamp': timestamp,
103             'subtitles': subtitles,
104         }