aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/rtbf.py
blob: acf10e253c8cedbce6242fdb4ae0eb3f8f7ddd6f (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
# coding: utf-8
from __future__ import unicode_literals

from .common import InfoExtractor
from ..utils import (
    int_or_none,
    unescapeHTML,
)


class RTBFIE(InfoExtractor):
    _VALID_URL = r'https?://www\.rtbf\.be/(?:video/[^?]+\?.*\bid=|ouftivi/(?:[^/]+/)*[^?]+\?.*\bvideoId=)(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.rtbf.be/video/detail_les-diables-au-coeur-episode-2?id=1921274',
        'md5': '799f334ddf2c0a582ba80c44655be570',
        'info_dict': {
            'id': '1921274',
            'ext': 'mp4',
            'title': 'Les Diables au coeur (épisode 2)',
            'duration': 3099,
        }
    }, {
        # geo restricted
        'url': 'http://www.rtbf.be/ouftivi/heros/detail_scooby-doo-mysteres-associes?id=1097&videoId=2057442',
        'only_matching': True,
    }, {
        'url': 'http://www.rtbf.be/ouftivi/niouzz?videoId=2055858',
        'only_matching': True,
    }]

    _QUALITIES = [
        ('mobile', 'mobile'),
        ('web', 'SD'),
        ('url', 'MD'),
        ('high', 'HD'),
    ]

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

        webpage = self._download_webpage(
            'http://www.rtbf.be/video/embed?id=%s' % video_id, video_id)

        data = self._parse_json(
            unescapeHTML(self._search_regex(
                r'data-media="([^"]+)"', webpage, 'data video')),
            video_id)

        if data.get('provider').lower() == 'youtube':
            video_url = data.get('downloadUrl') or data.get('url')
            return self.url_result(video_url, 'Youtube')
        formats = []
        for key, format_id in self._QUALITIES:
            format_url = data['sources'].get(key)
            if format_url:
                formats.append({
                    'format_id': format_id,
                    'url': format_url,
                })

        return {
            'id': video_id,
            'formats': formats,
            'title': data['title'],
            'description': data.get('description') or data.get('subtitle'),
            'thumbnail': data.get('thumbnail'),
            'duration': data.get('duration') or data.get('realDuration'),
            'timestamp': int_or_none(data.get('created')),
            'view_count': int_or_none(data.get('viewCount')),
        }