]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/watchbox.py
[ie/box] Fix formats extraction (#8649)
[yt-dlp.git] / yt_dlp / extractor / watchbox.py
1 from .common import InfoExtractor
2 from ..compat import compat_str
3 from ..utils import (
4 int_or_none,
5 js_to_json,
6 strip_or_none,
7 try_get,
8 unescapeHTML,
9 unified_timestamp,
10 )
11
12
13 class WatchBoxIE(InfoExtractor):
14 _VALID_URL = r'https?://(?:www\.)?watchbox\.de/(?P<kind>serien|filme)/(?:[^/]+/)*[^/]+-(?P<id>\d+)'
15 _TESTS = [{
16 # film
17 'url': 'https://www.watchbox.de/filme/free-jimmy-12325.html',
18 'info_dict': {
19 'id': '341368',
20 'ext': 'mp4',
21 'title': 'Free Jimmy',
22 'description': 'md5:bcd8bafbbf9dc0ef98063d344d7cc5f6',
23 'thumbnail': r're:^https?://.*\.jpg$',
24 'duration': 4890,
25 'age_limit': 16,
26 'release_year': 2009,
27 },
28 'params': {
29 'skip_download': True,
30 },
31 'expected_warnings': ['Failed to download m3u8 information'],
32 }, {
33 # episode
34 'url': 'https://www.watchbox.de/serien/ugly-americans-12231/staffel-1/date-in-der-hoelle-328286.html',
35 'info_dict': {
36 'id': '328286',
37 'ext': 'mp4',
38 'title': 'S01 E01 - Date in der Hölle',
39 'description': 'md5:2f31c74a8186899f33cb5114491dae2b',
40 'thumbnail': r're:^https?://.*\.jpg$',
41 'duration': 1291,
42 'age_limit': 12,
43 'release_year': 2010,
44 'series': 'Ugly Americans',
45 'season_number': 1,
46 'episode': 'Date in der Hölle',
47 'episode_number': 1,
48 },
49 'params': {
50 'skip_download': True,
51 },
52 'expected_warnings': ['Failed to download m3u8 information'],
53 }, {
54 'url': 'https://www.watchbox.de/serien/ugly-americans-12231/staffel-2/der-ring-des-powers-328270',
55 'only_matching': True,
56 }]
57
58 def _real_extract(self, url):
59 mobj = self._match_valid_url(url)
60 kind, video_id = mobj.group('kind', 'id')
61
62 webpage = self._download_webpage(url, video_id)
63
64 player_config = self._parse_json(
65 self._search_regex(
66 r'data-player-conf=(["\'])(?P<data>{.+?})\1', webpage,
67 'player config', default='{}', group='data'),
68 video_id, transform_source=unescapeHTML, fatal=False)
69
70 if not player_config:
71 player_config = self._parse_json(
72 self._search_regex(
73 r'playerConf\s*=\s*({.+?})\s*;', webpage, 'player config',
74 default='{}'),
75 video_id, transform_source=js_to_json, fatal=False) or {}
76
77 source = player_config.get('source') or {}
78
79 video_id = compat_str(source.get('videoId') or video_id)
80
81 devapi = self._download_json(
82 'http://api.watchbox.de/devapi/id/%s' % video_id, video_id, query={
83 'format': 'json',
84 'apikey': 'hbbtv',
85 }, fatal=False)
86
87 item = try_get(devapi, lambda x: x['items'][0], dict) or {}
88
89 title = item.get('title') or try_get(
90 item, lambda x: x['movie']['headline_movie'],
91 compat_str) or source['title']
92
93 formats = []
94 hls_url = item.get('media_videourl_hls') or source.get('hls')
95 if hls_url:
96 formats.extend(self._extract_m3u8_formats(
97 hls_url, video_id, 'mp4', entry_protocol='m3u8_native',
98 m3u8_id='hls', fatal=False))
99 dash_url = item.get('media_videourl_wv') or source.get('dash')
100 if dash_url:
101 formats.extend(self._extract_mpd_formats(
102 dash_url, video_id, mpd_id='dash', fatal=False))
103 mp4_url = item.get('media_videourl')
104 if mp4_url:
105 formats.append({
106 'url': mp4_url,
107 'format_id': 'mp4',
108 'width': int_or_none(item.get('width')),
109 'height': int_or_none(item.get('height')),
110 'tbr': int_or_none(item.get('bitrate')),
111 })
112
113 description = strip_or_none(item.get('descr'))
114 thumbnail = item.get('media_content_thumbnail_large') or source.get('poster') or item.get('media_thumbnail')
115 duration = int_or_none(item.get('media_length') or source.get('length'))
116 timestamp = unified_timestamp(item.get('pubDate'))
117 view_count = int_or_none(item.get('media_views'))
118 age_limit = int_or_none(try_get(item, lambda x: x['movie']['fsk']))
119 release_year = int_or_none(try_get(item, lambda x: x['movie']['rel_year']))
120
121 info = {
122 'id': video_id,
123 'title': title,
124 'description': description,
125 'thumbnail': thumbnail,
126 'duration': duration,
127 'timestamp': timestamp,
128 'view_count': view_count,
129 'age_limit': age_limit,
130 'release_year': release_year,
131 'formats': formats,
132 }
133
134 if kind.lower() == 'serien':
135 series = try_get(
136 item, lambda x: x['special']['title'],
137 compat_str) or source.get('format')
138 season_number = int_or_none(self._search_regex(
139 r'^S(\d{1,2})\s*E\d{1,2}', title, 'season number',
140 default=None) or self._search_regex(
141 r'/staffel-(\d+)/', url, 'season number', default=None))
142 episode = source.get('title')
143 episode_number = int_or_none(self._search_regex(
144 r'^S\d{1,2}\s*E(\d{1,2})', title, 'episode number',
145 default=None))
146 info.update({
147 'series': series,
148 'season_number': season_number,
149 'episode': episode,
150 'episode_number': episode_number,
151 })
152
153 return info