]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/tv4.py
[ie/matchtv] Fix extractor (#10190)
[yt-dlp.git] / yt_dlp / extractor / tv4.py
CommitLineData
6ae36035
RA
1import re
2
50efb383
NJ
3from .common import InfoExtractor
4from ..utils import (
125ffaa1 5 bool_or_none,
9d8985a1 6 int_or_none,
50efb383 7 parse_iso8601,
125ffaa1
T
8 traverse_obj,
9 url_or_none,
50efb383
NJ
10)
11
12
13class 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 (?:
2181983a 20 (?:program|barn)/(?:(?:[^/]+/){1,2}|(?:[^\?]+)\?video_id=)|
50efb383
NJ
21 iframe/video/|
22 film/|
23 sport/|
24 )
25 )(?P<id>[0-9]+)'''
125ffaa1 26 _GEO_BYPASS = False
50efb383
NJ
27 _TESTS = [
28 {
125ffaa1 29 # not geo-restricted
50efb383 30 'url': 'http://www.tv4.se/kalla-fakta/klipp/kalla-fakta-5-english-subtitles-2491650',
1f393a32 31 'md5': 'cb837212f342d77cec06e6dad190e96d',
50efb383
NJ
32 'info_dict': {
33 'id': '2491650',
34 'ext': 'mp4',
35 'title': 'Kalla Fakta 5 (english subtitles)',
125ffaa1
T
36 'description': '2491650',
37 'series': 'Kalla fakta',
38 'duration': 1335,
39 'thumbnail': r're:^https?://[^/?#]+/api/v2/img/',
40 'timestamp': 1385373240,
50efb383
NJ
41 'upload_date': '20131125',
42 },
125ffaa1
T
43 'params': {'skip_download': 'm3u8'},
44 'expected_warnings': ['Unable to download f4m manifest'],
50efb383
NJ
45 },
46 {
47 'url': 'http://www.tv4play.se/iframe/video/3054113',
1f393a32 48 'md5': 'cb837212f342d77cec06e6dad190e96d',
50efb383
NJ
49 'info_dict': {
50 'id': '3054113',
51 'ext': 'mp4',
52 'title': 'Så här jobbar ficktjuvarna - se avslöjande bilder',
ec85ded8 53 'thumbnail': r're:^https?://.*\.jpg$',
50efb383
NJ
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',
57 },
125ffaa1 58 'skip': '404 Not Found',
50efb383
NJ
59 },
60 {
61 'url': 'http://www.tv4play.se/sport/3060959',
62 'only_matching': True,
63 },
64 {
65 'url': 'http://www.tv4play.se/film/2378136',
66 'only_matching': True,
67 },
68 {
69 'url': 'http://www.tv4play.se/barn/looney-tunes?video_id=3062412',
70 'only_matching': True,
71 },
319fc706 72 {
fad9fc53 73 'url': 'http://www.tv4play.se/program/farang/3922081',
319fc706 74 'only_matching': True,
2181983a 75 },
76 {
77 'url': 'https://www.tv4play.se/program/nyheterna/avsnitt/13315940',
78 'only_matching': True,
add96eb9 79 },
50efb383
NJ
80 ]
81
125ffaa1
T
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,
90 })
91
50efb383
NJ
92 def _real_extract(self, url):
93 video_id = self._match_id(url)
94
125ffaa1
T
95 info = traverse_obj(self._call_api('asset', video_id, query={
96 'protocol': 'hls,dash',
97 'drm': 'widevine',
98 }), ('metadata', {dict})) or {}
50efb383 99
125ffaa1
T
100 manifest_url = self._call_api(
101 'play', video_id, headers=self.geo_verification_headers())['playbackItem']['manifestUrl']
50efb383 102
125ffaa1 103 formats, subtitles = [], {}
0c541b56
F
104
105 fmts, subs = self._extract_m3u8_formats_and_subtitles(
6ae36035
RA
106 manifest_url, video_id, 'mp4',
107 'm3u8_native', m3u8_id='hls', fatal=False)
0c541b56
F
108 formats.extend(fmts)
109 subtitles = self._merge_subtitles(subtitles, subs)
110
111 fmts, subs = self._extract_mpd_formats_and_subtitles(
6ae36035 112 manifest_url.replace('.m3u8', '.mpd'),
0c541b56
F
113 video_id, mpd_id='dash', fatal=False)
114 formats.extend(fmts)
115 subtitles = self._merge_subtitles(subtitles, subs)
116
117 fmts = self._extract_f4m_formats(
6ae36035 118 manifest_url.replace('.m3u8', '.f4m'),
0c541b56
F
119 video_id, f4m_id='hds', fatal=False)
120 formats.extend(fmts)
121
122 fmts, subs = self._extract_ism_formats_and_subtitles(
91bd3bd0 123 re.sub(r'\.ism/.*?\.m3u8', r'.ism/Manifest', manifest_url),
0c541b56
F
124 video_id, ism_id='mss', fatal=False)
125 formats.extend(fmts)
126 subtitles = self._merge_subtitles(subtitles, subs)
c58b7ffe
S
127
128 if not formats and info.get('is_geo_restricted'):
125ffaa1
T
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'])
c58b7ffe 132
50efb383
NJ
133 return {
134 'id': video_id,
50efb383 135 'formats': formats,
0c541b56 136 'subtitles': subtitles,
125ffaa1
T
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),
50efb383 149 }