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