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