]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/odnoklassniki.py
6c7149fe3859732978d34356f8c18aca14e74f63
[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
6 from ..utils import (
7 unified_strdate,
8 int_or_none,
9 qualities,
10 unescapeHTML,
11 )
12
13
14 class OdnoklassnikiIE(InfoExtractor):
15 _VALID_URL = r'https?://(?:odnoklassniki|ok)\.ru/(?:video|web-api/video/moviePlayer)/(?P<id>[\d-]+)'
16 _TESTS = [{
17 # metadata in JSON
18 'url': 'http://ok.ru/video/20079905452',
19 'md5': '8e24ad2da6f387948e7a7d44eb8668fe',
20 'info_dict': {
21 'id': '20079905452',
22 'ext': 'mp4',
23 'title': 'Культура меняет нас (прекрасный ролик!))',
24 'duration': 100,
25 'uploader_id': '330537914540',
26 'uploader': 'Виталий Добровольский',
27 'like_count': int,
28 },
29 }, {
30 # metadataUrl
31 'url': 'http://ok.ru/video/63567059965189-0',
32 'md5': '9676cf86eff5391d35dea675d224e131',
33 'info_dict': {
34 'id': '63567059965189-0',
35 'ext': 'mp4',
36 'title': 'Девушка без комплексов ...',
37 'duration': 191,
38 'uploader_id': '534380003155',
39 'uploader': 'Андрей Мещанинов',
40 'like_count': int,
41 },
42 }, {
43 'url': 'http://ok.ru/web-api/video/moviePlayer/20079905452',
44 'only_matching': True,
45 }]
46
47 def _real_extract(self, url):
48 video_id = self._match_id(url)
49
50 webpage = self._download_webpage(
51 'http://ok.ru/video/%s' % video_id, video_id)
52
53 player = self._parse_json(
54 unescapeHTML(self._search_regex(
55 r'data-attributes="([^"]+)"', webpage, 'player')),
56 video_id)
57
58 flashvars = player['flashvars']
59
60 metadata = flashvars.get('metadata')
61 if metadata:
62 metadata = self._parse_json(metadata, video_id)
63 else:
64 metadata = self._download_json(
65 compat_urllib_parse.unquote(flashvars['metadataUrl']),
66 video_id, 'Downloading metadata JSON')
67
68 movie = metadata['movie']
69 title = movie['title']
70 thumbnail = movie.get('poster')
71 duration = int_or_none(movie.get('duration'))
72
73 author = metadata.get('author', {})
74 uploader_id = author.get('id')
75 uploader = author.get('name')
76
77 upload_date = unified_strdate(self._html_search_meta(
78 'ya:ovs:upload_date', webpage, 'upload date', default=None))
79
80 age_limit = None
81 adult = self._html_search_meta(
82 'ya:ovs:adult', webpage, 'age limit', default=None)
83 if adult:
84 age_limit = 18 if adult == 'true' else 0
85
86 like_count = int_or_none(metadata.get('likeCount'))
87
88 quality = qualities(('mobile', 'lowest', 'low', 'sd', 'hd'))
89
90 formats = [{
91 'url': f['url'],
92 'ext': 'mp4',
93 'format_id': f['name'],
94 'quality': quality(f['name']),
95 } for f in metadata['videos']]
96
97 return {
98 'id': video_id,
99 'title': title,
100 'thumbnail': thumbnail,
101 'duration': duration,
102 'upload_date': upload_date,
103 'uploader': uploader,
104 'uploader_id': uploader_id,
105 'like_count': like_count,
106 'age_limit': age_limit,
107 'formats': formats,
108 }