]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/odnoklassniki.py
Rebuild with only Python 3 support.
[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_parse_qs,
7 compat_urllib_parse_unquote,
8 compat_urllib_parse_urlparse,
9 )
10 from ..utils import (
11 ExtractorError,
12 unified_strdate,
13 int_or_none,
14 qualities,
15 unescapeHTML,
16 )
17
18
19 class OdnoklassnikiIE(InfoExtractor):
20 _VALID_URL = r'https?://(?:(?:www|m|mobile)\.)?(?:odnoklassniki|ok)\.ru/(?:video(?:embed)?|web-api/video/moviePlayer)/(?P<id>[\d-]+)'
21 _TESTS = [{
22 # metadata in JSON
23 'url': 'http://ok.ru/video/20079905452',
24 'md5': '6ba728d85d60aa2e6dd37c9e70fdc6bc',
25 'info_dict': {
26 'id': '20079905452',
27 'ext': 'mp4',
28 'title': 'Культура меняет нас (прекрасный ролик!))',
29 'duration': 100,
30 'upload_date': '20141207',
31 'uploader_id': '330537914540',
32 'uploader': 'Виталий Добровольский',
33 'like_count': int,
34 'age_limit': 0,
35 },
36 'skip': 'Video has been blocked',
37 }, {
38 # metadataUrl
39 'url': 'http://ok.ru/video/63567059965189-0?fromTime=5',
40 'md5': '9676cf86eff5391d35dea675d224e131',
41 'info_dict': {
42 'id': '63567059965189-0',
43 'ext': 'mp4',
44 'title': 'Девушка без комплексов ...',
45 'duration': 191,
46 'upload_date': '20150518',
47 'uploader_id': '534380003155',
48 'uploader': '☭ Андрей Мещанинов ☭',
49 'like_count': int,
50 'age_limit': 0,
51 'start_time': 5,
52 },
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 },
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 },
84 }, {
85 'url': 'http://ok.ru/web-api/video/moviePlayer/20079905452',
86 'only_matching': True,
87 }, {
88 'url': 'http://www.ok.ru/video/20648036891',
89 'only_matching': True,
90 }, {
91 'url': 'http://www.ok.ru/videoembed/20648036891',
92 'only_matching': True,
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,
99 }]
100
101 def _real_extract(self, url):
102 start_time = int_or_none(compat_parse_qs(
103 compat_urllib_parse_urlparse(url).query).get('fromTime', [None])[0])
104
105 video_id = self._match_id(url)
106
107 webpage = self._download_webpage(
108 'http://ok.ru/video/%s' % video_id, video_id)
109
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
116 player = self._parse_json(
117 unescapeHTML(self._search_regex(
118 r'data-options=(?P<quote>["\'])(?P<player>{.+?%s.+?})(?P=quote)' % video_id,
119 webpage, 'player', group='player')),
120 video_id)
121
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(
129 compat_urllib_parse_unquote(flashvars['metadataUrl']),
130 video_id, 'Downloading metadata JSON')
131
132 movie = metadata['movie']
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
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(
149 'ya:ovs:upload_date', webpage, 'upload date', default=None))
150
151 age_limit = None
152 adult = self._html_search_meta(
153 'ya:ovs:adult', webpage, 'age limit', default=None)
154 if adult:
155 age_limit = 18 if adult == 'true' else 0
156
157 like_count = int_or_none(metadata.get('likeCount'))
158
159 info = {
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,
169 'start_time': start_time,
170 }
171
172 if provider == 'USER_YOUTUBE':
173 info.update({
174 '_type': 'url_transparent',
175 'url': movie['contentId'],
176 })
177 return info
178
179 quality = qualities(('mobile', 'lowest', 'low', 'sd', 'hd'))
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']]
187 self._sort_formats(formats)
188
189 info['formats'] = formats
190 return info