2 from __future__ import unicode_literals
4 from .common import InfoExtractor
14 class 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]+)'
18 'url': 'http://coub.com/view/5u5n1',
22 'title': 'The Matrix Moonwalk',
23 'thumbnail': r're:^https?://.*\.jpg$',
25 'timestamp': 1428527772,
26 'upload_date': '20150408',
27 'uploader': 'Artyom Loskutnikov',
28 'uploader_id': 'artyom.loskutnikov',
35 'url': 'http://c-cdn.coub.com/fb-player.swf?bot_type=vk&coubID=7w5a4',
36 'only_matching': True,
39 'only_matching': True,
42 'url': 'http://coub.com/view/237d5l5h',
43 'only_matching': True,
46 def _real_extract(self, url):
47 video_id = self._match_id(url)
49 coub = self._download_json(
50 'http://coub.com/api/v2/coubs/%s.json' % video_id, video_id)
54 '%s said: %s' % (self.IE_NAME, coub['error']), expected=True)
58 file_versions = coub['file_versions']
60 QUALITIES = ('low', 'med', 'high')
66 SOURCE_PREFERENCE = (MOBILE, IPHONE, HTML5)
68 quality_key = qualities(QUALITIES)
69 preference_key = qualities(SOURCE_PREFERENCE)
73 for kind, items in file_versions.get(HTML5, {}).items():
74 if kind not in ('video', 'audio'):
76 if not isinstance(items, dict):
78 for quality, item in items.items():
79 if not isinstance(item, dict):
81 item_url = item.get('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,
89 'quality': quality_key(quality),
90 'preference': preference_key(HTML5),
93 iphone_url = file_versions.get(IPHONE, {}).get('url')
98 'preference': preference_key(IPHONE),
101 mobile_url = file_versions.get(MOBILE, {}).get('audio_url')
105 'format_id': '%s-audio' % MOBILE,
106 'preference': preference_key(MOBILE),
109 self._sort_formats(formats)
111 thumbnail = coub.get('picture')
112 duration = float_or_none(coub.get('duration'))
113 timestamp = parse_iso8601(coub.get('published_at') or coub.get('created_at'))
114 uploader = coub.get('channel', {}).get('title')
115 uploader_id = coub.get('channel', {}).get('permalink')
117 view_count = int_or_none(coub.get('views_count') or coub.get('views_increase_count'))
118 like_count = int_or_none(coub.get('likes_count'))
119 repost_count = int_or_none(coub.get('recoubs_count'))
121 age_restricted = coub.get('age_restricted', coub.get('age_restricted_by_admin'))
122 if age_restricted is not None:
123 age_limit = 18 if age_restricted is True else 0
130 'thumbnail': thumbnail,
131 'duration': duration,
132 'timestamp': timestamp,
133 'uploader': uploader,
134 'uploader_id': uploader_id,
135 'view_count': view_count,
136 'like_count': like_count,
137 'repost_count': repost_count,
138 'age_limit': age_limit,