[cleanup] Misc (#8968)
[yt-dlp.git] / yt_dlp / extractor / tv4.py
blob10a2fe6e27b050b7ac419e2ab91a53a3335e1bbb
1 import re
3 from .common import InfoExtractor
4 from ..utils import (
5 bool_or_none,
6 int_or_none,
7 parse_iso8601,
8 traverse_obj,
9 url_or_none,
13 class TV4IE(InfoExtractor):
14 IE_DESC = 'tv4.se and tv4play.se'
15 _VALID_URL = r'''(?x)https?://(?:www\.)?
16 (?:
17 tv4\.se/(?:[^/]+)/klipp/(?:.*)-|
18 tv4play\.se/
19 (?:
20 (?:program|barn)/(?:(?:[^/]+/){1,2}|(?:[^\?]+)\?video_id=)|
21 iframe/video/|
22 film/|
23 sport/|
25 )(?P<id>[0-9]+)'''
26 _GEO_BYPASS = False
27 _TESTS = [
29 # not geo-restricted
30 'url': 'http://www.tv4.se/kalla-fakta/klipp/kalla-fakta-5-english-subtitles-2491650',
31 'md5': 'cb837212f342d77cec06e6dad190e96d',
32 'info_dict': {
33 'id': '2491650',
34 'ext': 'mp4',
35 'title': 'Kalla Fakta 5 (english subtitles)',
36 'description': '2491650',
37 'series': 'Kalla fakta',
38 'duration': 1335,
39 'thumbnail': r're:^https?://[^/?#]+/api/v2/img/',
40 'timestamp': 1385373240,
41 'upload_date': '20131125',
43 'params': {'skip_download': 'm3u8'},
44 'expected_warnings': ['Unable to download f4m manifest'],
47 'url': 'http://www.tv4play.se/iframe/video/3054113',
48 'md5': 'cb837212f342d77cec06e6dad190e96d',
49 'info_dict': {
50 'id': '3054113',
51 'ext': 'mp4',
52 'title': 'Så här jobbar ficktjuvarna - se avslöjande bilder',
53 'thumbnail': r're:^https?://.*\.jpg$',
54 'description': 'Unika bilder avslöjar hur turisternas fickor vittjas mitt på Stockholms central. Två experter på ficktjuvarna avslöjar knepen du ska se upp för.',
55 'timestamp': int,
56 'upload_date': '20150130',
58 'skip': '404 Not Found',
61 'url': 'http://www.tv4play.se/sport/3060959',
62 'only_matching': True,
65 'url': 'http://www.tv4play.se/film/2378136',
66 'only_matching': True,
69 'url': 'http://www.tv4play.se/barn/looney-tunes?video_id=3062412',
70 'only_matching': True,
73 'url': 'http://www.tv4play.se/program/farang/3922081',
74 'only_matching': True,
77 'url': 'https://www.tv4play.se/program/nyheterna/avsnitt/13315940',
78 'only_matching': True,
82 def _call_api(self, endpoint, video_id, headers=None, query={}):
83 return self._download_json(
84 f'https://playback2.a2d.tv/{endpoint}/{video_id}', video_id,
85 f'Downloading {endpoint} API JSON', headers=headers, query={
86 'service': 'tv4',
87 'device': 'browser',
88 'protocol': 'hls',
89 **query,
92 def _real_extract(self, url):
93 video_id = self._match_id(url)
95 info = traverse_obj(self._call_api('asset', video_id, query={
96 'protocol': 'hls,dash',
97 'drm': 'widevine',
98 }), ('metadata', {dict})) or {}
100 manifest_url = self._call_api(
101 'play', video_id, headers=self.geo_verification_headers())['playbackItem']['manifestUrl']
103 formats, subtitles = [], {}
105 fmts, subs = self._extract_m3u8_formats_and_subtitles(
106 manifest_url, video_id, 'mp4',
107 'm3u8_native', m3u8_id='hls', fatal=False)
108 formats.extend(fmts)
109 subtitles = self._merge_subtitles(subtitles, subs)
111 fmts, subs = self._extract_mpd_formats_and_subtitles(
112 manifest_url.replace('.m3u8', '.mpd'),
113 video_id, mpd_id='dash', fatal=False)
114 formats.extend(fmts)
115 subtitles = self._merge_subtitles(subtitles, subs)
117 fmts = self._extract_f4m_formats(
118 manifest_url.replace('.m3u8', '.f4m'),
119 video_id, f4m_id='hds', fatal=False)
120 formats.extend(fmts)
122 fmts, subs = self._extract_ism_formats_and_subtitles(
123 re.sub(r'\.ism/.*?\.m3u8', r'.ism/Manifest', manifest_url),
124 video_id, ism_id='mss', fatal=False)
125 formats.extend(fmts)
126 subtitles = self._merge_subtitles(subtitles, subs)
128 if not formats and info.get('is_geo_restricted'):
129 self.raise_geo_restricted(
130 'This video is not available from your location due to geo-restriction, or not being authenticated',
131 countries=['SE'])
133 return {
134 'id': video_id,
135 'formats': formats,
136 'subtitles': subtitles,
137 **traverse_obj(info, {
138 'title': ('title', {str}),
139 'description': ('description', {str}),
140 'timestamp': (('broadcast_date_time', 'broadcastDateTime'), {parse_iso8601}),
141 'duration': ('duration', {int_or_none}),
142 'thumbnail': ('image', {url_or_none}),
143 'is_live': ('isLive', {bool_or_none}),
144 'series': ('seriesTitle', {str}),
145 'season_number': ('seasonNumber', {int_or_none}),
146 'episode': ('episodeTitle', {str}),
147 'episode_number': ('episodeNumber', {int_or_none}),
148 }, get_all=False),