aboutsummaryrefslogtreecommitdiffstats
path: root/youtube_dl/extractor/rtp.py
blob: 533ee27cbefcb1bb19bc2bedf06163a0c64d74e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# coding: utf-8
from __future__ import unicode_literals

import re

from .common import InfoExtractor


class RTPIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?rtp\.pt/play/p(?P<program_id>[0-9]+)/(?P<id>[^/?#]+)/?'
    _TESTS = [{
        'url': 'http://www.rtp.pt/play/p405/e174042/paixoes-cruzadas',
        'md5': 'e736ce0c665e459ddb818546220b4ef8',
        'info_dict': {
            'id': 'e174042',
            'ext': 'mp3',
            'title': 'Paixões Cruzadas',
            'description': 'As paixões musicais de António Cartaxo e António Macedo',
            'thumbnail': r're:^https?://.*\.jpg',
        },
        'params': {
            # rtmp download
            'skip_download': True,
        },
    }, {
        'url': 'http://www.rtp.pt/play/p831/a-quimica-das-coisas',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)

        webpage = self._download_webpage(url, video_id)
        title = self._html_search_meta(
            'twitter:title', webpage, display_name='title', fatal=True)
        description = self._html_search_meta('description', webpage)
        thumbnail = self._og_search_thumbnail(webpage)

        player_config = self._search_regex(
            r'(?s)RTPPLAY\.player\.newPlayer\(\s*(\{.*?\})\s*\)', webpage, 'player config')
        config = self._parse_json(player_config, video_id)

        path, ext = config.get('file').rsplit('.', 1)
        formats = [{
            'format_id': 'rtmp',
            'ext': ext,
            'vcodec': config.get('type') == 'audio' and 'none' or None,
            'preference': -2,
            'url': 'rtmp://{streamer:s}/{application:s}'.format(**config),
            'app': config.get('application'),
            'play_path': '{ext:s}:{path:s}'.format(ext=ext, path=path),
            'page_url': url,
            'rtmp_live': config.get('live', False),
            'player_url': 'http://programas.rtp.pt/play/player.swf?v3',
            'rtmp_real_time': True,
        }]

        # Construct regular HTTP download URLs
        replacements = {
            'audio': {
                'format_id': 'mp3',
                'pattern': r'^nas2\.share/wavrss/',
                'repl': 'http://rsspod.rtp.pt/podcasts/',
                'vcodec': 'none',
            },
            'video': {
                'format_id': 'mp4_h264',
                'pattern': r'^nas2\.share/h264/',
                'repl': 'http://rsspod.rtp.pt/videocasts/',
                'vcodec': 'h264',
            },
        }
        r = replacements[config['type']]
        if re.match(r['pattern'], config['file']) is not None:
            formats.append({
                'format_id': r['format_id'],
                'url': re.sub(r['pattern'], r['repl'], config['file']),
                'vcodec': r['vcodec'],
            })

        self._sort_formats(formats)

        return {
            'id': video_id,
            'title': title,
            'formats': formats,
            'description': description,
            'thumbnail': thumbnail,
        }