]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/odnoklassniki.py
[odnoklassniki] Support extraction from metadata URL (Closes #5813)
[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
c6bbdadd 5from ..compat import compat_urllib_parse
4ffbf778
S
6from ..utils import (
7 unified_strdate,
8 int_or_none,
9 qualities,
372744c5 10 unescapeHTML,
4ffbf778
S
11)
12
13
14class OdnoklassnikiIE(InfoExtractor):
c6bbdadd 15 _VALID_URL = r'https?://(?:odnoklassniki|ok)\.ru/(?:video|web-api/video/moviePlayer)/(?P<id>[\d-]+)'
4ffbf778 16 _TESTS = [{
c6bbdadd 17 # metadata in JSON
4ffbf778
S
18 'url': 'http://ok.ru/video/20079905452',
19 'md5': '8e24ad2da6f387948e7a7d44eb8668fe',
20 'info_dict': {
21 'id': '20079905452',
22 'ext': 'mp4',
23 'title': 'Культура меняет нас (прекрасный ролик!))',
24 'duration': 100,
4ffbf778
S
25 'uploader_id': '330537914540',
26 'uploader': 'Виталий Добровольский',
27 'like_count': int,
c6bbdadd
S
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,
4ffbf778
S
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(url, video_id)
51
52 player = self._parse_json(
372744c5
S
53 unescapeHTML(self._search_regex(
54 r'data-attributes="([^"]+)"', webpage, 'player')),
4ffbf778
S
55 video_id)
56
c6bbdadd
S
57 flashvars = player['flashvars']
58
59 metadata = flashvars.get('metadata')
60 if metadata:
61 metadata = self._parse_json(metadata, video_id)
62 else:
63 metadata = self._download_json(
64 compat_urllib_parse.unquote(flashvars['metadataUrl']),
65 video_id, 'Downloading metadata JSON')
4ffbf778
S
66
67 movie = metadata['movie']
68 title = movie['title']
69 thumbnail = movie.get('poster')
70 duration = int_or_none(movie.get('duration'))
71
72 author = metadata.get('author', {})
73 uploader_id = author.get('id')
74 uploader = author.get('name')
75
76 upload_date = unified_strdate(self._html_search_meta(
c6bbdadd 77 'ya:ovs:upload_date', webpage, 'upload date', default=None))
4ffbf778
S
78
79 age_limit = None
80 adult = self._html_search_meta(
c6bbdadd 81 'ya:ovs:adult', webpage, 'age limit', default=None)
4ffbf778
S
82 if adult:
83 age_limit = 18 if adult == 'true' else 0
84
85 like_count = int_or_none(metadata.get('likeCount'))
86
87 quality = qualities(('mobile', 'lowest', 'low', 'sd', 'hd'))
88
89 formats = [{
90 'url': f['url'],
91 'ext': 'mp4',
92 'format_id': f['name'],
93 'quality': quality(f['name']),
94 } for f in metadata['videos']]
95
96 return {
97 'id': video_id,
98 'title': title,
99 'thumbnail': thumbnail,
100 'duration': duration,
101 'upload_date': upload_date,
102 'uploader': uploader,
103 'uploader_id': uploader_id,
104 'like_count': like_count,
105 'age_limit': age_limit,
106 'formats': formats,
107 }