Merge branch 'master' of github.com:rg3/youtube-dl into weibo
[youtube-dl] / youtube_dl / extractor / mailru.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     int_or_none,
9     remove_end,
10 )
11
12
13 class MailRuIE(InfoExtractor):
14     IE_NAME = 'mailru'
15     IE_DESC = 'Видео@Mail.Ru'
16     _VALID_URL = r'''(?x)
17                     https?://
18                         (?:(?:www|m)\.)?my\.mail\.ru/
19                         (?:
20                             video/.*\#video=/?(?P<idv1>(?:[^/]+/){3}\d+)|
21                             (?:(?P<idv2prefix>(?:[^/]+/){2})video/(?P<idv2suffix>[^/]+/\d+))\.html|
22                             (?:video/embed|\+/video/meta)/(?P<metaid>\d+)
23                         )
24                     '''
25     _TESTS = [
26         {
27             'url': 'http://my.mail.ru/video/top#video=/mail/sonypicturesrus/75/76',
28             'md5': 'dea205f03120046894db4ebb6159879a',
29             'info_dict': {
30                 'id': '46301138_76',
31                 'ext': 'mp4',
32                 'title': 'Новый Человек-Паук. Высокое напряжение. Восстание Электро',
33                 'timestamp': 1393235077,
34                 'upload_date': '20140224',
35                 'uploader': 'sonypicturesrus',
36                 'uploader_id': 'sonypicturesrus@mail.ru',
37                 'duration': 184,
38             },
39             'skip': 'Not accessible from Travis CI server',
40         },
41         {
42             'url': 'http://my.mail.ru/corp/hitech/video/news_hi-tech_mail_ru/1263.html',
43             'md5': '00a91a58c3402204dcced523777b475f',
44             'info_dict': {
45                 'id': '46843144_1263',
46                 'ext': 'mp4',
47                 'title': 'Samsung Galaxy S5 Hammer Smash Fail Battery Explosion',
48                 'timestamp': 1397039888,
49                 'upload_date': '20140409',
50                 'uploader': 'hitech',
51                 'uploader_id': 'hitech@corp.mail.ru',
52                 'duration': 245,
53             },
54             'skip': 'Not accessible from Travis CI server',
55         },
56         {
57             # only available via metaUrl API
58             'url': 'http://my.mail.ru/mail/720pizle/video/_myvideo/502.html',
59             'md5': '3b26d2491c6949d031a32b96bd97c096',
60             'info_dict': {
61                 'id': '56664382_502',
62                 'ext': 'mp4',
63                 'title': ':8336',
64                 'timestamp': 1449094163,
65                 'upload_date': '20151202',
66                 'uploader': '720pizle@mail.ru',
67                 'uploader_id': '720pizle@mail.ru',
68                 'duration': 6001,
69             },
70             'skip': 'Not accessible from Travis CI server',
71         },
72         {
73             'url': 'http://m.my.mail.ru/mail/3sktvtr/video/_myvideo/138.html',
74             'only_matching': True,
75         },
76         {
77             'url': 'https://my.mail.ru/video/embed/7949340477499637815',
78             'only_matching': True,
79         },
80         {
81             'url': 'http://my.mail.ru/+/video/meta/7949340477499637815',
82             'only_matching': True,
83         }
84     ]
85
86     def _real_extract(self, url):
87         mobj = re.match(self._VALID_URL, url)
88         meta_id = mobj.group('metaid')
89
90         video_id = None
91         if meta_id:
92             meta_url = 'https://my.mail.ru/+/video/meta/%s' % meta_id
93         else:
94             video_id = mobj.group('idv1')
95             if not video_id:
96                 video_id = mobj.group('idv2prefix') + mobj.group('idv2suffix')
97             webpage = self._download_webpage(url, video_id)
98             page_config = self._parse_json(self._search_regex(
99                 r'(?s)<script[^>]+class="sp-video__page-config"[^>]*>(.+?)</script>',
100                 webpage, 'page config', default='{}'), video_id, fatal=False)
101             if page_config:
102                 meta_url = page_config.get('metaUrl') or page_config.get('video', {}).get('metaUrl')
103             else:
104                 meta_url = None
105
106         video_data = None
107         if meta_url:
108             video_data = self._download_json(
109                 meta_url, video_id or meta_id, 'Downloading video meta JSON',
110                 fatal=not video_id)
111
112         # Fallback old approach
113         if not video_data:
114             video_data = self._download_json(
115                 'http://api.video.mail.ru/videos/%s.json?new=1' % video_id,
116                 video_id, 'Downloading video JSON')
117
118         formats = []
119         for f in video_data['videos']:
120             video_url = f.get('url')
121             if not video_url:
122                 continue
123             format_id = f.get('key')
124             height = int_or_none(self._search_regex(
125                 r'^(\d+)[pP]$', format_id, 'height', default=None)) if format_id else None
126             formats.append({
127                 'url': video_url,
128                 'format_id': format_id,
129                 'height': height,
130             })
131         self._sort_formats(formats)
132
133         meta_data = video_data['meta']
134         title = remove_end(meta_data['title'], '.mp4')
135
136         author = video_data.get('author')
137         uploader = author.get('name')
138         uploader_id = author.get('id') or author.get('email')
139         view_count = int_or_none(video_data.get('viewsCount') or video_data.get('views_count'))
140
141         acc_id = meta_data.get('accId')
142         item_id = meta_data.get('itemId')
143         content_id = '%s_%s' % (acc_id, item_id) if acc_id and item_id else video_id
144
145         thumbnail = meta_data.get('poster')
146         duration = int_or_none(meta_data.get('duration'))
147         timestamp = int_or_none(meta_data.get('timestamp'))
148
149         return {
150             'id': content_id,
151             'title': title,
152             'thumbnail': thumbnail,
153             'timestamp': timestamp,
154             'uploader': uploader,
155             'uploader_id': uploader_id,
156             'duration': duration,
157             'view_count': view_count,
158             'formats': formats,
159         }