[rtp] Construct regular HTTP download URLs (#4882)
[youtube-dl] / youtube_dl / extractor / rtp.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7
8
9 class RTPIE(InfoExtractor):
10     _VALID_URL = r'https?://(?:www\.)?rtp\.pt/play/p(?P<program_id>[0-9]+)/(?P<id>[^/?#]+)/?'
11     _TESTS = [{
12         'url': 'http://www.rtp.pt/play/p405/e174042/paixoes-cruzadas',
13         'md5': 'e736ce0c665e459ddb818546220b4ef8',
14         'info_dict': {
15             'id': 'e174042',
16             'ext': 'mp3',
17             'title': 'Paixões Cruzadas',
18             'description': 'As paixões musicais de António Cartaxo e António Macedo',
19             'thumbnail': 're:^https?://.*\.jpg',
20         },
21     }, {
22         'url': 'http://www.rtp.pt/play/p831/a-quimica-das-coisas',
23         'only_matching': True,
24     }]
25
26     def _real_extract(self, url):
27         video_id = self._match_id(url)
28
29         webpage = self._download_webpage(url, video_id)
30         title = self._html_search_meta(
31             'twitter:title', webpage, display_name='title', fatal=True)
32         description = self._html_search_meta('description', webpage)
33         thumbnail = self._og_search_thumbnail(webpage)
34
35         player_config = self._search_regex(
36             r'(?s)RTPPLAY\.player\.newPlayer\(\s*(\{.*?\})\s*\)', webpage, 'player config')
37         config = self._parse_json(player_config, video_id)
38
39         path, ext = config.get('file').rsplit('.', 1)
40         formats = [{
41             'format_id': 'rtmp',
42             'ext': ext,
43             'vcodec': config.get('type') == 'audio' and 'none' or None,
44             'preference': -2,
45             'url': 'rtmp://{streamer:s}/{application:s}'.format(**config),
46             'app': config.get('application'),
47             'play_path': '{ext:s}:{path:s}'.format(ext=ext, path=path),
48             'page_url': url,
49             'rtmp_live': config.get('live', False),
50             'player_url': 'http://programas.rtp.pt/play/player.swf?v3',
51             'rtmp_real_time': True,
52         }]
53
54         # Construct regular HTTP download URLs
55         replacements = {
56             'audio': {
57                 'format_id': 'mp3',
58                 'pattern': r'^nas2\.share/wavrss/',
59                 'repl': 'http://rsspod.rtp.pt/podcasts/',
60                 'vcodec': 'none',
61             },
62             'video': {
63                 'format_id': 'mp4_h264',
64                 'pattern': r'^nas2\.share/h264/',
65                 'repl': 'http://rsspod.rtp.pt/videocasts/',
66                 'vcodec': 'h264',
67             },
68         }
69         r = replacements[config['type']]
70         if re.match(r['pattern'], config['file']) is not None:
71             formats.append({
72                 'format_id': r['format_id'],
73                 'url': re.sub(r['pattern'], r['repl'], config['file']),
74                 'vcodec': r['vcodec'],
75             })
76
77         self._sort_formats(formats)
78
79         return {
80             'id': video_id,
81             'title': title,
82             'formats': formats,
83             'description': description,
84             'thumbnail': thumbnail,
85         }