[ie/wistia] Support password-protected videos (#11100)
[yt-dlp3.git] / yt_dlp / extractor / olympics.py
blobbbf83e531a01817e8169890176a9266e766c4b22
1 from .common import InfoExtractor
2 from ..networking.exceptions import HTTPError
3 from ..utils import (
4 ExtractorError,
5 int_or_none,
6 parse_iso8601,
7 parse_qs,
8 try_get,
9 update_url,
10 url_or_none,
12 from ..utils.traversal import traverse_obj
15 class OlympicsReplayIE(InfoExtractor):
16 _VALID_URL = r'https?://(?:www\.)?olympics\.com/[a-z]{2}/(?:paris-2024/)?(?:replay|videos?|original-series/episode)/(?P<id>[\w-]+)'
17 _TESTS = [{
18 'url': 'https://olympics.com/fr/video/men-s-109kg-group-a-weightlifting-tokyo-2020-replays',
19 'info_dict': {
20 'id': 'f6a0753c-8e6f-4b7d-a435-027054a4f8e9',
21 'ext': 'mp4',
22 'title': '+109kg (H) Groupe A - Haltérophilie | Replay de Tokyo 2020',
23 'upload_date': '20210801',
24 'timestamp': 1627797600,
25 'description': 'md5:c66af4a5bc7429dbcc43d15845ff03b3',
26 'thumbnail': 'https://img.olympics.com/images/image/private/t_1-1_1280/primary/nua4o7zwyaznoaejpbk2',
27 'duration': 7017.0,
29 }, {
30 'url': 'https://olympics.com/en/original-series/episode/b-boys-and-b-girls-take-the-spotlight-breaking-life-road-to-paris-2024',
31 'info_dict': {
32 'id': '32633650-c5ee-4280-8b94-fb6defb6a9b5',
33 'ext': 'mp4',
34 'title': 'B-girl Nicka - Breaking Life, Road to Paris 2024 | Episode 1',
35 'upload_date': '20240517',
36 'timestamp': 1715948200,
37 'description': 'md5:f63d728a41270ec628f6ac33ce471bb1',
38 'thumbnail': 'https://img.olympics.com/images/image/private/t_1-1_1280/primary/a3j96l7j6so3vyfijby1',
39 'duration': 1321.0,
41 }, {
42 'url': 'https://olympics.com/en/paris-2024/videos/men-s-preliminaries-gbr-esp-ned-rsa-hockey-olympic-games-paris-2024',
43 'info_dict': {
44 'id': '3d96db23-8eee-4b7c-8ef5-488a0361026c',
45 'ext': 'mp4',
46 'title': 'Men\'s Preliminaries GBR-ESP & NED-RSA | Hockey | Olympic Games Paris 2024',
47 'upload_date': '20240727',
48 'timestamp': 1722066600,
50 'skip': 'Geo-restricted to RU, BR, BT, NP, TM, BD, TL',
51 }, {
52 'url': 'https://olympics.com/en/paris-2024/videos/dnp-suni-lee-i-have-goals-and-i-have-expectations-for-myself-but-i-also-am-trying-to-give-myself-grace',
53 'info_dict': {
54 'id': 'a42f37ab-8a74-41d0-a7d9-af27b7b02a90',
55 'ext': 'mp4',
56 'title': 'md5:c7cfbc9918636a98e66400a812e4d407',
57 'upload_date': '20240729',
58 'timestamp': 1722288600,
61 _GEO_BYPASS = False
63 def _extract_from_nextjs_data(self, webpage, video_id):
64 data = traverse_obj(self._search_nextjs_data(webpage, video_id, default={}), (
65 'props', 'pageProps', 'page', 'items',
66 lambda _, v: v['name'] == 'videoPlaylist', 'data', 'currentVideo', {dict}, any))
67 if not data:
68 return None
70 geo_countries = traverse_obj(data, ('countries', ..., {str}))
71 if traverse_obj(data, ('geoRestrictedVideo', {bool})):
72 self.raise_geo_restricted(countries=geo_countries)
74 is_live = traverse_obj(data, ('streamingStatus', {str})) == 'LIVE'
75 m3u8_url = traverse_obj(data, ('videoUrl', {url_or_none})) or data['streamUrl']
76 tokenized_url = self._tokenize_url(m3u8_url, data['jwtToken'], is_live, video_id)
78 try:
79 formats, subtitles = self._extract_m3u8_formats_and_subtitles(
80 tokenized_url, video_id, 'mp4', m3u8_id='hls')
81 except ExtractorError as e:
82 if isinstance(e.cause, HTTPError) and 'georestricted' in e.cause.msg:
83 self.raise_geo_restricted(countries=geo_countries)
84 raise
86 return {
87 'formats': formats,
88 'subtitles': subtitles,
89 'is_live': is_live,
90 **traverse_obj(data, {
91 'id': ('videoID', {str}),
92 'title': ('title', {str}),
93 'timestamp': ('contentDate', {parse_iso8601}),
94 }),
97 def _tokenize_url(self, url, token, is_live, video_id):
98 return self._download_json(
99 'https://metering.olympics.com/tokengenerator', video_id,
100 'Downloading tokenized m3u8 url', query={
101 **parse_qs(url),
102 'url': update_url(url, query=None),
103 'service-id': 'live' if is_live else 'vod',
104 'user-auth': token,
105 })['data']['url']
107 def _legacy_tokenize_url(self, url, video_id):
108 return self._download_json(
109 'https://olympics.com/tokenGenerator', video_id,
110 'Downloading legacy tokenized m3u8 url', query={'url': url})
112 def _real_extract(self, url):
113 video_id = self._match_id(url)
114 webpage = self._download_webpage(url, video_id)
116 if info := self._extract_from_nextjs_data(webpage, video_id):
117 return info
119 title = self._html_search_meta(('title', 'og:title', 'twitter:title'), webpage)
120 video_uuid = self._html_search_meta('episode_uid', webpage)
121 m3u8_url = self._html_search_meta('video_url', webpage)
122 json_ld = self._search_json_ld(webpage, video_uuid)
123 thumbnails_list = json_ld.get('image')
124 if not thumbnails_list:
125 thumbnails_list = self._html_search_regex(
126 r'["\']image["\']:\s*["\']([^"\']+)["\']', webpage, 'images', default='')
127 thumbnails_list = thumbnails_list.replace('[', '').replace(']', '').split(',')
128 thumbnails_list = [thumbnail.strip() for thumbnail in thumbnails_list]
129 thumbnails = []
130 for thumbnail in thumbnails_list:
131 width_a, height_a, width = self._search_regex(
132 r'/images/image/private/t_(?P<width_a>\d+)-(?P<height_a>\d+)_(?P<width>\d+)/primary/[\W\w\d]+',
133 thumbnail, 'thumb', group=(1, 2, 3), default=(None, None, None))
134 width_a, height_a, width = int_or_none(width_a), int_or_none(height_a), int_or_none(width)
135 thumbnails.append({
136 'url': thumbnail,
137 'width': width,
138 'height': int_or_none(try_get(width, lambda x: x * height_a / width_a)),
141 formats, subtitles = self._extract_m3u8_formats_and_subtitles(
142 self._legacy_tokenize_url(m3u8_url, video_uuid), video_uuid, 'mp4', m3u8_id='hls')
144 return {
145 'id': video_uuid,
146 'title': title,
147 'thumbnails': thumbnails,
148 'formats': formats,
149 'subtitles': subtitles,
150 **json_ld,