]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/olympics.py
[misc] Add `hatch`, `ruff`, `pre-commit` and improve dev docs (#7409)
[yt-dlp.git] / yt_dlp / extractor / olympics.py
1 from .common import InfoExtractor
2 from ..utils import int_or_none, try_get
3
4
5 class OlympicsReplayIE(InfoExtractor):
6 _VALID_URL = r'https?://(?:www\.)?olympics\.com(?:/tokyo-2020)?/[a-z]{2}/(?:replay|video)/(?P<id>[^/#&?]+)'
7 _TESTS = [{
8 'url': 'https://olympics.com/fr/video/men-s-109kg-group-a-weightlifting-tokyo-2020-replays',
9 'info_dict': {
10 'id': 'f6a0753c-8e6f-4b7d-a435-027054a4f8e9',
11 'ext': 'mp4',
12 'title': '+109kg (H) Groupe A - Haltérophilie | Replay de Tokyo 2020',
13 'upload_date': '20210801',
14 'timestamp': 1627783200,
15 'description': 'md5:c66af4a5bc7429dbcc43d15845ff03b3',
16 'uploader': 'International Olympic Committee',
17 },
18 'params': {
19 'skip_download': True,
20 },
21 }, {
22 'url': 'https://olympics.com/tokyo-2020/en/replay/bd242924-4b22-49a5-a846-f1d4c809250d/mens-bronze-medal-match-hun-esp',
23 'only_matching': True,
24 }]
25
26 def _real_extract(self, url):
27 id = self._match_id(url)
28
29 webpage = self._download_webpage(url, id)
30 title = self._html_search_meta(('title', 'og:title', 'twitter:title'), webpage)
31 uuid = self._html_search_meta('episode_uid', webpage)
32 m3u8_url = self._html_search_meta('video_url', webpage)
33 json_ld = self._search_json_ld(webpage, uuid)
34 thumbnails_list = json_ld.get('image')
35 if not thumbnails_list:
36 thumbnails_list = self._html_search_regex(
37 r'["\']image["\']:\s*["\']([^"\']+)["\']', webpage, 'images', default='')
38 thumbnails_list = thumbnails_list.replace('[', '').replace(']', '').split(',')
39 thumbnails_list = [thumbnail.strip() for thumbnail in thumbnails_list]
40 thumbnails = []
41 for thumbnail in thumbnails_list:
42 width_a, height_a, width = self._search_regex(
43 r'/images/image/private/t_(?P<width_a>\d+)-(?P<height_a>\d+)_(?P<width>\d+)/primary/[\W\w\d]+',
44 thumbnail, 'thumb', group=(1, 2, 3), default=(None, None, None))
45 width_a, height_a, width = int_or_none(width_a), int_or_none(height_a), int_or_none(width)
46 thumbnails.append({
47 'url': thumbnail,
48 'width': width,
49 'height': int_or_none(try_get(width, lambda x: x * height_a / width_a))
50 })
51 m3u8_url = self._download_json(
52 f'https://olympics.com/tokenGenerator?url={m3u8_url}', uuid, note='Downloading m3u8 url')
53 formats, subtitles = self._extract_m3u8_formats_and_subtitles(m3u8_url, uuid, 'mp4', m3u8_id='hls')
54
55 return {
56 'id': uuid,
57 'title': title,
58 'thumbnails': thumbnails,
59 'formats': formats,
60 'subtitles': subtitles,
61 **json_ld
62 }