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': 'Артём Лоскутников',
28 'uploader_id': 'artyom.loskutnikov',
36 'url': 'http://c-cdn.coub.com/fb-player.swf?bot_type=vk&coubID=7w5a4',
37 'only_matching': True,
40 'only_matching': True,
43 'url': 'http://coub.com/view/237d5l5h',
44 'only_matching': True,
47 def _real_extract(self, url):
48 video_id = self._match_id(url)
50 coub = self._download_json(
51 'http://coub.com/api/v2/coubs/%s.json' % video_id, video_id)
55 '%s said: %s' % (self.IE_NAME, coub['error']), expected=True)
59 file_versions = coub['file_versions']
61 QUALITIES = ('low', 'med', 'high')
67 SOURCE_PREFERENCE = (MOBILE, IPHONE, HTML5)
69 quality_key = qualities(QUALITIES)
70 preference_key = qualities(SOURCE_PREFERENCE)
74 for kind, items in file_versions.get(HTML5, {}).items():
75 if kind not in ('video', 'audio'):
77 if not isinstance(items, dict):
79 for quality, item in items.items():
80 if not isinstance(item, dict):
82 item_url = item.get('url')
87 'format_id': '%s-%s-%s' % (HTML5, kind, quality),
88 'filesize': int_or_none(item.get('size')),
89 'vcodec': 'none' if kind == 'audio' else None,
90 'quality': quality_key(quality),
91 'preference': preference_key(HTML5),
94 iphone_url = file_versions.get(IPHONE, {}).get('url')
99 'preference': preference_key(IPHONE),
102 mobile_url = file_versions.get(MOBILE, {}).get('audio_url')
106 'format_id': '%s-audio' % MOBILE,
107 'preference': preference_key(MOBILE),
110 self._sort_formats(formats)
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')
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'))
121 comment_count = int_or_none(coub.get('comments_count'))
123 age_restricted = coub.get('age_restricted', coub.get('age_restricted_by_admin'))
124 if age_restricted is not None:
125 age_limit = 18 if age_restricted is True else 0
132 'thumbnail': thumbnail,
133 'duration': duration,
134 'timestamp': timestamp,
135 'uploader': uploader,
136 'uploader_id': uploader_id,
137 'view_count': view_count,
138 'like_count': like_count,
139 'repost_count': repost_count,
140 'comment_count': comment_count,
141 'age_limit': age_limit,