]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/vine.py
[extractors] Use new framework for existing embeds (#4307)
[yt-dlp.git] / yt_dlp / extractor / vine.py
1 from .common import InfoExtractor
2 from ..compat import compat_str
3 from ..utils import (
4 determine_ext,
5 format_field,
6 int_or_none,
7 unified_timestamp,
8 )
9
10
11 class VineIE(InfoExtractor):
12 _VALID_URL = r'https?://(?:www\.)?vine\.co/(?:v|oembed)/(?P<id>\w+)'
13 _EMBED_REGEX = [r'<iframe[^>]+src=[\'"](?P<url>(?:https?:)?//(?:www\.)?vine\.co/v/[^/]+/embed/(?:simple|postcard))']
14 _TESTS = [{
15 'url': 'https://vine.co/v/b9KOOWX7HUx',
16 'md5': '2f36fed6235b16da96ce9b4dc890940d',
17 'info_dict': {
18 'id': 'b9KOOWX7HUx',
19 'ext': 'mp4',
20 'title': 'Chicken.',
21 'alt_title': 'Vine by Jack',
22 'timestamp': 1368997951,
23 'upload_date': '20130519',
24 'uploader': 'Jack',
25 'uploader_id': '76',
26 'view_count': int,
27 'like_count': int,
28 'comment_count': int,
29 'repost_count': int,
30 },
31 }, {
32 'url': 'https://vine.co/v/e192BnZnZ9V',
33 'info_dict': {
34 'id': 'e192BnZnZ9V',
35 'ext': 'mp4',
36 'title': 'ยิ้ม~ เขิน~ อาย~ น่าร้ากอ้ะ >//< @n_whitewo @orlameena #lovesicktheseries #lovesickseason2',
37 'alt_title': 'Vine by Pimry_zaa',
38 'timestamp': 1436057405,
39 'upload_date': '20150705',
40 'uploader': 'Pimry_zaa',
41 'uploader_id': '1135760698325307392',
42 'view_count': int,
43 'like_count': int,
44 'comment_count': int,
45 'repost_count': int,
46 },
47 'params': {
48 'skip_download': True,
49 },
50 }, {
51 'url': 'https://vine.co/v/MYxVapFvz2z',
52 'only_matching': True,
53 }, {
54 'url': 'https://vine.co/v/bxVjBbZlPUH',
55 'only_matching': True,
56 }, {
57 'url': 'https://vine.co/oembed/MYxVapFvz2z.json',
58 'only_matching': True,
59 }]
60
61 def _real_extract(self, url):
62 video_id = self._match_id(url)
63
64 data = self._download_json(
65 'https://archive.vine.co/posts/%s.json' % video_id, video_id)
66
67 def video_url(kind):
68 for url_suffix in ('Url', 'URL'):
69 format_url = data.get('video%s%s' % (kind, url_suffix))
70 if format_url:
71 return format_url
72
73 formats = []
74 for quality, format_id in enumerate(('low', '', 'dash')):
75 format_url = video_url(format_id.capitalize())
76 if not format_url:
77 continue
78 # DASH link returns plain mp4
79 if format_id == 'dash' and determine_ext(format_url) == 'mpd':
80 formats.extend(self._extract_mpd_formats(
81 format_url, video_id, mpd_id='dash', fatal=False))
82 else:
83 formats.append({
84 'url': format_url,
85 'format_id': format_id or 'standard',
86 'quality': quality,
87 })
88 self._check_formats(formats, video_id)
89 self._sort_formats(formats)
90
91 username = data.get('username')
92
93 alt_title = format_field(username, None, 'Vine by %s')
94
95 return {
96 'id': video_id,
97 'title': data.get('description') or alt_title or 'Vine video',
98 'alt_title': alt_title,
99 'thumbnail': data.get('thumbnailUrl'),
100 'timestamp': unified_timestamp(data.get('created')),
101 'uploader': username,
102 'uploader_id': data.get('userIdStr'),
103 'view_count': int_or_none(data.get('loops')),
104 'like_count': int_or_none(data.get('likes')),
105 'comment_count': int_or_none(data.get('comments')),
106 'repost_count': int_or_none(data.get('reposts')),
107 'formats': formats,
108 }
109
110
111 class VineUserIE(InfoExtractor):
112 IE_NAME = 'vine:user'
113 _VALID_URL = r'https?://vine\.co/(?P<u>u/)?(?P<user>[^/]+)'
114 _VINE_BASE_URL = 'https://vine.co/'
115 _TESTS = [{
116 'url': 'https://vine.co/itsruthb',
117 'info_dict': {
118 'id': 'itsruthb',
119 'title': 'Ruth B',
120 'description': '| Instagram/Twitter: itsruthb | still a lost boy from neverland',
121 },
122 'playlist_mincount': 611,
123 }, {
124 'url': 'https://vine.co/u/942914934646415360',
125 'only_matching': True,
126 }]
127
128 @classmethod
129 def suitable(cls, url):
130 return False if VineIE.suitable(url) else super(VineUserIE, cls).suitable(url)
131
132 def _real_extract(self, url):
133 mobj = self._match_valid_url(url)
134 user = mobj.group('user')
135 u = mobj.group('u')
136
137 profile_url = '%sapi/users/profiles/%s%s' % (
138 self._VINE_BASE_URL, 'vanity/' if not u else '', user)
139 profile_data = self._download_json(
140 profile_url, user, note='Downloading user profile data')
141
142 data = profile_data['data']
143 user_id = data.get('userId') or data['userIdStr']
144 profile = self._download_json(
145 'https://archive.vine.co/profiles/%s.json' % user_id, user_id)
146 entries = [
147 self.url_result(
148 'https://vine.co/v/%s' % post_id, ie='Vine', video_id=post_id)
149 for post_id in profile['posts']
150 if post_id and isinstance(post_id, compat_str)]
151 return self.playlist_result(
152 entries, user, profile.get('username'), profile.get('description'))