]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/coub.py
[dplay] Add extractors for site changes (#2401)
[yt-dlp.git] / yt_dlp / extractor / coub.py
CommitLineData
11c70deb
S
1# coding: utf-8
2from __future__ import unicode_literals
3
4from .common import InfoExtractor
5from ..utils import (
6 ExtractorError,
7 float_or_none,
8 int_or_none,
9 parse_iso8601,
10 qualities,
11)
12
13
14class CoubIE(InfoExtractor):
15 _VALID_URL = r'(?:coub:|https?://(?:coub\.com/(?:view|embed|coubs)/|c-cdn\.coub\.com/fb-player\.swf\?.*\bcoub(?:ID|id)=))(?P<id>[\da-z]+)'
16
17 _TESTS = [{
18 'url': 'http://coub.com/view/5u5n1',
19 'info_dict': {
20 'id': '5u5n1',
21 'ext': 'mp4',
22 'title': 'The Matrix Moonwalk',
ec85ded8 23 'thumbnail': r're:^https?://.*\.jpg$',
11c70deb
S
24 'duration': 4.6,
25 'timestamp': 1428527772,
26 'upload_date': '20150408',
a146fa1c 27 'uploader': 'Artyom Loskutnikov',
11c70deb
S
28 'uploader_id': 'artyom.loskutnikov',
29 'view_count': int,
30 'like_count': int,
31 'repost_count': int,
11c70deb
S
32 'age_limit': 0,
33 },
34 }, {
35 'url': 'http://c-cdn.coub.com/fb-player.swf?bot_type=vk&coubID=7w5a4',
36 'only_matching': True,
37 }, {
38 'url': 'coub:5u5n1',
39 'only_matching': True,
de7d76af
S
40 }, {
41 # longer video id
42 'url': 'http://coub.com/view/237d5l5h',
43 'only_matching': True,
11c70deb
S
44 }]
45
46 def _real_extract(self, url):
47 video_id = self._match_id(url)
48
49 coub = self._download_json(
50 'http://coub.com/api/v2/coubs/%s.json' % video_id, video_id)
51
52 if coub.get('error'):
53 raise ExtractorError(
54 '%s said: %s' % (self.IE_NAME, coub['error']), expected=True)
55
56 title = coub['title']
57
58 file_versions = coub['file_versions']
59
aeaf3b2b 60 QUALITIES = ('low', 'med', 'high', 'higher')
11c70deb
S
61
62 MOBILE = 'mobile'
63 IPHONE = 'iphone'
64 HTML5 = 'html5'
65
66 SOURCE_PREFERENCE = (MOBILE, IPHONE, HTML5)
67
68 quality_key = qualities(QUALITIES)
69 preference_key = qualities(SOURCE_PREFERENCE)
70
71 formats = []
72
73 for kind, items in file_versions.get(HTML5, {}).items():
74 if kind not in ('video', 'audio'):
75 continue
76 if not isinstance(items, dict):
77 continue
78 for quality, item in items.items():
79 if not isinstance(item, dict):
80 continue
81 item_url = item.get('url')
82 if not item_url:
83 continue
84 formats.append({
85 'url': item_url,
86 'format_id': '%s-%s-%s' % (HTML5, kind, quality),
87 'filesize': int_or_none(item.get('size')),
88 'vcodec': 'none' if kind == 'audio' else None,
aeaf3b2b 89 'acodec': 'none' if kind == 'video' else None,
11c70deb 90 'quality': quality_key(quality),
f983b875 91 'source_preference': preference_key(HTML5),
11c70deb
S
92 })
93
94 iphone_url = file_versions.get(IPHONE, {}).get('url')
95 if iphone_url:
96 formats.append({
97 'url': iphone_url,
98 'format_id': IPHONE,
f983b875 99 'source_preference': preference_key(IPHONE),
11c70deb
S
100 })
101
102 mobile_url = file_versions.get(MOBILE, {}).get('audio_url')
103 if mobile_url:
104 formats.append({
105 'url': mobile_url,
106 'format_id': '%s-audio' % MOBILE,
f983b875 107 'source_preference': preference_key(MOBILE),
11c70deb
S
108 })
109
110 self._sort_formats(formats)
111
112 thumbnail = coub.get('picture')
113 duration = float_or_none(coub.get('duration'))
114 timestamp = parse_iso8601(coub.get('published_at') or coub.get('created_at'))
115 uploader = coub.get('channel', {}).get('title')
116 uploader_id = coub.get('channel', {}).get('permalink')
117
118 view_count = int_or_none(coub.get('views_count') or coub.get('views_increase_count'))
119 like_count = int_or_none(coub.get('likes_count'))
120 repost_count = int_or_none(coub.get('recoubs_count'))
11c70deb
S
121
122 age_restricted = coub.get('age_restricted', coub.get('age_restricted_by_admin'))
123 if age_restricted is not None:
124 age_limit = 18 if age_restricted is True else 0
125 else:
126 age_limit = None
127
128 return {
129 'id': video_id,
130 'title': title,
131 'thumbnail': thumbnail,
132 'duration': duration,
133 'timestamp': timestamp,
134 'uploader': uploader,
135 'uploader_id': uploader_id,
136 'view_count': view_count,
137 'like_count': like_count,
138 'repost_count': repost_count,
11c70deb
S
139 'age_limit': age_limit,
140 'formats': formats,
141 }