]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/odnoklassniki.py
Update changelog.
[youtubedl] / youtube_dl / extractor / odnoklassniki.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import (
6 compat_etree_fromstring,
7 compat_parse_qs,
8 compat_urllib_parse_unquote,
9 compat_urllib_parse_urlparse,
10 )
11 from ..utils import (
12 ExtractorError,
13 unified_strdate,
14 int_or_none,
15 qualities,
16 unescapeHTML,
17 )
18
19
20 class OdnoklassnikiIE(InfoExtractor):
21 _VALID_URL = r'https?://(?:(?:www|m|mobile)\.)?(?:odnoklassniki|ok)\.ru/(?:video(?:embed)?|web-api/video/moviePlayer)/(?P<id>[\d-]+)'
22 _TESTS = [{
23 # metadata in JSON
24 'url': 'http://ok.ru/video/20079905452',
25 'md5': '6ba728d85d60aa2e6dd37c9e70fdc6bc',
26 'info_dict': {
27 'id': '20079905452',
28 'ext': 'mp4',
29 'title': 'Культура меняет нас (прекрасный ролик!))',
30 'duration': 100,
31 'upload_date': '20141207',
32 'uploader_id': '330537914540',
33 'uploader': 'Виталий Добровольский',
34 'like_count': int,
35 'age_limit': 0,
36 },
37 'skip': 'Video has been blocked',
38 }, {
39 # metadataUrl
40 'url': 'http://ok.ru/video/63567059965189-0?fromTime=5',
41 'md5': '6ff470ea2dd51d5d18c295a355b0b6bc',
42 'info_dict': {
43 'id': '63567059965189-0',
44 'ext': 'mp4',
45 'title': 'Девушка без комплексов ...',
46 'duration': 191,
47 'upload_date': '20150518',
48 'uploader_id': '534380003155',
49 'uploader': '☭ Андрей Мещанинов ☭',
50 'like_count': int,
51 'age_limit': 0,
52 'start_time': 5,
53 },
54 }, {
55 # YouTube embed (metadataUrl, provider == USER_YOUTUBE)
56 'url': 'http://ok.ru/video/64211978996595-1',
57 'md5': '2f206894ffb5dbfcce2c5a14b909eea5',
58 'info_dict': {
59 'id': '64211978996595-1',
60 'ext': 'mp4',
61 'title': 'Космическая среда от 26 августа 2015',
62 'description': 'md5:848eb8b85e5e3471a3a803dae1343ed0',
63 'duration': 440,
64 'upload_date': '20150826',
65 'uploader_id': 'tvroscosmos',
66 'uploader': 'Телестудия Роскосмоса',
67 'age_limit': 0,
68 },
69 }, {
70 # YouTube embed (metadata, provider == USER_YOUTUBE, no metadata.movie.title field)
71 'url': 'http://ok.ru/video/62036049272859-0',
72 'info_dict': {
73 'id': '62036049272859-0',
74 'ext': 'mp4',
75 'title': 'МУЗЫКА ДОЖДЯ .',
76 'description': 'md5:6f1867132bd96e33bf53eda1091e8ed0',
77 'upload_date': '20120106',
78 'uploader_id': '473534735899',
79 'uploader': 'МARINA D',
80 'age_limit': 0,
81 },
82 'params': {
83 'skip_download': True,
84 },
85 'skip': 'Video has not been found',
86 }, {
87 'url': 'http://ok.ru/web-api/video/moviePlayer/20079905452',
88 'only_matching': True,
89 }, {
90 'url': 'http://www.ok.ru/video/20648036891',
91 'only_matching': True,
92 }, {
93 'url': 'http://www.ok.ru/videoembed/20648036891',
94 'only_matching': True,
95 }, {
96 'url': 'http://m.ok.ru/video/20079905452',
97 'only_matching': True,
98 }, {
99 'url': 'http://mobile.ok.ru/video/20079905452',
100 'only_matching': True,
101 }]
102
103 def _real_extract(self, url):
104 start_time = int_or_none(compat_parse_qs(
105 compat_urllib_parse_urlparse(url).query).get('fromTime', [None])[0])
106
107 video_id = self._match_id(url)
108
109 webpage = self._download_webpage(
110 'http://ok.ru/video/%s' % video_id, video_id)
111
112 error = self._search_regex(
113 r'[^>]+class="vp_video_stub_txt"[^>]*>([^<]+)<',
114 webpage, 'error', default=None)
115 if error:
116 raise ExtractorError(error, expected=True)
117
118 player = self._parse_json(
119 unescapeHTML(self._search_regex(
120 r'data-options=(?P<quote>["\'])(?P<player>{.+?%s.+?})(?P=quote)' % video_id,
121 webpage, 'player', group='player')),
122 video_id)
123
124 flashvars = player['flashvars']
125
126 metadata = flashvars.get('metadata')
127 if metadata:
128 metadata = self._parse_json(metadata, video_id)
129 else:
130 metadata = self._download_json(
131 compat_urllib_parse_unquote(flashvars['metadataUrl']),
132 video_id, 'Downloading metadata JSON')
133
134 movie = metadata['movie']
135
136 # Some embedded videos may not contain title in movie dict (e.g.
137 # http://ok.ru/video/62036049272859-0) thus we allow missing title
138 # here and it's going to be extracted later by an extractor that
139 # will process the actual embed.
140 provider = metadata.get('provider')
141 title = movie['title'] if provider == 'UPLOADED_ODKL' else movie.get('title')
142
143 thumbnail = movie.get('poster')
144 duration = int_or_none(movie.get('duration'))
145
146 author = metadata.get('author', {})
147 uploader_id = author.get('id')
148 uploader = author.get('name')
149
150 upload_date = unified_strdate(self._html_search_meta(
151 'ya:ovs:upload_date', webpage, 'upload date', default=None))
152
153 age_limit = None
154 adult = self._html_search_meta(
155 'ya:ovs:adult', webpage, 'age limit', default=None)
156 if adult:
157 age_limit = 18 if adult == 'true' else 0
158
159 like_count = int_or_none(metadata.get('likeCount'))
160
161 info = {
162 'id': video_id,
163 'title': title,
164 'thumbnail': thumbnail,
165 'duration': duration,
166 'upload_date': upload_date,
167 'uploader': uploader,
168 'uploader_id': uploader_id,
169 'like_count': like_count,
170 'age_limit': age_limit,
171 'start_time': start_time,
172 }
173
174 if provider == 'USER_YOUTUBE':
175 info.update({
176 '_type': 'url_transparent',
177 'url': movie['contentId'],
178 })
179 return info
180
181 quality = qualities(('4', '0', '1', '2', '3', '5'))
182
183 formats = [{
184 'url': f['url'],
185 'ext': 'mp4',
186 'format_id': f['name'],
187 } for f in metadata['videos']]
188
189 m3u8_url = metadata.get('hlsManifestUrl')
190 if m3u8_url:
191 formats.extend(self._extract_m3u8_formats(
192 m3u8_url, video_id, 'mp4', 'm3u8_native',
193 m3u8_id='hls', fatal=False))
194
195 dash_manifest = metadata.get('metadataEmbedded')
196 if dash_manifest:
197 formats.extend(self._parse_mpd_formats(
198 compat_etree_fromstring(dash_manifest), 'mpd'))
199
200 for fmt in formats:
201 fmt_type = self._search_regex(
202 r'\btype[/=](\d)', fmt['url'],
203 'format type', default=None)
204 if fmt_type:
205 fmt['quality'] = quality(fmt_type)
206
207 self._sort_formats(formats)
208
209 info['formats'] = formats
210 return info