]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/odnoklassniki.py
debian/watch: Skip pgpsigurlmangle for the moment.
[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 compat_urllib_parse_unquote
6 from ..utils import (
7 ExtractorError,
8 unified_strdate,
9 int_or_none,
10 qualities,
11 unescapeHTML,
12 )
13
14
15 class OdnoklassnikiIE(InfoExtractor):
16 _VALID_URL = r'https?://(?:(?:www|m|mobile)\.)?(?:odnoklassniki|ok)\.ru/(?:video(?:embed)?|web-api/video/moviePlayer)/(?P<id>[\d-]+)'
17 _TESTS = [{
18 # metadata in JSON
19 'url': 'http://ok.ru/video/20079905452',
20 'md5': '6ba728d85d60aa2e6dd37c9e70fdc6bc',
21 'info_dict': {
22 'id': '20079905452',
23 'ext': 'mp4',
24 'title': 'Культура меняет нас (прекрасный ролик!))',
25 'duration': 100,
26 'upload_date': '20141207',
27 'uploader_id': '330537914540',
28 'uploader': 'Виталий Добровольский',
29 'like_count': int,
30 'age_limit': 0,
31 },
32 'skip': 'Video has been blocked',
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,
42 'upload_date': '20150518',
43 'uploader_id': '534380003155',
44 'uploader': '☭ Андрей Мещанинов ☭',
45 'like_count': int,
46 'age_limit': 0,
47 },
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 },
63 }, {
64 'url': 'http://ok.ru/web-api/video/moviePlayer/20079905452',
65 'only_matching': True,
66 }, {
67 'url': 'http://www.ok.ru/video/20648036891',
68 'only_matching': True,
69 }, {
70 'url': 'http://www.ok.ru/videoembed/20648036891',
71 'only_matching': True,
72 }, {
73 'url': 'http://m.ok.ru/video/20079905452',
74 'only_matching': True,
75 }, {
76 'url': 'http://mobile.ok.ru/video/20079905452',
77 'only_matching': True,
78 }]
79
80 def _real_extract(self, url):
81 video_id = self._match_id(url)
82
83 webpage = self._download_webpage(
84 'http://ok.ru/video/%s' % video_id, video_id)
85
86 error = self._search_regex(
87 r'[^>]+class="vp_video_stub_txt"[^>]*>([^<]+)<',
88 webpage, 'error', default=None)
89 if error:
90 raise ExtractorError(error, expected=True)
91
92 player = self._parse_json(
93 unescapeHTML(self._search_regex(
94 r'data-options=(?P<quote>["\'])(?P<player>{.+?%s.+?})(?P=quote)' % video_id,
95 webpage, 'player', group='player')),
96 video_id)
97
98 flashvars = player['flashvars']
99
100 metadata = flashvars.get('metadata')
101 if metadata:
102 metadata = self._parse_json(metadata, video_id)
103 else:
104 metadata = self._download_json(
105 compat_urllib_parse_unquote(flashvars['metadataUrl']),
106 video_id, 'Downloading metadata JSON')
107
108 movie = metadata['movie']
109 title = movie['title']
110 thumbnail = movie.get('poster')
111 duration = int_or_none(movie.get('duration'))
112
113 author = metadata.get('author', {})
114 uploader_id = author.get('id')
115 uploader = author.get('name')
116
117 upload_date = unified_strdate(self._html_search_meta(
118 'ya:ovs:upload_date', webpage, 'upload date', default=None))
119
120 age_limit = None
121 adult = self._html_search_meta(
122 'ya:ovs:adult', webpage, 'age limit', default=None)
123 if adult:
124 age_limit = 18 if adult == 'true' else 0
125
126 like_count = int_or_none(metadata.get('likeCount'))
127
128 info = {
129 'id': video_id,
130 'title': title,
131 'thumbnail': thumbnail,
132 'duration': duration,
133 'upload_date': upload_date,
134 'uploader': uploader,
135 'uploader_id': uploader_id,
136 'like_count': like_count,
137 'age_limit': age_limit,
138 }
139
140 if metadata.get('provider') == 'USER_YOUTUBE':
141 info.update({
142 '_type': 'url_transparent',
143 'url': movie['contentId'],
144 })
145 return info
146
147 quality = qualities(('mobile', 'lowest', 'low', 'sd', 'hd'))
148
149 formats = [{
150 'url': f['url'],
151 'ext': 'mp4',
152 'format_id': f['name'],
153 'quality': quality(f['name']),
154 } for f in metadata['videos']]
155 self._sort_formats(formats)
156
157 info['formats'] = formats
158 return info