]>
Commit | Line | Data |
---|---|---|
f2908d07 S |
1 | import itertools |
2 | import json | |
69bb54eb | 3 | import re |
6c5211ce | 4 | import urllib.parse |
69bb54eb S |
5 | |
6 | from .common import InfoExtractor | |
b081350b S |
7 | from ..utils import ( |
8 | int_or_none, | |
f2908d07 | 9 | parse_duration, |
b081350b | 10 | remove_end, |
f2908d07 | 11 | try_get, |
8abd647c | 12 | urljoin, |
b081350b | 13 | ) |
69bb54eb S |
14 | |
15 | ||
16 | class MailRuIE(InfoExtractor): | |
17 | IE_NAME = 'mailru' | |
18 | IE_DESC = 'Видео@Mail.Ru' | |
549bb416 S |
19 | _VALID_URL = r'''(?x) |
20 | https?:// | |
d53fec69 | 21 | (?:(?:www|m|videoapi)\.)?my\.mail\.ru/+ |
549bb416 S |
22 | (?: |
23 | video/.*\#video=/?(?P<idv1>(?:[^/]+/){3}\d+)| | |
d53fec69 | 24 | (?:videos/embed/)?(?:(?P<idv2prefix>(?:[^/]+/+){2})(?:video/(?:embed/)?)?(?P<idv2suffix>[^/]+/\d+))(?:\.html)?| |
549bb416 S |
25 | (?:video/embed|\+/video/meta)/(?P<metaid>\d+) |
26 | ) | |
27 | ''' | |
ceb7a17f S |
28 | _TESTS = [ |
29 | { | |
30 | 'url': 'http://my.mail.ru/video/top#video=/mail/sonypicturesrus/75/76', | |
31 | 'md5': 'dea205f03120046894db4ebb6159879a', | |
32 | 'info_dict': { | |
00d9ef0b | 33 | 'id': '46301138_76', |
ceb7a17f S |
34 | 'ext': 'mp4', |
35 | 'title': 'Новый Человек-Паук. Высокое напряжение. Восстание Электро', | |
549bb416 | 36 | 'timestamp': 1393235077, |
ceb7a17f S |
37 | 'upload_date': '20140224', |
38 | 'uploader': 'sonypicturesrus', | |
39 | 'uploader_id': 'sonypicturesrus@mail.ru', | |
40 | 'duration': 184, | |
41 | }, | |
95e431e9 | 42 | 'skip': 'Not accessible from Travis CI server', |
ceb7a17f S |
43 | }, |
44 | { | |
45 | 'url': 'http://my.mail.ru/corp/hitech/video/news_hi-tech_mail_ru/1263.html', | |
46 | 'md5': '00a91a58c3402204dcced523777b475f', | |
47 | 'info_dict': { | |
00d9ef0b | 48 | 'id': '46843144_1263', |
ceb7a17f S |
49 | 'ext': 'mp4', |
50 | 'title': 'Samsung Galaxy S5 Hammer Smash Fail Battery Explosion', | |
3967a761 S |
51 | 'timestamp': 1397039888, |
52 | 'upload_date': '20140409', | |
549bb416 | 53 | 'uploader': 'hitech', |
ceb7a17f S |
54 | 'uploader_id': 'hitech@corp.mail.ru', |
55 | 'duration': 245, | |
56 | }, | |
95e431e9 | 57 | 'skip': 'Not accessible from Travis CI server', |
ceb7a17f | 58 | }, |
16f1430b S |
59 | { |
60 | # only available via metaUrl API | |
61 | 'url': 'http://my.mail.ru/mail/720pizle/video/_myvideo/502.html', | |
62 | 'md5': '3b26d2491c6949d031a32b96bd97c096', | |
63 | 'info_dict': { | |
64 | 'id': '56664382_502', | |
65 | 'ext': 'mp4', | |
66 | 'title': ':8336', | |
67 | 'timestamp': 1449094163, | |
68 | 'upload_date': '20151202', | |
69 | 'uploader': '720pizle@mail.ru', | |
70 | 'uploader_id': '720pizle@mail.ru', | |
71 | 'duration': 6001, | |
72 | }, | |
73 | 'skip': 'Not accessible from Travis CI server', | |
b5a5bbf3 S |
74 | }, |
75 | { | |
76 | 'url': 'http://m.my.mail.ru/mail/3sktvtr/video/_myvideo/138.html', | |
77 | 'only_matching': True, | |
25475dfa HD |
78 | }, |
79 | { | |
80 | 'url': 'https://my.mail.ru/video/embed/7949340477499637815', | |
81 | 'only_matching': True, | |
549bb416 S |
82 | }, |
83 | { | |
84 | 'url': 'http://my.mail.ru/+/video/meta/7949340477499637815', | |
85 | 'only_matching': True, | |
278be57b S |
86 | }, |
87 | { | |
88 | 'url': 'https://my.mail.ru//list/sinyutin10/video/_myvideo/4.html', | |
89 | 'only_matching': True, | |
90 | }, | |
91 | { | |
92 | 'url': 'https://my.mail.ru//list//sinyutin10/video/_myvideo/4.html', | |
93 | 'only_matching': True, | |
8abd647c | 94 | }, |
95 | { | |
96 | 'url': 'https://my.mail.ru/mail/cloud-strife/video/embed/Games/2009', | |
97 | 'only_matching': True, | |
98 | }, | |
99 | { | |
100 | 'url': 'https://videoapi.my.mail.ru/videos/embed/mail/cloud-strife/Games/2009.html', | |
101 | 'only_matching': True, | |
add96eb9 | 102 | }, |
ceb7a17f | 103 | ] |
69bb54eb S |
104 | |
105 | def _real_extract(self, url): | |
5ad28e7f | 106 | mobj = self._match_valid_url(url) |
549bb416 S |
107 | meta_id = mobj.group('metaid') |
108 | ||
25475dfa | 109 | video_id = None |
549bb416 | 110 | if meta_id: |
add96eb9 | 111 | meta_url = f'https://my.mail.ru/+/video/meta/{meta_id}' |
549bb416 | 112 | else: |
25475dfa HD |
113 | video_id = mobj.group('idv1') |
114 | if not video_id: | |
115 | video_id = mobj.group('idv2prefix') + mobj.group('idv2suffix') | |
25475dfa | 116 | webpage = self._download_webpage(url, video_id) |
d53fec69 | 117 | page_config = self._parse_json(self._search_regex([ |
25475dfa | 118 | r'(?s)<script[^>]+class="sp-video__page-config"[^>]*>(.+?)</script>', |
8abd647c | 119 | r'(?s)"video":\s*({.+?}),'], |
25475dfa | 120 | webpage, 'page config', default='{}'), video_id, fatal=False) |
25475dfa | 121 | if page_config: |
d53fec69 | 122 | meta_url = page_config.get('metaUrl') or page_config.get('video', {}).get('metaUrl') or page_config.get('metadataUrl') |
549bb416 S |
123 | else: |
124 | meta_url = None | |
125 | ||
126 | video_data = None | |
d53fec69 | 127 | |
128 | # fix meta_url if missing the host address | |
129 | if re.match(r'^\/\+\/', meta_url): | |
8abd647c | 130 | meta_url = urljoin('https://my.mail.ru', meta_url) |
d53fec69 | 131 | |
549bb416 S |
132 | if meta_url: |
133 | video_data = self._download_json( | |
134 | meta_url, video_id or meta_id, 'Downloading video meta JSON', | |
135 | fatal=not video_id) | |
16f1430b S |
136 | |
137 | # Fallback old approach | |
138 | if not video_data: | |
139 | video_data = self._download_json( | |
add96eb9 | 140 | f'http://api.video.mail.ru/videos/{video_id}.json?new=1', |
16f1430b | 141 | video_id, 'Downloading video JSON') |
69bb54eb | 142 | |
adc13b07 | 143 | video_key = self._get_cookies('https://my.mail.ru').get('video_key') |
adc13b07 | 144 | |
b081350b S |
145 | formats = [] |
146 | for f in video_data['videos']: | |
147 | video_url = f.get('url') | |
148 | if not video_url: | |
149 | continue | |
6c5211ce | 150 | if video_key: |
151 | self._set_cookie(urllib.parse.urlparse(video_url).hostname, 'video_key', video_key.value) | |
b081350b S |
152 | format_id = f.get('key') |
153 | height = int_or_none(self._search_regex( | |
154 | r'^(\d+)[pP]$', format_id, 'height', default=None)) if format_id else None | |
155 | formats.append({ | |
156 | 'url': video_url, | |
157 | 'format_id': format_id, | |
158 | 'height': height, | |
159 | }) | |
69bb54eb | 160 | |
00d9ef0b | 161 | meta_data = video_data['meta'] |
b081350b S |
162 | title = remove_end(meta_data['title'], '.mp4') |
163 | ||
164 | author = video_data.get('author') | |
165 | uploader = author.get('name') | |
166 | uploader_id = author.get('id') or author.get('email') | |
167 | view_count = int_or_none(video_data.get('viewsCount') or video_data.get('views_count')) | |
168 | ||
169 | acc_id = meta_data.get('accId') | |
170 | item_id = meta_data.get('itemId') | |
add96eb9 | 171 | content_id = f'{acc_id}_{item_id}' if acc_id and item_id else video_id |
b081350b S |
172 | |
173 | thumbnail = meta_data.get('poster') | |
174 | duration = int_or_none(meta_data.get('duration')) | |
175 | timestamp = int_or_none(meta_data.get('timestamp')) | |
69bb54eb S |
176 | |
177 | return { | |
178 | 'id': content_id, | |
179 | 'title': title, | |
180 | 'thumbnail': thumbnail, | |
00d9ef0b | 181 | 'timestamp': timestamp, |
69bb54eb S |
182 | 'uploader': uploader, |
183 | 'uploader_id': uploader_id, | |
184 | 'duration': duration, | |
185 | 'view_count': view_count, | |
186 | 'formats': formats, | |
00d9ef0b | 187 | } |
f2908d07 S |
188 | |
189 | ||
190 | class MailRuMusicSearchBaseIE(InfoExtractor): | |
191 | def _search(self, query, url, audio_id, limit=100, offset=0): | |
192 | search = self._download_json( | |
193 | 'https://my.mail.ru/cgi-bin/my/ajax', audio_id, | |
add96eb9 | 194 | f'Downloading songs JSON page {offset // limit + 1}', |
f2908d07 S |
195 | headers={ |
196 | 'Referer': url, | |
197 | 'X-Requested-With': 'XMLHttpRequest', | |
198 | }, query={ | |
199 | 'xemail': '', | |
200 | 'ajax_call': '1', | |
201 | 'func_name': 'music.search', | |
202 | 'mna': '', | |
203 | 'mnb': '', | |
204 | 'arg_query': query, | |
205 | 'arg_extended': '1', | |
206 | 'arg_search_params': json.dumps({ | |
207 | 'music': { | |
208 | 'limit': limit, | |
209 | 'offset': offset, | |
210 | }, | |
211 | }), | |
212 | 'arg_limit': limit, | |
213 | 'arg_offset': offset, | |
214 | }) | |
215 | return next(e for e in search if isinstance(e, dict)) | |
216 | ||
217 | @staticmethod | |
218 | def _extract_track(t, fatal=True): | |
219 | audio_url = t['URL'] if fatal else t.get('URL') | |
220 | if not audio_url: | |
221 | return | |
222 | ||
223 | audio_id = t['File'] if fatal else t.get('File') | |
224 | if not audio_id: | |
225 | return | |
226 | ||
227 | thumbnail = t.get('AlbumCoverURL') or t.get('FiledAlbumCover') | |
228 | uploader = t.get('OwnerName') or t.get('OwnerName_Text_HTML') | |
229 | uploader_id = t.get('UploaderID') | |
230 | duration = int_or_none(t.get('DurationInSeconds')) or parse_duration( | |
231 | t.get('Duration') or t.get('DurationStr')) | |
232 | view_count = int_or_none(t.get('PlayCount') or t.get('PlayCount_hr')) | |
233 | ||
234 | track = t.get('Name') or t.get('Name_Text_HTML') | |
235 | artist = t.get('Author') or t.get('Author_Text_HTML') | |
236 | ||
237 | if track: | |
add96eb9 | 238 | title = f'{artist} - {track}' if artist else track |
f2908d07 S |
239 | else: |
240 | title = audio_id | |
241 | ||
242 | return { | |
243 | 'extractor_key': MailRuMusicIE.ie_key(), | |
244 | 'id': audio_id, | |
245 | 'title': title, | |
246 | 'thumbnail': thumbnail, | |
247 | 'uploader': uploader, | |
248 | 'uploader_id': uploader_id, | |
249 | 'duration': duration, | |
250 | 'view_count': view_count, | |
251 | 'vcodec': 'none', | |
252 | 'abr': int_or_none(t.get('BitRate')), | |
253 | 'track': track, | |
254 | 'artist': artist, | |
255 | 'album': t.get('Album'), | |
256 | 'url': audio_url, | |
257 | } | |
258 | ||
259 | ||
260 | class MailRuMusicIE(MailRuMusicSearchBaseIE): | |
261 | IE_NAME = 'mailru:music' | |
262 | IE_DESC = 'Музыка@Mail.Ru' | |
278be57b | 263 | _VALID_URL = r'https?://my\.mail\.ru/+music/+songs/+[^/?#&]+-(?P<id>[\da-f]+)' |
f2908d07 S |
264 | _TESTS = [{ |
265 | 'url': 'https://my.mail.ru/music/songs/%D0%BC8%D0%BB8%D1%82%D1%85-l-a-h-luciferian-aesthetics-of-herrschaft-single-2017-4e31f7125d0dfaef505d947642366893', | |
266 | 'md5': '0f8c22ef8c5d665b13ac709e63025610', | |
267 | 'info_dict': { | |
268 | 'id': '4e31f7125d0dfaef505d947642366893', | |
269 | 'ext': 'mp3', | |
270 | 'title': 'L.A.H. (Luciferian Aesthetics of Herrschaft) single, 2017 - М8Л8ТХ', | |
271 | 'uploader': 'Игорь Мудрый', | |
272 | 'uploader_id': '1459196328', | |
273 | 'duration': 280, | |
274 | 'view_count': int, | |
275 | 'vcodec': 'none', | |
276 | 'abr': 320, | |
277 | 'track': 'L.A.H. (Luciferian Aesthetics of Herrschaft) single, 2017', | |
278 | 'artist': 'М8Л8ТХ', | |
279 | }, | |
280 | }] | |
281 | ||
282 | def _real_extract(self, url): | |
283 | audio_id = self._match_id(url) | |
284 | ||
285 | webpage = self._download_webpage(url, audio_id) | |
286 | ||
287 | title = self._og_search_title(webpage) | |
288 | music_data = self._search(title, url, audio_id)['MusicData'] | |
289 | t = next(t for t in music_data if t.get('File') == audio_id) | |
290 | ||
291 | info = self._extract_track(t) | |
292 | info['title'] = title | |
293 | return info | |
294 | ||
295 | ||
296 | class MailRuMusicSearchIE(MailRuMusicSearchBaseIE): | |
297 | IE_NAME = 'mailru:music:search' | |
298 | IE_DESC = 'Музыка@Mail.Ru' | |
278be57b | 299 | _VALID_URL = r'https?://my\.mail\.ru/+music/+search/+(?P<id>[^/?#&]+)' |
f2908d07 S |
300 | _TESTS = [{ |
301 | 'url': 'https://my.mail.ru/music/search/black%20shadow', | |
302 | 'info_dict': { | |
303 | 'id': 'black shadow', | |
304 | }, | |
305 | 'playlist_mincount': 532, | |
306 | }] | |
307 | ||
308 | def _real_extract(self, url): | |
add96eb9 | 309 | query = urllib.parse.unquote(self._match_id(url)) |
f2908d07 S |
310 | |
311 | entries = [] | |
312 | ||
313 | LIMIT = 100 | |
314 | offset = 0 | |
315 | ||
316 | for _ in itertools.count(1): | |
317 | search = self._search(query, url, query, LIMIT, offset) | |
318 | ||
319 | music_data = search.get('MusicData') | |
320 | if not music_data or not isinstance(music_data, list): | |
321 | break | |
322 | ||
323 | for t in music_data: | |
324 | track = self._extract_track(t, fatal=False) | |
325 | if track: | |
326 | entries.append(track) | |
327 | ||
328 | total = try_get( | |
329 | search, lambda x: x['Results']['music']['Total'], int) | |
330 | ||
331 | if total is not None: | |
332 | if offset > total: | |
333 | break | |
334 | ||
335 | offset += LIMIT | |
336 | ||
337 | return self.playlist_result(entries, query) |