]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/odnoklassniki.py
[ndr:embed:base] Add missing ext for m3u8
[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
b78f5ec4 5from ..compat import compat_urllib_parse_unquote
4ffbf778 6from ..utils import (
1806a754 7 ExtractorError,
4ffbf778
S
8 unified_strdate,
9 int_or_none,
10 qualities,
372744c5 11 unescapeHTML,
4ffbf778
S
12)
13
14
15class OdnoklassnikiIE(InfoExtractor):
d762f86e 16 _VALID_URL = r'https?://(?:www\.)?(?:odnoklassniki|ok)\.ru/(?:video(?:embed)?|web-api/video/moviePlayer)/(?P<id>[\d-]+)'
4ffbf778 17 _TESTS = [{
c6bbdadd 18 # metadata in JSON
4ffbf778 19 'url': 'http://ok.ru/video/20079905452',
887e9bc7 20 'md5': '6ba728d85d60aa2e6dd37c9e70fdc6bc',
4ffbf778
S
21 'info_dict': {
22 'id': '20079905452',
23 'ext': 'mp4',
24 'title': 'Культура меняет нас (прекрасный ролик!))',
25 'duration': 100,
887e9bc7 26 'upload_date': '20141207',
4ffbf778
S
27 'uploader_id': '330537914540',
28 'uploader': 'Виталий Добровольский',
29 'like_count': int,
9f2e7c2f 30 'age_limit': 0,
c6bbdadd 31 },
131d0503 32 'skip': 'Video has been blocked',
c6bbdadd
S
33 }, {
34 # metadataUrl
35 'url': 'http://ok.ru/video/63567059965189-0',
36 'md5': '9676cf86eff5391d35dea675d224e131',
37 'info_dict': {
38 'id': '63567059965189-0',
39 'ext': 'mp4',
40 'title': 'Девушка без комплексов ...',
41 'duration': 191,
887e9bc7 42 'upload_date': '20150518',
c6bbdadd 43 'uploader_id': '534380003155',
887e9bc7 44 'uploader': '☭ Андрей Мещанинов ☭',
c6bbdadd 45 'like_count': int,
9f2e7c2f 46 'age_limit': 0,
4ffbf778 47 },
88720ed0
S
48 }, {
49 # YouTube embed (metadataUrl, provider == USER_YOUTUBE)
50 'url': 'http://ok.ru/video/64211978996595-1',
51 'md5': '5d7475d428845cd2e13bae6f1a992278',
52 'info_dict': {
53 'id': '64211978996595-1',
54 'ext': 'mp4',
55 'title': 'Космическая среда от 26 августа 2015',
56 'description': 'md5:848eb8b85e5e3471a3a803dae1343ed0',
57 'duration': 440,
58 'upload_date': '20150826',
59 'uploader_id': '750099571',
60 'uploader': 'Алина П',
61 'age_limit': 0,
62 },
4ffbf778
S
63 }, {
64 'url': 'http://ok.ru/web-api/video/moviePlayer/20079905452',
65 'only_matching': True,
cdc8d0c3
YCH
66 }, {
67 'url': 'http://www.ok.ru/video/20648036891',
68 'only_matching': True,
d762f86e
S
69 }, {
70 'url': 'http://www.ok.ru/videoembed/20648036891',
71 'only_matching': True,
4ffbf778
S
72 }]
73
74 def _real_extract(self, url):
75 video_id = self._match_id(url)
76
ba2df04b
S
77 webpage = self._download_webpage(
78 'http://ok.ru/video/%s' % video_id, video_id)
4ffbf778 79
1806a754
S
80 error = self._search_regex(
81 r'[^>]+class="vp_video_stub_txt"[^>]*>([^<]+)<',
82 webpage, 'error', default=None)
83 if error:
84 raise ExtractorError(error, expected=True)
85
4ffbf778 86 player = self._parse_json(
372744c5 87 unescapeHTML(self._search_regex(
1e804244
S
88 r'data-options=(?P<quote>["\'])(?P<player>{.+?%s.+?})(?P=quote)' % video_id,
89 webpage, 'player', group='player')),
4ffbf778
S
90 video_id)
91
c6bbdadd
S
92 flashvars = player['flashvars']
93
94 metadata = flashvars.get('metadata')
95 if metadata:
96 metadata = self._parse_json(metadata, video_id)
97 else:
98 metadata = self._download_json(
b78f5ec4 99 compat_urllib_parse_unquote(flashvars['metadataUrl']),
c6bbdadd 100 video_id, 'Downloading metadata JSON')
4ffbf778
S
101
102 movie = metadata['movie']
103 title = movie['title']
104 thumbnail = movie.get('poster')
105 duration = int_or_none(movie.get('duration'))
106
107 author = metadata.get('author', {})
108 uploader_id = author.get('id')
109 uploader = author.get('name')
110
111 upload_date = unified_strdate(self._html_search_meta(
c6bbdadd 112 'ya:ovs:upload_date', webpage, 'upload date', default=None))
4ffbf778
S
113
114 age_limit = None
115 adult = self._html_search_meta(
c6bbdadd 116 'ya:ovs:adult', webpage, 'age limit', default=None)
4ffbf778
S
117 if adult:
118 age_limit = 18 if adult == 'true' else 0
119
120 like_count = int_or_none(metadata.get('likeCount'))
121
88720ed0 122 info = {
4ffbf778
S
123 'id': video_id,
124 'title': title,
125 'thumbnail': thumbnail,
126 'duration': duration,
127 'upload_date': upload_date,
128 'uploader': uploader,
129 'uploader_id': uploader_id,
130 'like_count': like_count,
131 'age_limit': age_limit,
4ffbf778 132 }
88720ed0
S
133
134 if metadata.get('provider') == 'USER_YOUTUBE':
135 info.update({
136 '_type': 'url_transparent',
137 'url': movie['contentId'],
138 })
139 return info
140
141 quality = qualities(('mobile', 'lowest', 'low', 'sd', 'hd'))
142
143 formats = [{
144 'url': f['url'],
145 'ext': 'mp4',
146 'format_id': f['name'],
147 'quality': quality(f['name']),
148 } for f in metadata['videos']]
e8dcfa3d 149 self._sort_formats(formats)
88720ed0
S
150
151 info['formats'] = formats
152 return info