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