aboutsummaryrefslogtreecommitdiffstats
path: root/youtube_dl/extractor/tvnow.py
blob: e2169f2bce30a3bc42fcb422bd6978dda0175f3b (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# coding: utf-8
from __future__ import unicode_literals

import re

from .common import InfoExtractor
from ..compat import compat_str
from ..utils import (
    ExtractorError,
    parse_iso8601,
    parse_duration,
    update_url_query,
)


class TVNowBaseIE(InfoExtractor):
    _VIDEO_FIELDS = (
        'id', 'title', 'free', 'geoblocked', 'articleLong', 'articleShort',
        'broadcastStartDate', 'isDrm', 'duration', 'manifest.dashclear',
        'format.defaultImage169Format', 'format.defaultImage169Logo')

    def _call_api(self, path, video_id, query):
        return self._download_json(
            'https://api.tvnow.de/v3/' + path,
            video_id, query=query)

    def _extract_video(self, info, display_id):
        video_id = compat_str(info['id'])
        title = info['title']

        mpd_url = info['manifest']['dashclear']
        if not mpd_url:
            if info.get('isDrm'):
                raise ExtractorError(
                    'Video %s is DRM protected' % video_id, expected=True)
            if info.get('geoblocked'):
                raise ExtractorError(
                    'Video %s is not available from your location due to geo restriction' % video_id,
                    expected=True)
            if not info.get('free', True):
                raise ExtractorError(
                    'Video %s is not available for free' % video_id, expected=True)

        mpd_url = update_url_query(mpd_url, {'filter': ''})
        formats = self._extract_mpd_formats(mpd_url, video_id, mpd_id='dash', fatal=False)
        formats.extend(self._extract_ism_formats(
            mpd_url.replace('dash.', 'hss.').replace('/.mpd', '/Manifest'),
            video_id, ism_id='mss', fatal=False))
        formats.extend(self._extract_m3u8_formats(
            mpd_url.replace('dash.', 'hls.').replace('/.mpd', '/.m3u8'),
            video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
        self._sort_formats(formats)

        description = info.get('articleLong') or info.get('articleShort')
        timestamp = parse_iso8601(info.get('broadcastStartDate'), ' ')
        duration = parse_duration(info.get('duration'))

        f = info.get('format', {})
        thumbnail = f.get('defaultImage169Format') or f.get('defaultImage169Logo')

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


class TVNowIE(TVNowBaseIE):
    _VALID_URL = r'https?://(?:www\.)?tvnow\.(?:de|at|ch)/(?:rtl(?:2|plus)?|nitro|superrtl|ntv|vox)/(?P<show_id>[^/]+)/(?:(?:list/[^/]+|jahr/\d{4}/\d{1,2})/)?(?P<id>[^/]+)/(?:player|preview)'

    _TESTS = [{
        # rtl
        'url': 'https://www.tvnow.de/rtl/alarm-fuer-cobra-11/freier-fall/player?return=/rtl',
        'info_dict': {
            'id': '385314',
            'display_id': 'alarm-fuer-cobra-11/freier-fall',
            'ext': 'mp4',
            'title': 'Freier Fall',
            'description': 'md5:8c2d8f727261adf7e0dc18366124ca02',
            'thumbnail': r're:^https?://.*\.jpg$',
            'timestamp': 1512677700,
            'upload_date': '20171207',
            'duration': 2862.0,
        },
    }, {
        # rtl2
        'url': 'https://www.tvnow.de/rtl2/armes-deutschland/episode-0008/player',
        'only_matching': 'True',
    }, {
        # rtlnitro
        'url': 'https://www.tvnow.de/nitro/alarm-fuer-cobra-11-die-autobahnpolizei/auf-eigene-faust-pilot/player',
        'only_matching': 'True',
    }, {
        # superrtl
        'url': 'https://www.tvnow.de/superrtl/die-lustigsten-schlamassel-der-welt/u-a-ketchup-effekt/player',
        'only_matching': 'True',
    }, {
        # ntv
        'url': 'https://www.tvnow.de/ntv/startup-news/goetter-in-weiss/player',
        'only_matching': 'True',
    }, {
        # vox
        'url': 'https://www.tvnow.de/vox/auto-mobil/neues-vom-automobilmarkt-2017-11-19-17-00-00/player',
        'only_matching': 'True',
    }, {
        # rtlplus
        'url': 'https://www.tvnow.de/rtlplus/op-ruft-dr-bruckner/die-vernaehte-frau/player',
        'only_matching': 'True',
    }]

    def _real_extract(self, url):
        display_id = '%s/%s' % re.match(self._VALID_URL, url).groups()

        info = self._call_api(
            'movies/' + display_id, display_id, query={
                'fields': ','.join(self._VIDEO_FIELDS),
            })

        return self._extract_video(info, display_id)


class TVNowListIE(TVNowBaseIE):
    _VALID_URL = r'(?P<base_url>https?://(?:www\.)?tvnow\.(?:de|at|ch)/(?:rtl(?:2|plus)?|nitro|superrtl|ntv|vox)/(?P<show_id>[^/]+)/)list/(?P<id>[^?/#&]+)$'

    _SHOW_FIELDS = ('title', )
    _SEASON_FIELDS = ('id', 'headline', 'seoheadline', )
    _VIDEO_FIELDS = ('id', 'headline', 'seoUrl', )

    _TESTS = [{
        'url': 'https://www.tvnow.de/rtl/30-minuten-deutschland/list/aktuell',
        'info_dict': {
            'id': '28296',
            'title': '30 Minuten Deutschland - Aktuell',
        },
        'playlist_mincount': 1,
    }]

    def _real_extract(self, url):
        base_url, show_id, season_id = re.match(self._VALID_URL, url).groups()

        fields = []
        fields.extend(self._SHOW_FIELDS)
        fields.extend('formatTabs.%s' % field for field in self._SEASON_FIELDS)
        fields.extend(
            'formatTabs.formatTabPages.container.movies.%s' % field
            for field in self._VIDEO_FIELDS)

        list_info = self._call_api(
            'formats/seo', season_id, query={
                'fields': ','.join(fields),
                'name': show_id + '.php'
            })

        season = next(
            season for season in list_info['formatTabs']['items']
            if season.get('seoheadline') == season_id)

        title = '%s - %s' % (list_info['title'], season['headline'])

        entries = []
        for container in season['formatTabPages']['items']:
            for info in ((container.get('container') or {}).get('movies') or {}).get('items') or []:
                seo_url = info.get('seoUrl')
                if not seo_url:
                    continue
                entries.append(self.url_result(
                    base_url + seo_url + '/player', 'TVNow', info.get('id')))

        return self.playlist_result(
            entries, compat_str(season.get('id') or season_id), title)