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