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