[rtp] Add new extractor (Closes #4382)
[youtube-dl] / youtube_dl / extractor / rtp.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5
6 from .common import InfoExtractor
7 from ..utils import js_to_json
8
9
10 class RTPIE(InfoExtractor):
11     _VALID_URL = r'https?://(?:www\.)?rtp\.pt/play/p(?P<program_id>[0-9]+)/e(?P<id>[0-9]+)/?'
12     _TEST = {
13         'url': 'http://www.rtp.pt/play/p405/e174042/paixoes-cruzadas',
14         'info_dict': {
15             'id': '174042',
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         'params': {
22             'skip_download': True,  # RTMP download
23         },
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 = json.loads(js_to_json(player_config))
38
39         path, ext = config.get('file').rsplit('.', 1)
40         formats = [{
41             'app': config.get('application'),
42             'play_path': '{ext:s}:{path:s}'.format(ext=ext, path=path),
43             'page_url': url,
44             'url': 'rtmp://{streamer:s}/{application:s}'.format(**config),
45             'rtmp_live': config.get('live', False),
46             'ext': ext,
47             'vcodec': config.get('type') == 'audio' and 'none' or None,
48             'player_url': 'http://programas.rtp.pt/play/player.swf?v3',
49         }]
50
51         return {
52             'id': video_id,
53             'title': title,
54             'formats': formats,
55             'description': description,
56             'thumbnail': thumbnail,
57         }