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