[youtube] Skip unsupported adaptive stream type (#18804)
[youtube-dl] / youtube_dl / extractor / rtl2.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..aes import aes_cbc_decrypt
8 from ..compat import (
9     compat_b64decode,
10     compat_ord,
11     compat_str,
12 )
13 from ..utils import (
14     bytes_to_intlist,
15     ExtractorError,
16     intlist_to_bytes,
17     int_or_none,
18     strip_or_none,
19 )
20
21
22 class RTL2IE(InfoExtractor):
23     IE_NAME = 'rtl2'
24     _VALID_URL = r'http?://(?:www\.)?rtl2\.de/[^?#]*?/(?P<id>[^?#/]*?)(?:$|/(?:$|[?#]))'
25     _TESTS = [{
26         'url': 'http://www.rtl2.de/sendung/grip-das-motormagazin/folge/folge-203-0',
27         'info_dict': {
28             'id': 'folge-203-0',
29             'ext': 'f4v',
30             'title': 'GRIP sucht den Sommerkönig',
31             'description': 'md5:e3adbb940fd3c6e76fa341b8748b562f'
32         },
33         'params': {
34             # rtmp download
35             'skip_download': True,
36         },
37     }, {
38         'url': 'http://www.rtl2.de/sendung/koeln-50667/video/5512-anna/21040-anna-erwischt-alex/',
39         'info_dict': {
40             'id': '21040-anna-erwischt-alex',
41             'ext': 'mp4',
42             'title': 'Anna erwischt Alex!',
43             'description': 'Anna nimmt ihrem Vater nicht ab, dass er nicht spielt. Und tatsächlich erwischt sie ihn auf frischer Tat.'
44         },
45         'params': {
46             # rtmp download
47             'skip_download': True,
48         },
49     }]
50
51     def _real_extract(self, url):
52         # Some rtl2 urls have no slash at the end, so append it.
53         if not url.endswith('/'):
54             url += '/'
55
56         video_id = self._match_id(url)
57         webpage = self._download_webpage(url, video_id)
58
59         mobj = re.search(
60             r'<div[^>]+data-collection="(?P<vico_id>\d+)"[^>]+data-video="(?P<vivi_id>\d+)"',
61             webpage)
62         if mobj:
63             vico_id = mobj.group('vico_id')
64             vivi_id = mobj.group('vivi_id')
65         else:
66             vico_id = self._html_search_regex(
67                 r'vico_id\s*:\s*([0-9]+)', webpage, 'vico_id')
68             vivi_id = self._html_search_regex(
69                 r'vivi_id\s*:\s*([0-9]+)', webpage, 'vivi_id')
70
71         info = self._download_json(
72             'http://www.rtl2.de/sites/default/modules/rtl2/mediathek/php/get_video_jw.php',
73             video_id, query={
74                 'vico_id': vico_id,
75                 'vivi_id': vivi_id,
76             })
77         video_info = info['video']
78         title = video_info['titel']
79
80         formats = []
81
82         rtmp_url = video_info.get('streamurl')
83         if rtmp_url:
84             rtmp_url = rtmp_url.replace('\\', '')
85             stream_url = 'mp4:' + self._html_search_regex(r'/ondemand/(.+)', rtmp_url, 'stream URL')
86             rtmp_conn = ['S:connect', 'O:1', 'NS:pageUrl:' + url, 'NB:fpad:0', 'NN:videoFunction:1', 'O:0']
87
88             formats.append({
89                 'format_id': 'rtmp',
90                 'url': rtmp_url,
91                 'play_path': stream_url,
92                 'player_url': 'http://www.rtl2.de/flashplayer/vipo_player.swf',
93                 'page_url': url,
94                 'flash_version': 'LNX 11,2,202,429',
95                 'rtmp_conn': rtmp_conn,
96                 'no_resume': True,
97                 'preference': 1,
98             })
99
100         m3u8_url = video_info.get('streamurl_hls')
101         if m3u8_url:
102             formats.extend(self._extract_akamai_formats(m3u8_url, video_id))
103
104         self._sort_formats(formats)
105
106         return {
107             'id': video_id,
108             'title': title,
109             'thumbnail': video_info.get('image'),
110             'description': video_info.get('beschreibung'),
111             'duration': int_or_none(video_info.get('duration')),
112             'formats': formats,
113         }
114
115
116 class RTL2YouBaseIE(InfoExtractor):
117     _BACKWERK_BASE_URL = 'https://p-you-backwerk.rtl2apps.de/'
118
119
120 class RTL2YouIE(RTL2YouBaseIE):
121     IE_NAME = 'rtl2:you'
122     _VALID_URL = r'http?://you\.rtl2\.de/(?:video/\d+/|youplayer/index\.html\?.*?\bvid=)(?P<id>\d+)'
123     _TESTS = [{
124         'url': 'http://you.rtl2.de/video/3002/15740/MJUNIK%20%E2%80%93%20Home%20of%20YOU/307-hirn-wo-bist-du',
125         'info_dict': {
126             'id': '15740',
127             'ext': 'mp4',
128             'title': 'MJUNIK – Home of YOU - #307 Hirn, wo bist du?!',
129             'description': 'md5:ddaa95c61b372b12b66e115b2772fe01',
130             'age_limit': 12,
131         },
132     }, {
133         'url': 'http://you.rtl2.de/youplayer/index.html?vid=15712',
134         'only_matching': True,
135     }]
136     _AES_KEY = b'\xe9W\xe4.<*\xb8\x1a\xd2\xb6\x92\xf3C\xd3\xefL\x1b\x03*\xbbbH\xc0\x03\xffo\xc2\xf2(\xaa\xaa!'
137     _GEO_COUNTRIES = ['DE']
138
139     def _real_extract(self, url):
140         video_id = self._match_id(url)
141
142         stream_data = self._download_json(
143             self._BACKWERK_BASE_URL + 'stream/video/' + video_id, video_id)
144
145         data, iv = compat_b64decode(stream_data['streamUrl']).decode().split(':')
146         stream_url = intlist_to_bytes(aes_cbc_decrypt(
147             bytes_to_intlist(compat_b64decode(data)),
148             bytes_to_intlist(self._AES_KEY),
149             bytes_to_intlist(compat_b64decode(iv))
150         ))
151         if b'rtl2_you_video_not_found' in stream_url:
152             raise ExtractorError('video not found', expected=True)
153
154         formats = self._extract_m3u8_formats(
155             stream_url[:-compat_ord(stream_url[-1])].decode(),
156             video_id, 'mp4', 'm3u8_native')
157         self._sort_formats(formats)
158
159         video_data = self._download_json(
160             self._BACKWERK_BASE_URL + 'video/' + video_id, video_id)
161
162         series = video_data.get('formatTitle')
163         title = episode = video_data.get('title') or series
164         if series and series != title:
165             title = '%s - %s' % (series, title)
166
167         return {
168             'id': video_id,
169             'title': title,
170             'formats': formats,
171             'description': strip_or_none(video_data.get('description')),
172             'thumbnail': video_data.get('image'),
173             'duration': int_or_none(stream_data.get('duration') or video_data.get('duration'), 1000),
174             'series': series,
175             'episode': episode,
176             'age_limit': int_or_none(video_data.get('minimumAge')),
177         }
178
179
180 class RTL2YouSeriesIE(RTL2YouBaseIE):
181     IE_NAME = 'rtl2:you:series'
182     _VALID_URL = r'http?://you\.rtl2\.de/videos/(?P<id>\d+)'
183     _TEST = {
184         'url': 'http://you.rtl2.de/videos/115/dragon-ball',
185         'info_dict': {
186             'id': '115',
187         },
188         'playlist_mincount': 5,
189     }
190
191     def _real_extract(self, url):
192         series_id = self._match_id(url)
193         stream_data = self._download_json(
194             self._BACKWERK_BASE_URL + 'videos',
195             series_id, query={
196                 'formatId': series_id,
197                 'limit': 1000000000,
198             })
199
200         entries = []
201         for video in stream_data.get('videos', []):
202             video_id = compat_str(video['videoId'])
203             if not video_id:
204                 continue
205             entries.append(self.url_result(
206                 'http://you.rtl2.de/video/%s/%s' % (series_id, video_id),
207                 'RTL2You', video_id))
208         return self.playlist_result(entries, series_id)