[vine] Remove duplicate metadata, make more robust and modernize (Closes #7215)
[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         },
28     }, {
29         'url': 'https://vine.co/v/MYxVapFvz2z',
30         'md5': '7b9a7cbc76734424ff942eb52c8f1065',
31         'info_dict': {
32             'id': 'MYxVapFvz2z',
33             'ext': 'mp4',
34             'title': 'Fuck Da Police #Mikebrown #justice #ferguson #prayforferguson #protesting #NMOS14',
35             'alt_title': 'Vine by Mars Ruiz',
36             'upload_date': '20140815',
37             'uploader': 'Mars Ruiz',
38             'uploader_id': '1102363502380728320',
39         },
40     }, {
41         'url': 'https://vine.co/v/bxVjBbZlPUH',
42         'md5': 'ea27decea3fa670625aac92771a96b73',
43         'info_dict': {
44             'id': 'bxVjBbZlPUH',
45             'ext': 'mp4',
46             'title': '#mw3 #ac130 #killcam #angelofdeath',
47             'alt_title': 'Vine by Z3k3',
48             'upload_date': '20130430',
49             'uploader': 'Z3k3',
50             'uploader_id': '936470460173008896',
51         },
52     }, {
53         'url': 'https://vine.co/oembed/MYxVapFvz2z.json',
54         'only_matching': True,
55     }, {
56         'url': 'https://vine.co/v/e192BnZnZ9V',
57         'info_dict': {
58             'id': 'e192BnZnZ9V',
59             'ext': 'mp4',
60             'title': 'ยิ้ม~ เขิน~ อาย~ น่าร้ากอ้ะ >//< @n_whitewo @orlameena #lovesicktheseries  #lovesickseason2',
61             'alt_title': 'Vine by Pimry_zaa',
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.get('format'),
84             'quality': f.get('rate'),
85             'url': f['videoUrl'],
86         } for f in data['videoUrls'] if f.get('videoUrl')]
87
88         self._sort_formats(formats)
89
90         username = data.get('username')
91
92         return {
93             'id': video_id,
94             'title': data.get('description') or self._og_search_title(webpage),
95             'alt_title': 'Vine by %s' % username if username else self._og_search_description(webpage, default=None),
96             'thumbnail': data.get('thumbnailUrl'),
97             'upload_date': unified_strdate(data.get('created')),
98             'uploader': username,
99             'uploader_id': data.get('userIdStr'),
100             'like_count': int_or_none(data.get('likes', {}).get('count')),
101             'comment_count': int_or_none(data.get('comments', {}).get('count')),
102             'repost_count': int_or_none(data.get('reposts', {}).get('count')),
103             'formats': formats,
104         }
105
106
107 class VineUserIE(InfoExtractor):
108     IE_NAME = 'vine:user'
109     _VALID_URL = r'(?:https?://)?vine\.co/(?P<u>u/)?(?P<user>[^/]+)/?(\?.*)?$'
110     _VINE_BASE_URL = "https://vine.co/"
111     _TESTS = [
112         {
113             'url': 'https://vine.co/Visa',
114             'info_dict': {
115                 'id': 'Visa',
116             },
117             'playlist_mincount': 46,
118         },
119         {
120             'url': 'https://vine.co/u/941705360593584128',
121             'only_matching': True,
122         },
123     ]
124
125     def _real_extract(self, url):
126         mobj = re.match(self._VALID_URL, url)
127         user = mobj.group('user')
128         u = mobj.group('u')
129
130         profile_url = "%sapi/users/profiles/%s%s" % (
131             self._VINE_BASE_URL, 'vanity/' if not u else '', user)
132         profile_data = self._download_json(
133             profile_url, user, note='Downloading user profile data')
134
135         user_id = profile_data['data']['userId']
136         timeline_data = []
137         for pagenum in itertools.count(1):
138             timeline_url = "%sapi/timelines/users/%s?page=%s&size=100" % (
139                 self._VINE_BASE_URL, user_id, pagenum)
140             timeline_page = self._download_json(
141                 timeline_url, user, note='Downloading page %d' % pagenum)
142             timeline_data.extend(timeline_page['data']['records'])
143             if timeline_page['data']['nextPage'] is None:
144                 break
145
146         entries = [
147             self.url_result(e['permalinkUrl'], 'Vine') for e in timeline_data]
148         return self.playlist_result(entries, user)