]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/medaltv.py
[ie/matchtv] Fix extractor (#10190)
[yt-dlp.git] / yt_dlp / extractor / medaltv.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 ExtractorError,
6 float_or_none,
7 format_field,
8 int_or_none,
9 str_or_none,
10 traverse_obj,
11 )
12
13
14 class MedalTVIE(InfoExtractor):
15 _VALID_URL = r'https?://(?:www\.)?medal\.tv/games/[^/?#&]+/clips/(?P<id>[^/?#&]+)'
16 _TESTS = [{
17 'url': 'https://medal.tv/games/valorant/clips/jTBFnLKdLy15K',
18 'md5': '03e4911fdcf7fce563090705c2e79267',
19 'info_dict': {
20 'id': 'jTBFnLKdLy15K',
21 'ext': 'mp4',
22 'title': "Mornu's clutch",
23 'description': '',
24 'uploader': 'Aciel',
25 'timestamp': 1651628243,
26 'upload_date': '20220504',
27 'uploader_id': '19335460',
28 'uploader_url': 'https://medal.tv/users/19335460',
29 'comment_count': int,
30 'view_count': int,
31 'like_count': int,
32 'duration': 13,
33 },
34 }, {
35 'url': 'https://medal.tv/games/cod-cold-war/clips/2mA60jWAGQCBH',
36 'md5': 'fc7a3e4552ae8993c1c4006db46be447',
37 'info_dict': {
38 'id': '2mA60jWAGQCBH',
39 'ext': 'mp4',
40 'title': 'Quad Cold',
41 'description': 'Medal,https://medal.tv/desktop/',
42 'uploader': 'MowgliSB',
43 'timestamp': 1603165266,
44 'upload_date': '20201020',
45 'uploader_id': '10619174',
46 'thumbnail': 'https://cdn.medal.tv/10619174/thumbnail-34934644-720p.jpg?t=1080p&c=202042&missing',
47 'uploader_url': 'https://medal.tv/users/10619174',
48 'comment_count': int,
49 'view_count': int,
50 'like_count': int,
51 'duration': 23,
52 },
53 }, {
54 'url': 'https://medal.tv/games/cod-cold-war/clips/2um24TWdty0NA',
55 'md5': 'b6dc76b78195fff0b4f8bf4a33ec2148',
56 'info_dict': {
57 'id': '2um24TWdty0NA',
58 'ext': 'mp4',
59 'title': 'u tk me i tk u bigger',
60 'description': 'Medal,https://medal.tv/desktop/',
61 'uploader': 'Mimicc',
62 'timestamp': 1605580939,
63 'upload_date': '20201117',
64 'uploader_id': '5156321',
65 'thumbnail': 'https://cdn.medal.tv/5156321/thumbnail-36787208-360p.jpg?t=1080p&c=202046&missing',
66 'uploader_url': 'https://medal.tv/users/5156321',
67 'comment_count': int,
68 'view_count': int,
69 'like_count': int,
70 'duration': 9,
71 },
72 }, {
73 'url': 'https://medal.tv/games/valorant/clips/37rMeFpryCC-9',
74 'only_matching': True,
75 }, {
76 'url': 'https://medal.tv/games/valorant/clips/2WRj40tpY_EU9',
77 'only_matching': True,
78 }]
79
80 def _real_extract(self, url):
81 video_id = self._match_id(url)
82
83 webpage = self._download_webpage(url, video_id, query={'mobilebypass': 'true'})
84
85 hydration_data = self._search_json(
86 r'<script[^>]*>[^<]*\bhydrationData\s*=', webpage,
87 'next data', video_id, end_pattern='</script>', fatal=False)
88
89 clip = traverse_obj(hydration_data, ('clips', ...), get_all=False)
90 if not clip:
91 raise ExtractorError(
92 'Could not find video information.', video_id=video_id)
93
94 title = clip['contentTitle']
95
96 source_width = int_or_none(clip.get('sourceWidth'))
97 source_height = int_or_none(clip.get('sourceHeight'))
98
99 aspect_ratio = source_width / source_height if source_width and source_height else 16 / 9
100
101 def add_item(container, item_url, height, id_key='format_id', item_id=None):
102 item_id = item_id or '%dp' % height
103 if item_id not in item_url:
104 return
105 width = int(round(aspect_ratio * height))
106 container.append({
107 'url': item_url,
108 id_key: item_id,
109 'width': width,
110 'height': height,
111 })
112
113 formats = []
114 thumbnails = []
115 for k, v in clip.items():
116 if not (v and isinstance(v, str)):
117 continue
118 mobj = re.match(r'(contentUrl|thumbnail)(?:(\d+)p)?$', k)
119 if not mobj:
120 continue
121 prefix = mobj.group(1)
122 height = int_or_none(mobj.group(2))
123 if prefix == 'contentUrl':
124 add_item(
125 formats, v, height or source_height,
126 item_id=None if height else 'source')
127 elif prefix == 'thumbnail':
128 add_item(thumbnails, v, height, 'id')
129
130 error = clip.get('error')
131 if not formats and error:
132 if error == 404:
133 self.raise_no_formats(
134 'That clip does not exist.',
135 expected=True, video_id=video_id)
136 else:
137 self.raise_no_formats(
138 f'An unknown error occurred ({error}).',
139 video_id=video_id)
140
141 # Necessary because the id of the author is not known in advance.
142 # Won't raise an issue if no profile can be found as this is optional.
143 author = traverse_obj(hydration_data, ('profiles', ...), get_all=False) or {}
144 author_id = str_or_none(author.get('userId'))
145 author_url = format_field(author_id, None, 'https://medal.tv/users/%s')
146
147 return {
148 'id': video_id,
149 'title': title,
150 'formats': formats,
151 'thumbnails': thumbnails,
152 'description': clip.get('contentDescription'),
153 'uploader': author.get('displayName'),
154 'timestamp': float_or_none(clip.get('created'), 1000),
155 'uploader_id': author_id,
156 'uploader_url': author_url,
157 'duration': int_or_none(clip.get('videoLengthSeconds')),
158 'view_count': int_or_none(clip.get('views')),
159 'like_count': int_or_none(clip.get('likes')),
160 'comment_count': int_or_none(clip.get('comments')),
161 }