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