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