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