]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/mailru.py
[mailru] Prefer metaUrl API (Closes #8474)
[yt-dlp.git] / youtube_dl / extractor / mailru.py
CommitLineData
69bb54eb
S
1# encoding: utf-8
2from __future__ import unicode_literals
3
4import re
69bb54eb
S
5
6from .common import InfoExtractor
7
8
9class MailRuIE(InfoExtractor):
10 IE_NAME = 'mailru'
11 IE_DESC = 'Видео@Mail.Ru'
ceb7a17f 12 _VALID_URL = r'http://(?:www\.)?my\.mail\.ru/(?:video/.*#video=/?(?P<idv1>(?:[^/]+/){3}\d+)|(?:(?P<idv2prefix>(?:[^/]+/){2})video/(?P<idv2suffix>[^/]+/\d+))\.html)'
69bb54eb 13
ceb7a17f
S
14 _TESTS = [
15 {
16 'url': 'http://my.mail.ru/video/top#video=/mail/sonypicturesrus/75/76',
17 'md5': 'dea205f03120046894db4ebb6159879a',
18 'info_dict': {
00d9ef0b 19 'id': '46301138_76',
ceb7a17f
S
20 'ext': 'mp4',
21 'title': 'Новый Человек-Паук. Высокое напряжение. Восстание Электро',
22 'timestamp': 1393232740,
23 'upload_date': '20140224',
24 'uploader': 'sonypicturesrus',
25 'uploader_id': 'sonypicturesrus@mail.ru',
26 'duration': 184,
27 },
95e431e9 28 'skip': 'Not accessible from Travis CI server',
ceb7a17f
S
29 },
30 {
31 'url': 'http://my.mail.ru/corp/hitech/video/news_hi-tech_mail_ru/1263.html',
32 'md5': '00a91a58c3402204dcced523777b475f',
33 'info_dict': {
00d9ef0b 34 'id': '46843144_1263',
ceb7a17f
S
35 'ext': 'mp4',
36 'title': 'Samsung Galaxy S5 Hammer Smash Fail Battery Explosion',
37 'timestamp': 1397217632,
38 'upload_date': '20140411',
39 'uploader': 'hitech',
40 'uploader_id': 'hitech@corp.mail.ru',
41 'duration': 245,
42 },
95e431e9 43 'skip': 'Not accessible from Travis CI server',
ceb7a17f 44 },
16f1430b
S
45 {
46 # only available via metaUrl API
47 'url': 'http://my.mail.ru/mail/720pizle/video/_myvideo/502.html',
48 'md5': '3b26d2491c6949d031a32b96bd97c096',
49 'info_dict': {
50 'id': '56664382_502',
51 'ext': 'mp4',
52 'title': ':8336',
53 'timestamp': 1449094163,
54 'upload_date': '20151202',
55 'uploader': '720pizle@mail.ru',
56 'uploader_id': '720pizle@mail.ru',
57 'duration': 6001,
58 },
59 'skip': 'Not accessible from Travis CI server',
60 }
ceb7a17f 61 ]
69bb54eb
S
62
63 def _real_extract(self, url):
64 mobj = re.match(self._VALID_URL, url)
ceb7a17f
S
65 video_id = mobj.group('idv1')
66
67 if not video_id:
68 video_id = mobj.group('idv2prefix') + mobj.group('idv2suffix')
69bb54eb 69
16f1430b
S
70 webpage = self._download_webpage(url, video_id)
71
72 video_data = None
73
74 page_config = self._parse_json(self._search_regex(
75 r'(?s)<script[^>]+class="sp-video__page-config"[^>]*>(.+?)</script>',
76 webpage, 'page config', default='{}'), video_id, fatal=False)
77 if page_config:
78 meta_url = page_config.get('metaUrl') or page_config.get('video', {}).get('metaUrl')
79 if meta_url:
80 video_data = self._download_json(
81 meta_url, video_id, 'Downloading video meta JSON', fatal=False)
82
83 # Fallback old approach
84 if not video_data:
85 video_data = self._download_json(
86 'http://api.video.mail.ru/videos/%s.json?new=1' % video_id,
87 video_id, 'Downloading video JSON')
69bb54eb
S
88
89 author = video_data['author']
90 uploader = author['name']
00d9ef0b
PH
91 uploader_id = author.get('id') or author.get('email')
92 view_count = video_data.get('views_count')
69bb54eb 93
00d9ef0b
PH
94 meta_data = video_data['meta']
95 content_id = '%s_%s' % (
96 meta_data.get('accId', ''), meta_data['itemId'])
97 title = meta_data['title']
ceb7a17f
S
98 if title.endswith('.mp4'):
99 title = title[:-4]
00d9ef0b
PH
100 thumbnail = meta_data['poster']
101 duration = meta_data['duration']
102 timestamp = meta_data['timestamp']
69bb54eb
S
103
104 formats = [
105 {
106 'url': video['url'],
00d9ef0b
PH
107 'format_id': video['key'],
108 'height': int(video['key'].rstrip('p'))
69bb54eb
S
109 } for video in video_data['videos']
110 ]
00d9ef0b 111 self._sort_formats(formats)
69bb54eb
S
112
113 return {
114 'id': content_id,
115 'title': title,
116 'thumbnail': thumbnail,
00d9ef0b 117 'timestamp': timestamp,
69bb54eb
S
118 'uploader': uploader,
119 'uploader_id': uploader_id,
120 'duration': duration,
121 'view_count': view_count,
122 'formats': formats,
00d9ef0b 123 }