]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/vine.py
[nrk:playlist] Relax video id regex and improve _VALID_URL
[yt-dlp.git] / youtube_dl / extractor / vine.py
CommitLineData
5dc733f0
JMF
1from __future__ import unicode_literals
2
eb1634cb 3import re
f919201e 4import json
ea783d01 5import itertools
eb1634cb
PH
6
7from .common import InfoExtractor
f919201e 8from ..utils import unified_strdate
eb1634cb
PH
9
10
11class VineIE(InfoExtractor):
5dc733f0 12 _VALID_URL = r'https?://(?:www\.)?vine\.co/v/(?P<id>\w+)'
6f5ac90c 13 _TEST = {
5dc733f0
JMF
14 'url': 'https://vine.co/v/b9KOOWX7HUx',
15 'md5': '2f36fed6235b16da96ce9b4dc890940d',
16 'info_dict': {
17 'id': 'b9KOOWX7HUx',
18 'ext': 'mp4',
5dc733f0 19 'title': 'Chicken.',
f5e43bc6 20 'alt_title': 'Vine by Jack Dorsey',
f919201e
S
21 'description': 'Chicken.',
22 'upload_date': '20130519',
23 'uploader': 'Jack Dorsey',
24 'uploader_id': '76',
5dc733f0 25 },
6f5ac90c 26 }
eb1634cb
PH
27
28 def _real_extract(self, url):
63e0f295 29 video_id = self._match_id(url)
f919201e 30 webpage = self._download_webpage('https://vine.co/v/' + video_id, video_id)
eb1634cb 31
f919201e
S
32 data = json.loads(self._html_search_regex(
33 r'window\.POST_DATA = { %s: ({.+?}) }' % video_id, webpage, 'vine data'))
0049594e 34
63e0f295 35 formats = [{
2684871b
NJ
36 'format_id': '%(format)s-%(rate)s' % f,
37 'vcodec': f['format'],
38 'quality': f['rate'],
39 'url': f['videoUrl'],
40 } for f in data['videoUrls'] if f.get('rate')]
41
42 self._sort_formats(formats)
eb1634cb 43
5dc733f0
JMF
44 return {
45 'id': video_id,
5dc733f0 46 'title': self._og_search_title(webpage),
f5e43bc6 47 'alt_title': self._og_search_description(webpage),
f919201e
S
48 'description': data['description'],
49 'thumbnail': data['thumbnailUrl'],
50 'upload_date': unified_strdate(data['created']),
51 'uploader': data['username'],
52 'uploader_id': data['userIdStr'],
53 'like_count': data['likes']['count'],
54 'comment_count': data['comments']['count'],
55 'repost_count': data['reposts']['count'],
56 'formats': formats,
acd69589 57 }
ea783d01 58
a172b258 59
ea783d01
JN
60class VineUserIE(InfoExtractor):
61 IE_NAME = 'vine:user'
b128c9ed 62 _VALID_URL = r'(?:https?://)?vine\.co/(?P<u>u/)?(?P<user>[^/]+)/?(\?.*)?$'
ea783d01 63 _VINE_BASE_URL = "https://vine.co/"
b128c9ed
S
64 _TESTS = [
65 {
66 'url': 'https://vine.co/Visa',
67 'info_dict': {
68 'id': 'Visa',
69 },
70 'playlist_mincount': 46,
22a6f150 71 },
b128c9ed
S
72 {
73 'url': 'https://vine.co/u/941705360593584128',
74 'only_matching': True,
75 },
76 ]
ea783d01 77
a172b258
PH
78 def _real_extract(self, url):
79 mobj = re.match(self._VALID_URL, url)
80 user = mobj.group('user')
b128c9ed 81 u = mobj.group('u')
ea783d01 82
b128c9ed
S
83 profile_url = "%sapi/users/profiles/%s%s" % (
84 self._VINE_BASE_URL, 'vanity/' if not u else '', user)
a172b258
PH
85 profile_data = self._download_json(
86 profile_url, user, note='Downloading user profile data')
ea783d01 87
ea783d01
JN
88 user_id = profile_data['data']['userId']
89 timeline_data = []
90 for pagenum in itertools.count(1):
b128c9ed 91 timeline_url = "%sapi/timelines/users/%s?page=%s&size=100" % (
a172b258
PH
92 self._VINE_BASE_URL, user_id, pagenum)
93 timeline_page = self._download_json(
94 timeline_url, user, note='Downloading page %d' % pagenum)
ea783d01
JN
95 timeline_data.extend(timeline_page['data']['records'])
96 if timeline_page['data']['nextPage'] is None:
97 break
ea783d01 98
a172b258
PH
99 entries = [
100 self.url_result(e['permalinkUrl'], 'Vine') for e in timeline_data]
ea783d01 101 return self.playlist_result(entries, user)