]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/coub.py
Update list of supported sites
[youtubedl] / youtube_dl / extractor / coub.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6 ExtractorError,
7 float_or_none,
8 int_or_none,
9 parse_iso8601,
10 qualities,
11 )
12
13
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]+)'
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 # longer video id
43 'url': 'http://coub.com/view/237d5l5h',
44 'only_matching': True,
45 }]
46
47 def _real_extract(self, url):
48 video_id = self._match_id(url)
49
50 coub = self._download_json(
51 'http://coub.com/api/v2/coubs/%s.json' % video_id, video_id)
52
53 if coub.get('error'):
54 raise ExtractorError(
55 '%s said: %s' % (self.IE_NAME, coub['error']), expected=True)
56
57 title = coub['title']
58
59 file_versions = coub['file_versions']
60
61 QUALITIES = ('low', 'med', 'high')
62
63 MOBILE = 'mobile'
64 IPHONE = 'iphone'
65 HTML5 = 'html5'
66
67 SOURCE_PREFERENCE = (MOBILE, IPHONE, HTML5)
68
69 quality_key = qualities(QUALITIES)
70 preference_key = qualities(SOURCE_PREFERENCE)
71
72 formats = []
73
74 for kind, items in file_versions.get(HTML5, {}).items():
75 if kind not in ('video', 'audio'):
76 continue
77 if not isinstance(items, dict):
78 continue
79 for quality, item in items.items():
80 if not isinstance(item, dict):
81 continue
82 item_url = item.get('url')
83 if not item_url:
84 continue
85 formats.append({
86 'url': item_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),
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,
99 'preference': preference_key(IPHONE),
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,
107 'preference': preference_key(MOBILE),
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'))
121 comment_count = int_or_none(coub.get('comments_count'))
122
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
126 else:
127 age_limit = None
128
129 return {
130 'id': video_id,
131 'title': title,
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,
142 'formats': formats,
143 }