Merge remote-tracking branch 'origin/master'
[youtube-dl] / youtube_dl / extractor / odnoklassniki.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     unified_strdate,
7     int_or_none,
8     qualities,
9 )
10
11
12 class OdnoklassnikiIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:odnoklassniki|ok)\.ru/(?:video|web-api/video/moviePlayer)/(?P<id>\d+)'
14     _TESTS = [{
15         'url': 'http://ok.ru/video/20079905452',
16         'md5': '8e24ad2da6f387948e7a7d44eb8668fe',
17         'info_dict': {
18             'id': '20079905452',
19             'ext': 'mp4',
20             'title': 'Культура меняет нас (прекрасный ролик!))',
21             'duration': 100,
22             'upload_date': '20141207',
23             'uploader_id': '330537914540',
24             'uploader': 'Виталий Добровольский',
25             'like_count': int,
26             'age_limit': 0,
27         },
28     }, {
29         'url': 'http://ok.ru/web-api/video/moviePlayer/20079905452',
30         'only_matching': True,
31     }]
32
33     def _real_extract(self, url):
34         video_id = self._match_id(url)
35
36         webpage = self._download_webpage(url, video_id)
37
38         player = self._parse_json(
39             self._search_regex(
40                 r"OKVideo\.start\(({.+?})\s*,\s*'VideoAutoplay_player'", webpage, 'player'),
41             video_id)
42
43         metadata = self._parse_json(player['flashvars']['metadata'], video_id)
44
45         movie = metadata['movie']
46         title = movie['title']
47         thumbnail = movie.get('poster')
48         duration = int_or_none(movie.get('duration'))
49
50         author = metadata.get('author', {})
51         uploader_id = author.get('id')
52         uploader = author.get('name')
53
54         upload_date = unified_strdate(self._html_search_meta(
55             'ya:ovs:upload_date', webpage, 'upload date'))
56
57         age_limit = None
58         adult = self._html_search_meta(
59             'ya:ovs:adult', webpage, 'age limit')
60         if adult:
61             age_limit = 18 if adult == 'true' else 0
62
63         like_count = int_or_none(metadata.get('likeCount'))
64
65         quality = qualities(('mobile', 'lowest', 'low', 'sd', 'hd'))
66
67         formats = [{
68             'url': f['url'],
69             'ext': 'mp4',
70             'format_id': f['name'],
71             'quality': quality(f['name']),
72         } for f in metadata['videos']]
73
74         return {
75             'id': video_id,
76             'title': title,
77             'thumbnail': thumbnail,
78             'duration': duration,
79             'upload_date': upload_date,
80             'uploader': uploader,
81             'uploader_id': uploader_id,
82             'like_count': like_count,
83             'age_limit': age_limit,
84             'formats': formats,
85         }