]>
Commit | Line | Data |
---|---|---|
38d70284 | 1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
4 | import re | |
5 | ||
6 | from .common import InfoExtractor | |
7 | from ..compat import compat_str | |
8 | from ..utils import ( | |
9 | ExtractorError, | |
e0ddbd02 | 10 | format_field, |
38d70284 | 11 | float_or_none, |
12 | int_or_none, | |
13 | str_or_none, | |
14 | try_get, | |
15 | ) | |
16 | ||
17 | ||
18 | class MedalTVIE(InfoExtractor): | |
41d1cca3 | 19 | _VALID_URL = r'https?://(?:www\.)?medal\.tv/clips/(?P<id>[^/?#&]+)' |
38d70284 | 20 | _TESTS = [{ |
41d1cca3 | 21 | 'url': 'https://medal.tv/clips/2mA60jWAGQCBH', |
38d70284 | 22 | 'md5': '7b07b064331b1cf9e8e5c52a06ae68fa', |
23 | 'info_dict': { | |
41d1cca3 | 24 | 'id': '2mA60jWAGQCBH', |
38d70284 | 25 | 'ext': 'mp4', |
26 | 'title': 'Quad Cold', | |
27 | 'description': 'Medal,https://medal.tv/desktop/', | |
28 | 'uploader': 'MowgliSB', | |
29 | 'timestamp': 1603165266, | |
30 | 'upload_date': '20201020', | |
41d1cca3 | 31 | 'uploader_id': '10619174', |
38d70284 | 32 | } |
33 | }, { | |
41d1cca3 | 34 | 'url': 'https://medal.tv/clips/2um24TWdty0NA', |
38d70284 | 35 | 'md5': 'b6dc76b78195fff0b4f8bf4a33ec2148', |
36 | 'info_dict': { | |
41d1cca3 | 37 | 'id': '2um24TWdty0NA', |
38d70284 | 38 | 'ext': 'mp4', |
39 | 'title': 'u tk me i tk u bigger', | |
40 | 'description': 'Medal,https://medal.tv/desktop/', | |
41 | 'uploader': 'Mimicc', | |
42 | 'timestamp': 1605580939, | |
43 | 'upload_date': '20201117', | |
41d1cca3 | 44 | 'uploader_id': '5156321', |
38d70284 | 45 | } |
41d1cca3 | 46 | }, { |
47 | 'url': 'https://medal.tv/clips/37rMeFpryCC-9', | |
48 | 'only_matching': True, | |
49 | }, { | |
50 | 'url': 'https://medal.tv/clips/2WRj40tpY_EU9', | |
51 | 'only_matching': True, | |
38d70284 | 52 | }] |
53 | ||
54 | def _real_extract(self, url): | |
55 | video_id = self._match_id(url) | |
56 | webpage = self._download_webpage(url, video_id) | |
57 | ||
58 | hydration_data = self._parse_json(self._search_regex( | |
59 | r'<script[^>]*>\s*(?:var\s*)?hydrationData\s*=\s*({.+?})\s*</script>', | |
60 | webpage, 'hydration data', default='{}'), video_id) | |
61 | ||
62 | clip = try_get( | |
63 | hydration_data, lambda x: x['clips'][video_id], dict) or {} | |
64 | if not clip: | |
65 | raise ExtractorError( | |
66 | 'Could not find video information.', video_id=video_id) | |
67 | ||
68 | title = clip['contentTitle'] | |
69 | ||
70 | source_width = int_or_none(clip.get('sourceWidth')) | |
71 | source_height = int_or_none(clip.get('sourceHeight')) | |
72 | ||
73 | aspect_ratio = source_width / source_height if source_width and source_height else 16 / 9 | |
74 | ||
75 | def add_item(container, item_url, height, id_key='format_id', item_id=None): | |
76 | item_id = item_id or '%dp' % height | |
77 | if item_id not in item_url: | |
78 | return | |
79 | width = int(round(aspect_ratio * height)) | |
80 | container.append({ | |
81 | 'url': item_url, | |
82 | id_key: item_id, | |
83 | 'width': width, | |
84 | 'height': height | |
85 | }) | |
86 | ||
87 | formats = [] | |
88 | thumbnails = [] | |
89 | for k, v in clip.items(): | |
90 | if not (v and isinstance(v, compat_str)): | |
91 | continue | |
92 | mobj = re.match(r'(contentUrl|thumbnail)(?:(\d+)p)?$', k) | |
93 | if not mobj: | |
94 | continue | |
95 | prefix = mobj.group(1) | |
96 | height = int_or_none(mobj.group(2)) | |
97 | if prefix == 'contentUrl': | |
98 | add_item( | |
99 | formats, v, height or source_height, | |
100 | item_id=None if height else 'source') | |
101 | elif prefix == 'thumbnail': | |
102 | add_item(thumbnails, v, height, 'id') | |
103 | ||
104 | error = clip.get('error') | |
105 | if not formats and error: | |
106 | if error == 404: | |
b7da73eb | 107 | self.raise_no_formats( |
38d70284 | 108 | 'That clip does not exist.', |
109 | expected=True, video_id=video_id) | |
110 | else: | |
b7da73eb | 111 | self.raise_no_formats( |
38d70284 | 112 | 'An unknown error occurred ({0}).'.format(error), |
113 | video_id=video_id) | |
114 | ||
115 | self._sort_formats(formats) | |
116 | ||
117 | # Necessary because the id of the author is not known in advance. | |
118 | # Won't raise an issue if no profile can be found as this is optional. | |
119 | author = try_get( | |
120 | hydration_data, lambda x: list(x['profiles'].values())[0], dict) or {} | |
121 | author_id = str_or_none(author.get('id')) | |
e0ddbd02 | 122 | author_url = format_field(author_id, template='https://medal.tv/users/%s') |
38d70284 | 123 | |
124 | return { | |
125 | 'id': video_id, | |
126 | 'title': title, | |
127 | 'formats': formats, | |
128 | 'thumbnails': thumbnails, | |
129 | 'description': clip.get('contentDescription'), | |
130 | 'uploader': author.get('displayName'), | |
131 | 'timestamp': float_or_none(clip.get('created'), 1000), | |
132 | 'uploader_id': author_id, | |
133 | 'uploader_url': author_url, | |
134 | 'duration': int_or_none(clip.get('videoLengthSeconds')), | |
135 | 'view_count': int_or_none(clip.get('views')), | |
136 | 'like_count': int_or_none(clip.get('likes')), | |
137 | 'comment_count': int_or_none(clip.get('comments')), | |
138 | } |