]>
Commit | Line | Data |
---|---|---|
4ffbf778 S |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
4 | from .common import InfoExtractor | |
c9fd5306 | 5 | from ..compat import ( |
1c35b3da | 6 | compat_etree_fromstring, |
c9fd5306 S |
7 | compat_parse_qs, |
8 | compat_urllib_parse_unquote, | |
9 | compat_urllib_parse_urlparse, | |
10 | ) | |
4ffbf778 | 11 | from ..utils import ( |
1806a754 | 12 | ExtractorError, |
4ffbf778 S |
13 | unified_strdate, |
14 | int_or_none, | |
15 | qualities, | |
372744c5 | 16 | unescapeHTML, |
a3474aa5 | 17 | urlencode_postdata, |
4ffbf778 S |
18 | ) |
19 | ||
20 | ||
21 | class OdnoklassnikiIE(InfoExtractor): | |
d04ca976 S |
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 | ''' | |
4ffbf778 | 34 | _TESTS = [{ |
c6bbdadd | 35 | # metadata in JSON |
4ffbf778 | 36 | 'url': 'http://ok.ru/video/20079905452', |
8005dc68 | 37 | 'md5': '0b62089b479e06681abaaca9d204f152', |
4ffbf778 S |
38 | 'info_dict': { |
39 | 'id': '20079905452', | |
40 | 'ext': 'mp4', | |
41 | 'title': 'Культура меняет нас (прекрасный ролик!))', | |
42 | 'duration': 100, | |
887e9bc7 | 43 | 'upload_date': '20141207', |
4ffbf778 S |
44 | 'uploader_id': '330537914540', |
45 | 'uploader': 'Виталий Добровольский', | |
46 | 'like_count': int, | |
9f2e7c2f | 47 | 'age_limit': 0, |
c6bbdadd S |
48 | }, |
49 | }, { | |
50 | # metadataUrl | |
c9fd5306 | 51 | 'url': 'http://ok.ru/video/63567059965189-0?fromTime=5', |
58f6ab72 | 52 | 'md5': '6ff470ea2dd51d5d18c295a355b0b6bc', |
c6bbdadd S |
53 | 'info_dict': { |
54 | 'id': '63567059965189-0', | |
55 | 'ext': 'mp4', | |
56 | 'title': 'Девушка без комплексов ...', | |
57 | 'duration': 191, | |
887e9bc7 | 58 | 'upload_date': '20150518', |
c6bbdadd | 59 | 'uploader_id': '534380003155', |
887e9bc7 | 60 | 'uploader': '☭ Андрей Мещанинов ☭', |
c6bbdadd | 61 | 'like_count': int, |
9f2e7c2f | 62 | 'age_limit': 0, |
c9fd5306 | 63 | 'start_time': 5, |
4ffbf778 | 64 | }, |
88720ed0 S |
65 | }, { |
66 | # YouTube embed (metadataUrl, provider == USER_YOUTUBE) | |
67 | 'url': 'http://ok.ru/video/64211978996595-1', | |
58f6ab72 | 68 | 'md5': '2f206894ffb5dbfcce2c5a14b909eea5', |
88720ed0 | 69 | 'info_dict': { |
a3474aa5 | 70 | 'id': 'V_VztHT5BzY', |
88720ed0 S |
71 | 'ext': 'mp4', |
72 | 'title': 'Космическая среда от 26 августа 2015', | |
73 | 'description': 'md5:848eb8b85e5e3471a3a803dae1343ed0', | |
74 | 'duration': 440, | |
75 | 'upload_date': '20150826', | |
58f6ab72 RA |
76 | 'uploader_id': 'tvroscosmos', |
77 | 'uploader': 'Телестудия Роскосмоса', | |
88720ed0 S |
78 | 'age_limit': 0, |
79 | }, | |
749b0046 S |
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 | }, | |
58f6ab72 | 96 | 'skip': 'Video has not been found', |
4ffbf778 S |
97 | }, { |
98 | 'url': 'http://ok.ru/web-api/video/moviePlayer/20079905452', | |
99 | 'only_matching': True, | |
cdc8d0c3 YCH |
100 | }, { |
101 | 'url': 'http://www.ok.ru/video/20648036891', | |
102 | 'only_matching': True, | |
d762f86e S |
103 | }, { |
104 | 'url': 'http://www.ok.ru/videoembed/20648036891', | |
105 | 'only_matching': True, | |
10e6ed93 S |
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, | |
8005dc68 S |
112 | }, { |
113 | 'url': 'https://www.ok.ru/live/484531969818', | |
114 | 'only_matching': True, | |
608c738c G |
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, | |
4ffbf778 S |
118 | }] |
119 | ||
120 | def _real_extract(self, url): | |
c9fd5306 S |
121 | start_time = int_or_none(compat_parse_qs( |
122 | compat_urllib_parse_urlparse(url).query).get('fromTime', [None])[0]) | |
123 | ||
4ffbf778 S |
124 | video_id = self._match_id(url) |
125 | ||
ba2df04b S |
126 | webpage = self._download_webpage( |
127 | 'http://ok.ru/video/%s' % video_id, video_id) | |
4ffbf778 | 128 | |
1806a754 S |
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 | ||
4ffbf778 | 135 | player = self._parse_json( |
372744c5 | 136 | unescapeHTML(self._search_regex( |
1e804244 S |
137 | r'data-options=(?P<quote>["\'])(?P<player>{.+?%s.+?})(?P=quote)' % video_id, |
138 | webpage, 'player', group='player')), | |
4ffbf778 S |
139 | video_id) |
140 | ||
c6bbdadd S |
141 | flashvars = player['flashvars'] |
142 | ||
143 | metadata = flashvars.get('metadata') | |
144 | if metadata: | |
145 | metadata = self._parse_json(metadata, video_id) | |
146 | else: | |
a3474aa5 RA |
147 | data = {} |
148 | st_location = flashvars.get('location') | |
149 | if st_location: | |
150 | data['st.location'] = st_location | |
c6bbdadd | 151 | metadata = self._download_json( |
b78f5ec4 | 152 | compat_urllib_parse_unquote(flashvars['metadataUrl']), |
a3474aa5 RA |
153 | video_id, 'Downloading metadata JSON', |
154 | data=urlencode_postdata(data)) | |
4ffbf778 S |
155 | |
156 | movie = metadata['movie'] | |
749b0046 S |
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 | ||
4ffbf778 S |
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( | |
c6bbdadd | 173 | 'ya:ovs:upload_date', webpage, 'upload date', default=None)) |
4ffbf778 S |
174 | |
175 | age_limit = None | |
176 | adult = self._html_search_meta( | |
c6bbdadd | 177 | 'ya:ovs:adult', webpage, 'age limit', default=None) |
4ffbf778 S |
178 | if adult: |
179 | age_limit = 18 if adult == 'true' else 0 | |
180 | ||
181 | like_count = int_or_none(metadata.get('likeCount')) | |
182 | ||
88720ed0 | 183 | info = { |
4ffbf778 S |
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, | |
c9fd5306 | 193 | 'start_time': start_time, |
4ffbf778 | 194 | } |
88720ed0 | 195 | |
749b0046 | 196 | if provider == 'USER_YOUTUBE': |
88720ed0 S |
197 | info.update({ |
198 | '_type': 'url_transparent', | |
199 | 'url': movie['contentId'], | |
200 | }) | |
201 | return info | |
202 | ||
8005dc68 S |
203 | assert title |
204 | if provider == 'LIVE_TV_APP': | |
205 | info['title'] = self._live_title(title) | |
206 | ||
1c35b3da | 207 | quality = qualities(('4', '0', '1', '2', '3', '5')) |
88720ed0 S |
208 | |
209 | formats = [{ | |
210 | 'url': f['url'], | |
211 | 'ext': 'mp4', | |
212 | 'format_id': f['name'], | |
88720ed0 | 213 | } for f in metadata['videos']] |
1c35b3da RA |
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 | ||
8005dc68 S |
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 | ||
e8dcfa3d | 247 | self._sort_formats(formats) |
88720ed0 S |
248 | |
249 | info['formats'] = formats | |
250 | return info |