]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/coub.py
[ie/youtube] Suppress "Unavailable videos are hidden" warning (#10159)
[yt-dlp.git] / yt_dlp / extractor / coub.py
1 from .common import InfoExtractor
2 from ..utils import (
3 ExtractorError,
4 float_or_none,
5 int_or_none,
6 parse_iso8601,
7 qualities,
8 )
9
10
11 class CoubIE(InfoExtractor):
12 _VALID_URL = r'(?:coub:|https?://(?:coub\.com/(?:view|embed|coubs)/|c-cdn\.coub\.com/fb-player\.swf\?.*\bcoub(?:ID|id)=))(?P<id>[\da-z]+)'
13
14 _TESTS = [{
15 'url': 'http://coub.com/view/5u5n1',
16 'info_dict': {
17 'id': '5u5n1',
18 'ext': 'mp4',
19 'title': 'The Matrix Moonwalk',
20 'thumbnail': r're:^https?://.*\.jpg$',
21 'duration': 4.6,
22 'timestamp': 1428527772,
23 'upload_date': '20150408',
24 'uploader': 'Artyom Loskutnikov',
25 'uploader_id': 'artyom.loskutnikov',
26 'view_count': int,
27 'like_count': int,
28 'repost_count': int,
29 'age_limit': 0,
30 },
31 }, {
32 'url': 'http://c-cdn.coub.com/fb-player.swf?bot_type=vk&coubID=7w5a4',
33 'only_matching': True,
34 }, {
35 'url': 'coub:5u5n1',
36 'only_matching': True,
37 }, {
38 # longer video id
39 'url': 'http://coub.com/view/237d5l5h',
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 f'http://coub.com/api/v2/coubs/{video_id}.json', video_id)
48
49 if coub.get('error'):
50 raise ExtractorError(
51 '{} said: {}'.format(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', 'higher')
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': f'{HTML5}-{kind}-{quality}',
84 'filesize': int_or_none(item.get('size')),
85 'vcodec': 'none' if kind == 'audio' else None,
86 'acodec': 'none' if kind == 'video' else None,
87 'quality': quality_key(quality),
88 'source_preference': preference_key(HTML5),
89 })
90
91 iphone_url = file_versions.get(IPHONE, {}).get('url')
92 if iphone_url:
93 formats.append({
94 'url': iphone_url,
95 'format_id': IPHONE,
96 'source_preference': preference_key(IPHONE),
97 })
98
99 mobile_url = file_versions.get(MOBILE, {}).get('audio_url')
100 if mobile_url:
101 formats.append({
102 'url': mobile_url,
103 'format_id': f'{MOBILE}-audio',
104 'source_preference': preference_key(MOBILE),
105 })
106
107 thumbnail = coub.get('picture')
108 duration = float_or_none(coub.get('duration'))
109 timestamp = parse_iso8601(coub.get('published_at') or coub.get('created_at'))
110 uploader = coub.get('channel', {}).get('title')
111 uploader_id = coub.get('channel', {}).get('permalink')
112
113 view_count = int_or_none(coub.get('views_count') or coub.get('views_increase_count'))
114 like_count = int_or_none(coub.get('likes_count'))
115 repost_count = int_or_none(coub.get('recoubs_count'))
116
117 age_restricted = coub.get('age_restricted', coub.get('age_restricted_by_admin'))
118 if age_restricted is not None:
119 age_limit = 18 if age_restricted is True else 0
120 else:
121 age_limit = None
122
123 return {
124 'id': video_id,
125 'title': title,
126 'thumbnail': thumbnail,
127 'duration': duration,
128 'timestamp': timestamp,
129 'uploader': uploader,
130 'uploader_id': uploader_id,
131 'view_count': view_count,
132 'like_count': like_count,
133 'repost_count': repost_count,
134 'age_limit': age_limit,
135 'formats': formats,
136 }