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