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