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