]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/medaltv.py
[ie/youtube] Suppress "Unavailable videos are hidden" warning (#10159)
[yt-dlp.git] / yt_dlp / extractor / medaltv.py
CommitLineData
38d70284 1import re
2
3from .common import InfoExtractor
38d70284 4from ..utils import (
5 ExtractorError,
6 float_or_none,
e897bd82 7 format_field,
38d70284 8 int_or_none,
9 str_or_none,
02e343f6 10 traverse_obj,
38d70284 11)
12
13
14class MedalTVIE(InfoExtractor):
1e3c2b6e 15 _VALID_URL = r'https?://(?:www\.)?medal\.tv/games/[^/?#&]+/clips/(?P<id>[^/?#&]+)'
38d70284 16 _TESTS = [{
07275b70 17 'url': 'https://medal.tv/games/valorant/clips/jTBFnLKdLy15K',
02e343f6 18 'md5': '03e4911fdcf7fce563090705c2e79267',
07275b70
JL
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,
add96eb9 33 },
07275b70 34 }, {
02e343f6
DH
35 'url': 'https://medal.tv/games/cod-cold-war/clips/2mA60jWAGQCBH',
36 'md5': 'fc7a3e4552ae8993c1c4006db46be447',
38d70284 37 'info_dict': {
41d1cca3 38 'id': '2mA60jWAGQCBH',
38d70284 39 'ext': 'mp4',
40 'title': 'Quad Cold',
41 'description': 'Medal,https://medal.tv/desktop/',
42 'uploader': 'MowgliSB',
43 'timestamp': 1603165266,
44 'upload_date': '20201020',
41d1cca3 45 'uploader_id': '10619174',
07275b70
JL
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,
add96eb9 52 },
38d70284 53 }, {
02e343f6 54 'url': 'https://medal.tv/games/cod-cold-war/clips/2um24TWdty0NA',
38d70284 55 'md5': 'b6dc76b78195fff0b4f8bf4a33ec2148',
56 'info_dict': {
41d1cca3 57 'id': '2um24TWdty0NA',
38d70284 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',
41d1cca3 64 'uploader_id': '5156321',
07275b70
JL
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,
add96eb9 71 },
41d1cca3 72 }, {
07275b70 73 'url': 'https://medal.tv/games/valorant/clips/37rMeFpryCC-9',
41d1cca3 74 'only_matching': True,
75 }, {
07275b70 76 'url': 'https://medal.tv/games/valorant/clips/2WRj40tpY_EU9',
41d1cca3 77 'only_matching': True,
38d70284 78 }]
79
80 def _real_extract(self, url):
81 video_id = self._match_id(url)
07275b70 82
615a8444 83 webpage = self._download_webpage(url, video_id, query={'mobilebypass': 'true'})
38d70284 84
1e3c2b6e
JL
85 hydration_data = self._search_json(
86 r'<script[^>]*>[^<]*\bhydrationData\s*=', webpage,
07275b70
JL
87 'next data', video_id, end_pattern='</script>', fatal=False)
88
1e3c2b6e 89 clip = traverse_obj(hydration_data, ('clips', ...), get_all=False)
38d70284 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,
add96eb9 110 'height': height,
38d70284 111 })
112
113 formats = []
114 thumbnails = []
115 for k, v in clip.items():
add96eb9 116 if not (v and isinstance(v, str)):
38d70284 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:
b7da73eb 133 self.raise_no_formats(
38d70284 134 'That clip does not exist.',
135 expected=True, video_id=video_id)
136 else:
b7da73eb 137 self.raise_no_formats(
add96eb9 138 f'An unknown error occurred ({error}).',
38d70284 139 video_id=video_id)
38d70284 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.
1e3c2b6e 143 author = traverse_obj(hydration_data, ('profiles', ...), get_all=False) or {}
07275b70 144 author_id = str_or_none(author.get('userId'))
a70635b8 145 author_url = format_field(author_id, None, 'https://medal.tv/users/%s')
38d70284 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 }