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