e09670da26c92294493a6f864ff33a7a752bfb03
[youtube-dl] / youtube_dl / extractor / rte.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_HTTPError
8 from ..utils import (
9     float_or_none,
10     parse_iso8601,
11     unescapeHTML,
12     ExtractorError,
13 )
14
15
16 class RteBaseIE(InfoExtractor):
17     def _real_extract(self, url):
18         item_id = self._match_id(url)
19
20         try:
21             json_string = self._download_json(
22                 'http://www.rte.ie/rteavgen/getplaylist/?type=web&format=json&id=' + item_id,
23                 item_id)
24         except ExtractorError as ee:
25             if isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 404:
26                 error_info = self._parse_json(ee.cause.read().decode(), item_id, fatal=False)
27                 if error_info:
28                     raise ExtractorError(
29                         '%s said: %s' % (self.IE_NAME, error_info['message']),
30                         expected=True)
31             raise
32
33         # NB the string values in the JSON are stored using XML escaping(!)
34         show = json_string['shows'][0]
35         title = unescapeHTML(show['title'])
36         description = unescapeHTML(show.get('description'))
37         thumbnail = show.get('thumbnail')
38         duration = float_or_none(show.get('duration'), 1000)
39         timestamp = parse_iso8601(show.get('published'))
40
41         mg = show['media:group'][0]
42
43         formats = []
44
45         if mg.get('url'):
46             m = re.match(r'(?P<url>rtmpe?://[^/]+)/(?P<app>.+)/(?P<playpath>mp4:.*)', mg['url'])
47             if m:
48                 m = m.groupdict()
49                 formats.append({
50                     'url': m['url'] + '/' + m['app'],
51                     'app': m['app'],
52                     'play_path': m['playpath'],
53                     'player_url': url,
54                     'ext': 'flv',
55                     'format_id': 'rtmp',
56                 })
57
58         if mg.get('hls_server') and mg.get('hls_url'):
59             formats.extend(self._extract_m3u8_formats(
60                 mg['hls_server'] + mg['hls_url'], item_id, 'mp4',
61                 entry_protocol='m3u8_native', m3u8_id='hls', fatal=False))
62
63         if mg.get('hds_server') and mg.get('hds_url'):
64             formats.extend(self._extract_f4m_formats(
65                 mg['hds_server'] + mg['hds_url'], item_id,
66                 f4m_id='hds', fatal=False))
67
68         self._sort_formats(formats)
69
70         return {
71             'id': item_id,
72             'title': title,
73             'description': description,
74             'thumbnail': thumbnail,
75             'timestamp': timestamp,
76             'duration': duration,
77             'formats': formats,
78         }
79
80
81 class RteIE(RteBaseIE):
82     IE_NAME = 'rte'
83     IE_DESC = 'Raidió Teilifís Éireann TV'
84     _VALID_URL = r'https?://(?:www\.)?rte\.ie/player/[^/]{2,3}/show/[^/]+/(?P<id>[0-9]+)'
85     _TEST = {
86         'url': 'http://www.rte.ie/player/ie/show/iwitness-862/10478715/',
87         'md5': '4a76eb3396d98f697e6e8110563d2604',
88         'info_dict': {
89             'id': '10478715',
90             'ext': 'mp4',
91             'title': 'iWitness',
92             'thumbnail': 're:^https?://.*\.jpg$',
93             'description': 'The spirit of Ireland, one voice and one minute at a time.',
94             'duration': 60.046,
95             'upload_date': '20151012',
96             'timestamp': 1444694160,
97         },
98     }
99
100
101 class RteRadioIE(RteBaseIE):
102     IE_NAME = 'rte:radio'
103     IE_DESC = 'Raidió Teilifís Éireann radio'
104     # Radioplayer URLs have two distinct specifier formats,
105     # the old format #!rii=<channel_id>:<id>:<playable_item_id>:<date>:
106     # the new format #!rii=b<channel_id>_<id>_<playable_item_id>_<date>_
107     # where the IDs are int/empty, the date is DD-MM-YYYY, and the specifier may be truncated.
108     # An <id> uniquely defines an individual recording, and is the only part we require.
109     _VALID_URL = r'https?://(?:www\.)?rte\.ie/radio/utils/radioplayer/rteradioweb\.html#!rii=(?:b?[0-9]*)(?:%3A|:|%5F|_)(?P<id>[0-9]+)'
110
111     _TESTS = [{
112         # Old-style player URL; HLS and RTMPE formats
113         'url': 'http://www.rte.ie/radio/utils/radioplayer/rteradioweb.html#!rii=16:10507902:2414:27-12-2015:',
114         'md5': 'c79ccb2c195998440065456b69760411',
115         'info_dict': {
116             'id': '10507902',
117             'ext': 'mp4',
118             'title': 'Gloria',
119             'thumbnail': 're:^https?://.*\.jpg$',
120             'description': 'md5:9ce124a7fb41559ec68f06387cabddf0',
121             'timestamp': 1451203200,
122             'upload_date': '20151227',
123             'duration': 7230.0,
124         },
125     }, {
126         # New-style player URL; RTMPE formats only
127         'url': 'http://rte.ie/radio/utils/radioplayer/rteradioweb.html#!rii=b16_3250678_8861_06-04-2012_',
128         'info_dict': {
129             'id': '3250678',
130             'ext': 'flv',
131             'title': 'The Lyric Concert with Paul Herriott',
132             'thumbnail': 're:^https?://.*\.jpg$',
133             'description': '',
134             'timestamp': 1333742400,
135             'upload_date': '20120406',
136             'duration': 7199.016,
137         },
138         'params': {
139             # rtmp download
140             'skip_download': True,
141         },
142     }]