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