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