[extractor/rte.py] Add support for RTE radio player
[youtube-dl] / youtube_dl / extractor / rte.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5
6 from ..utils import (
7     float_or_none,
8     unescapeHTML,
9 )
10
11
12 class RteIE(InfoExtractor):
13     IE_NAME = 'rte'
14     IE_DESC = 'Raidió Teilifís Éireann TV'
15     _VALID_URL = r'https?://(?:www\.)?rte\.ie/player/[^/]{2,3}/show/[^/]+/(?P<id>[0-9]+)'
16     _TEST = {
17         'url': 'http://www.rte.ie/player/ie/show/iwitness-862/10478715/',
18         'info_dict': {
19             'id': '10478715',
20             'ext': 'flv',
21             'title': 'Watch iWitness  online',
22             'thumbnail': 're:^https?://.*\.jpg$',
23             'description': 'iWitness : The spirit of Ireland, one voice and one minute at a time.',
24             'duration': 60.046,
25         },
26         'params': {
27             'skip_download': 'f4m fails with --test atm'
28         }
29     }
30
31     def _real_extract(self, url):
32         video_id = self._match_id(url)
33         webpage = self._download_webpage(url, video_id)
34
35         title = self._og_search_title(webpage)
36         description = self._html_search_meta('description', webpage, 'description')
37         duration = float_or_none(self._html_search_meta(
38             'duration', webpage, 'duration', fatal=False), 1000)
39
40         thumbnail_id = self._search_regex(
41             r'<meta name="thumbnail" content="uri:irus:(.*?)" />', webpage, 'thumbnail')
42         thumbnail = 'http://img.rasset.ie/' + thumbnail_id + '.jpg'
43
44         feeds_url = self._html_search_meta("feeds-prefix", webpage, 'feeds url') + video_id
45         json_string = self._download_json(feeds_url, video_id)
46
47         # f4m_url = server + relative_url
48         f4m_url = json_string['shows'][0]['media:group'][0]['rte:server'] + json_string['shows'][0]['media:group'][0]['url']
49         f4m_formats = self._extract_f4m_formats(f4m_url, video_id)
50
51         return {
52             'id': video_id,
53             'title': title,
54             'formats': f4m_formats,
55             'description': description,
56             'thumbnail': thumbnail,
57             'duration': duration,
58         }
59
60
61
62 class RteRadioIE(InfoExtractor):
63     IE_NAME = 'rte:radio'
64     IE_DESC = 'Raidió Teilifís Éireann radio'
65     # Radioplayer URLs have the specifier #!rii=<channel_id>:<id>:<playable_item_id>:<date>:
66     # where the IDs are int/empty, the date is DD-MM-YYYY, and the specifier may be truncated.
67     # An <id> uniquely defines an individual recording, and is the only part we require.
68     _VALID_URL = r'https?://(?:www\.)?rte\.ie/radio/utils/radioplayer/rteradioweb\.html#!rii=(?:[0-9]*)(?:%3A|:)(?P<id>[0-9]+)'
69
70     _TEST = {
71         'url': 'http://www.rte.ie/radio/utils/radioplayer/rteradioweb.html#!rii=16:10507902:2414:27-12-2015:',
72         'info_dict': {
73             'id': '10507902',
74             'ext': 'flv',
75             'title': 'Gloria',
76             'thumbnail': 're:^https?://.*\.jpg$',
77             'description': 'Tim Thurston guides you through a millennium of sacred music featuring Gregorian chant, pure solo voices and choral masterpieces, framed around the glorious music of J.S. Bach.',
78             'duration': 7230.0,
79         },
80         'params': {
81             'skip_download': 'f4m fails with --test atm'
82         }
83     }
84
85     def _real_extract(self, url):
86         item_id = self._match_id(url)
87         feeds_url = 'http://www.rte.ie/rteavgen/getplaylist/?type=web&format=json&id=' + item_id
88         json_string = self._download_json(feeds_url, item_id)
89
90         # NB the string values in the JSON are stored using XML escaping(!)
91         show = json_string['shows'][0]
92         title = unescapeHTML(show['title'])
93         description = unescapeHTML(show.get('description'))
94         thumbnail = show.get('thumbnail')
95         duration = float_or_none(show.get('duration'), 1000)
96
97         mg = show['media:group'][0]
98
99         formats = []
100
101         if mg.get('url') and not mg['url'].startswith('rtmpe:'):
102             formats.append({'url': mg.get('url')})
103
104         if mg.get('hls_server') and mg.get('hls_url'):
105             hls_url = mg['hls_server'] +  mg['hls_url']
106             hls_formats = self._extract_m3u8_formats(
107                     hls_url, item_id, 'mp4', m3u8_id='hls', fatal=False)
108             formats.extend(hls_formats)
109
110         if mg.get('hds_server') and mg.get('hds_url'):
111             f4m_url = mg['hds_server'] + mg['hds_url']
112             f4m_formats = self._extract_f4m_formats(
113                     f4m_url, item_id, f4m_id='hds', fatal=False)
114             formats.extend(f4m_formats)
115
116         return {
117             'id': item_id,
118             'title': title,
119             'formats': formats,
120             'description': description,
121             'thumbnail': thumbnail,
122             'duration': duration,
123         }