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