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