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