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