[srgssr] use SRFIE format ids
[youtube-dl] / youtube_dl / extractor / srgssr.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 (
8     ExtractorError,
9     parse_iso8601,
10     qualities,
11 )
12
13
14 class SRGSSRIE(InfoExtractor):
15     _VALID_URL = r'(?:https?://tp\.srgssr\.ch/p(?:/[^/]+)+\?urn=urn|srgssr):(?P<bu>srf|rts|rsi|rtr|swi):(?:[^:]+:)?(?P<type>video|audio):(?P<id>[0-9a-f\-]{36}|\d+)'
16
17     _ERRORS = {
18         'AGERATING12': 'To protect children under the age of 12, this video is only available between 8 p.m. and 6 a.m.',
19         'AGERATING18': 'To protect children under the age of 18, this video is only available between 11 p.m. and 5 a.m.',
20         # 'ENDDATE': 'For legal reasons, this video was only available for a specified period of time.',
21         'GEOBLOCK': 'For legal reasons, this video is only available in Switzerland.',
22         'LEGAL': 'The video cannot be transmitted for legal reasons.',
23         'STARTDATE': 'This video is not yet available. Please try again later.',
24     }
25
26     def get_media_data(self, bu, media_type, media_id):
27         media_data = self._download_json(
28             'http://il.srgssr.ch/integrationlayer/1.0/ue/%s/%s/play/%s.json' % (bu, media_type, media_id),
29             media_id)[media_type.capitalize()]
30
31         if media_data.get('block') and media_data['block'] in self._ERRORS:
32             raise ExtractorError('%s said: %s' % (
33                 self.IE_NAME, self._ERRORS[media_data['block']]), expected=True)
34
35         return media_data
36
37     def _real_extract(self, url):
38         bu, media_type, media_id = re.match(self._VALID_URL, url).groups()
39
40         if bu == 'rts':
41             return self.url_result('rts:%s' % media_id, 'RTS')
42
43         media_data = self.get_media_data(bu, media_type, media_id)
44
45         metadata = media_data['AssetMetadatas']['AssetMetadata'][0]
46         title = metadata['title']
47         description = metadata.get('description')
48         created_date = media_data.get('createdDate') or metadata.get('createdDate')
49         timestamp = parse_iso8601(created_date)
50
51         thumbnails = [{
52             'id': image.get('id'),
53             'url': image['url'],
54         } for image in media_data.get('Image', {}).get('ImageRepresentations', {}).get('ImageRepresentation', [])]
55
56         preference = qualities(['LQ', 'MQ', 'SD', 'HQ', 'HD'])
57         formats = []
58         for source in media_data.get('Playlists', {}).get('Playlist', []) + media_data.get('Downloads', {}).get('Download', []):
59             protocol = source.get('@protocol')
60             for asset in source['url']:
61                 asset_url = asset['text']
62                 quality = asset['@quality']
63                 format_id = '%s-%s' % (protocol, quality)
64                 if protocol == 'HTTP-HDS':
65                     formats.extend(self._extract_f4m_formats(
66                         asset_url + '?hdcore=3.4.0', media_id,
67                         f4m_id=format_id, fatal=False))
68                 elif protocol == 'HTTP-HLS':
69                     formats.extend(self._extract_m3u8_formats(
70                         asset_url, media_id, 'mp4', 'm3u8_native',
71                         m3u8_id=format_id, fatal=False))
72                 else:
73                     ext = None
74                     if protocol == 'RTMP':
75                         ext = self._search_regex(r'([a-z0-9]+):[^/]+', asset_url, 'ext')
76                     formats.append({
77                         'format_id': format_id,
78                         'url': asset_url,
79                         'preference': preference(quality),
80                         'ext': ext,
81                     })
82         self._sort_formats(formats)
83
84         return {
85             'id': media_id,
86             'title': title,
87             'description': description,
88             'timestamp': timestamp,
89             'thumbnails': thumbnails,
90             'formats': formats,
91         }
92
93
94 class SRGSSRPlayIE(InfoExtractor):
95     IE_DESC = 'srf.ch, rts.ch, rsi.ch, rtr.ch and swissinfo.ch play sites'
96     _VALID_URL = r'https?://(?:(?:www|play)\.)?(?P<bu>srf|rts|rsi|rtr|swissinfo)\.ch/play/(?:tv|radio)/[^/]+/(?P<type>video|audio)/[^?]+\?id=(?P<id>[0-9a-f\-]{36}|\d+)'
97
98     _TESTS = [{
99         'url': 'http://www.srf.ch/play/tv/10vor10/video/snowden-beantragt-asyl-in-russland?id=28e1a57d-5b76-4399-8ab3-9097f071e6c5',
100         'md5': '4cd93523723beff51bb4bee974ee238d',
101         'info_dict': {
102             'id': '28e1a57d-5b76-4399-8ab3-9097f071e6c5',
103             'ext': 'm4v',
104             'upload_date': '20130701',
105             'title': 'Snowden beantragt Asyl in Russland',
106             'timestamp': 1372713995,
107         }
108     }, {
109         # No Speichern (Save) button
110         'url': 'http://www.srf.ch/play/tv/top-gear/video/jaguar-xk120-shadow-und-tornado-dampflokomotive?id=677f5829-e473-4823-ac83-a1087fe97faa',
111         'md5': '0a274ce38fda48c53c01890651985bc6',
112         'info_dict': {
113             'id': '677f5829-e473-4823-ac83-a1087fe97faa',
114             'ext': 'flv',
115             'upload_date': '20130710',
116             'title': 'Jaguar XK120, Shadow und Tornado-Dampflokomotive',
117             'description': 'md5:88604432b60d5a38787f152dec89cd56',
118             'timestamp': 1373493600,
119         },
120     }, {
121         'url': 'http://www.rtr.ch/play/radio/actualitad/audio/saira-tujetsch-tuttina-cuntinuar-cun-sedrun-muster-turissem?id=63cb0778-27f8-49af-9284-8c7a8c6d15fc',
122         'info_dict': {
123             'id': '63cb0778-27f8-49af-9284-8c7a8c6d15fc',
124             'ext': 'mp3',
125             'upload_date': '20151013',
126             'title': 'Saira: Tujetsch - tuttina cuntinuar cun Sedrun Mustér Turissem',
127             'timestamp': 1444750398,
128         },
129         'params': {
130             # rtmp download
131             'skip_download': True,
132         },
133     }, {
134         'url': 'http://www.rts.ch/play/tv/-/video/le-19h30?id=6348260',
135         'md5': '67a2a9ae4e8e62a68d0e9820cc9782df',
136         'info_dict': {
137             'id': '6348260',
138             'display_id': '6348260',
139             'ext': 'mp4',
140             'duration': 1796,
141             'title': 'Le 19h30',
142             'description': '',
143             'uploader': '19h30',
144             'upload_date': '20141201',
145             'timestamp': 1417458600,
146             'thumbnail': 're:^https?://.*\.image',
147             'view_count': int,
148         },
149         'params': {
150             # m3u8 download
151             'skip_download': True,
152         }
153     }]
154
155     def _real_extract(self, url):
156         bu, media_type, media_id = re.match(self._VALID_URL, url).groups()
157         # other info can be extracted from url + '&layout=json'
158         return self.url_result('srgssr:%s:%s:%s' % (bu[:3], media_type, media_id), 'SRGSSR')