]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/coub.py
[coub] Add extractor (Closes #9609)
[yt-dlp.git] / youtube_dl / 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',
23 'thumbnail': 're:^https?://.*\.jpg$',
24 'duration': 4.6,
25 'timestamp': 1428527772,
26 'upload_date': '20150408',
27 'uploader': 'Артём Лоскутников',
28 'uploader_id': 'artyom.loskutnikov',
29 'view_count': int,
30 'like_count': int,
31 'repost_count': int,
32 'comment_count': int,
33 'age_limit': 0,
34 },
35 }, {
36 'url': 'http://c-cdn.coub.com/fb-player.swf?bot_type=vk&coubID=7w5a4',
37 'only_matching': True,
38 }, {
39 'url': 'coub:5u5n1',
40 'only_matching': True,
41 }]
42
43 def _real_extract(self, url):
44 video_id = self._match_id(url)
45
46 coub = self._download_json(
47 'http://coub.com/api/v2/coubs/%s.json' % video_id, video_id)
48
49 if coub.get('error'):
50 raise ExtractorError(
51 '%s said: %s' % (self.IE_NAME, coub['error']), expected=True)
52
53 title = coub['title']
54
55 file_versions = coub['file_versions']
56
57 QUALITIES = ('low', 'med', 'high')
58
59 MOBILE = 'mobile'
60 IPHONE = 'iphone'
61 HTML5 = 'html5'
62
63 SOURCE_PREFERENCE = (MOBILE, IPHONE, HTML5)
64
65 quality_key = qualities(QUALITIES)
66 preference_key = qualities(SOURCE_PREFERENCE)
67
68 formats = []
69
70 for kind, items in file_versions.get(HTML5, {}).items():
71 if kind not in ('video', 'audio'):
72 continue
73 if not isinstance(items, dict):
74 continue
75 for quality, item in items.items():
76 if not isinstance(item, dict):
77 continue
78 item_url = item.get('url')
79 if not item_url:
80 continue
81 formats.append({
82 'url': item_url,
83 'format_id': '%s-%s-%s' % (HTML5, kind, quality),
84 'filesize': int_or_none(item.get('size')),
85 'vcodec': 'none' if kind == 'audio' else None,
86 'quality': quality_key(quality),
87 'preference': preference_key(HTML5),
88 })
89
90 iphone_url = file_versions.get(IPHONE, {}).get('url')
91 if iphone_url:
92 formats.append({
93 'url': iphone_url,
94 'format_id': IPHONE,
95 'preference': preference_key(IPHONE),
96 })
97
98 mobile_url = file_versions.get(MOBILE, {}).get('audio_url')
99 if mobile_url:
100 formats.append({
101 'url': mobile_url,
102 'format_id': '%s-audio' % MOBILE,
103 'preference': preference_key(MOBILE),
104 })
105
106 self._sort_formats(formats)
107
108 thumbnail = coub.get('picture')
109 duration = float_or_none(coub.get('duration'))
110 timestamp = parse_iso8601(coub.get('published_at') or coub.get('created_at'))
111 uploader = coub.get('channel', {}).get('title')
112 uploader_id = coub.get('channel', {}).get('permalink')
113
114 view_count = int_or_none(coub.get('views_count') or coub.get('views_increase_count'))
115 like_count = int_or_none(coub.get('likes_count'))
116 repost_count = int_or_none(coub.get('recoubs_count'))
117 comment_count = int_or_none(coub.get('comments_count'))
118
119 age_restricted = coub.get('age_restricted', coub.get('age_restricted_by_admin'))
120 if age_restricted is not None:
121 age_limit = 18 if age_restricted is True else 0
122 else:
123 age_limit = None
124
125 return {
126 'id': video_id,
127 'title': title,
128 'thumbnail': thumbnail,
129 'duration': duration,
130 'timestamp': timestamp,
131 'uploader': uploader,
132 'uploader_id': uploader_id,
133 'view_count': view_count,
134 'like_count': like_count,
135 'repost_count': repost_count,
136 'comment_count': comment_count,
137 'age_limit': age_limit,
138 'formats': formats,
139 }