cb2a4b0b54527b635e5255be38d98d3993e026c8
[youtube-dl] / youtube_dl / extractor / vine.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import itertools
6
7 from .common import InfoExtractor
8 from ..utils import (
9     int_or_none,
10     unified_strdate,
11 )
12
13
14 class VineIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:www\.)?vine\.co/(?:v|oembed)/(?P<id>\w+)'
16     _TESTS = [{
17         'url': 'https://vine.co/v/b9KOOWX7HUx',
18         'md5': '2f36fed6235b16da96ce9b4dc890940d',
19         'info_dict': {
20             'id': 'b9KOOWX7HUx',
21             'ext': 'mp4',
22             'title': 'Chicken.',
23             'alt_title': 'Vine by Jack Dorsey',
24             'upload_date': '20130519',
25             'uploader': 'Jack Dorsey',
26             'uploader_id': '76',
27             'like_count': int,
28             'comment_count': int,
29             'repost_count': int,
30         },
31     }, {
32         'url': 'https://vine.co/v/MYxVapFvz2z',
33         'md5': '7b9a7cbc76734424ff942eb52c8f1065',
34         'info_dict': {
35             'id': 'MYxVapFvz2z',
36             'ext': 'mp4',
37             'title': 'Fuck Da Police #Mikebrown #justice #ferguson #prayforferguson #protesting #NMOS14',
38             'alt_title': 'Vine by Mars Ruiz',
39             'upload_date': '20140815',
40             'uploader': 'Mars Ruiz',
41             'uploader_id': '1102363502380728320',
42             'like_count': int,
43             'comment_count': int,
44             'repost_count': int,
45         },
46     }, {
47         'url': 'https://vine.co/v/bxVjBbZlPUH',
48         'md5': 'ea27decea3fa670625aac92771a96b73',
49         'info_dict': {
50             'id': 'bxVjBbZlPUH',
51             'ext': 'mp4',
52             'title': '#mw3 #ac130 #killcam #angelofdeath',
53             'alt_title': 'Vine by Z3k3',
54             'upload_date': '20130430',
55             'uploader': 'Z3k3',
56             'uploader_id': '936470460173008896',
57             'like_count': int,
58             'comment_count': int,
59             'repost_count': int,
60         },
61     }, {
62         'url': 'https://vine.co/oembed/MYxVapFvz2z.json',
63         'only_matching': True,
64     }, {
65         'url': 'https://vine.co/v/e192BnZnZ9V',
66         'info_dict': {
67             'id': 'e192BnZnZ9V',
68             'ext': 'mp4',
69             'title': 'ยิ้ม~ เขิน~ อาย~ น่าร้ากอ้ะ >//< @n_whitewo @orlameena #lovesicktheseries  #lovesickseason2',
70             'alt_title': 'Vine by Pimry_zaa',
71             'upload_date': '20150705',
72             'uploader': 'Pimry_zaa',
73             'uploader_id': '1135760698325307392',
74             'like_count': int,
75             'comment_count': int,
76             'repost_count': int,
77         },
78         'params': {
79             'skip_download': True,
80         },
81     }]
82
83     def _real_extract(self, url):
84         video_id = self._match_id(url)
85         webpage = self._download_webpage('https://vine.co/v/' + video_id, video_id)
86
87         data = self._parse_json(
88             self._search_regex(
89                 r'window\.POST_DATA\s*=\s*{\s*%s\s*:\s*({.+?})\s*};\s*</script>' % video_id,
90                 webpage, 'vine data'),
91             video_id)
92
93         formats = [{
94             'format_id': '%(format)s-%(rate)s' % f,
95             'vcodec': f.get('format'),
96             'quality': f.get('rate'),
97             'url': f['videoUrl'],
98         } for f in data['videoUrls'] if f.get('videoUrl')]
99
100         self._sort_formats(formats)
101
102         username = data.get('username')
103
104         return {
105             'id': video_id,
106             'title': data.get('description') or self._og_search_title(webpage),
107             'alt_title': 'Vine by %s' % username if username else self._og_search_description(webpage, default=None),
108             'thumbnail': data.get('thumbnailUrl'),
109             'upload_date': unified_strdate(data.get('created')),
110             'uploader': username,
111             'uploader_id': data.get('userIdStr'),
112             'like_count': int_or_none(data.get('likes', {}).get('count')),
113             'comment_count': int_or_none(data.get('comments', {}).get('count')),
114             'repost_count': int_or_none(data.get('reposts', {}).get('count')),
115             'formats': formats,
116         }
117
118
119 class VineUserIE(InfoExtractor):
120     IE_NAME = 'vine:user'
121     _VALID_URL = r'(?:https?://)?vine\.co/(?P<u>u/)?(?P<user>[^/]+)/?(\?.*)?$'
122     _VINE_BASE_URL = "https://vine.co/"
123     _TESTS = [
124         {
125             'url': 'https://vine.co/Visa',
126             'info_dict': {
127                 'id': 'Visa',
128             },
129             'playlist_mincount': 46,
130         },
131         {
132             'url': 'https://vine.co/u/941705360593584128',
133             'only_matching': True,
134         },
135     ]
136
137     def _real_extract(self, url):
138         mobj = re.match(self._VALID_URL, url)
139         user = mobj.group('user')
140         u = mobj.group('u')
141
142         profile_url = "%sapi/users/profiles/%s%s" % (
143             self._VINE_BASE_URL, 'vanity/' if not u else '', user)
144         profile_data = self._download_json(
145             profile_url, user, note='Downloading user profile data')
146
147         user_id = profile_data['data']['userId']
148         timeline_data = []
149         for pagenum in itertools.count(1):
150             timeline_url = "%sapi/timelines/users/%s?page=%s&size=100" % (
151                 self._VINE_BASE_URL, user_id, pagenum)
152             timeline_page = self._download_json(
153                 timeline_url, user, note='Downloading page %d' % pagenum)
154             timeline_data.extend(timeline_page['data']['records'])
155             if timeline_page['data']['nextPage'] is None:
156                 break
157
158         entries = [
159             self.url_result(e['permalinkUrl'], 'Vine') for e in timeline_data]
160         return self.playlist_result(entries, user)