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