Extract all vid.me formats
[youtube-dl] / youtube_dl / extractor / vidme.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor, ExtractorError
4 from ..utils import (
5     int_or_none,
6     float_or_none,
7     parse_iso8601,
8 )
9
10
11 class VidmeIE(InfoExtractor):
12     _VALID_URL = r'https?://vid\.me/(?:e/)?(?P<id>[\da-zA-Z]+)'
13     _TESTS = [{
14         'url': 'https://vid.me/QNB',
15         'md5': 'c62f1156138dc3323902188c5b5a8bd6',
16         'info_dict': {
17             'id': 'QNB',
18             'ext': 'mp4',
19             'title': 'Fishing for piranha - the easy way',
20             'description': 'source: https://www.facebook.com/photo.php?v=312276045600871',
21             'duration': 119.92,
22             'timestamp': 1406313244,
23             'upload_date': '20140725',
24             'thumbnail': 're:^https?://.*\.jpg',
25             'view_count': int,
26             'like_count': int,
27             'comment_count': int,
28         },
29     }, {
30         'url': 'https://vid.me/Gc6M',
31         'md5': 'f42d05e7149aeaec5c037b17e5d3dc82',
32         'info_dict': {
33             'id': 'Gc6M',
34             'ext': 'mp4',
35             'title': 'O Mere Dil ke chain - Arnav and Khushi VM',
36             'duration': 223.72,
37             'timestamp': 1441211642,
38             'upload_date': '20150902',
39             'thumbnail': 're:^https?://.*\.jpg',
40             'view_count': int,
41             'like_count': int,
42             'comment_count': int,
43             'comment_count': int,
44         },
45         'params': {
46             'skip_download': True,
47         },
48     }, {
49         # tests uploader field
50         'url': 'https://vid.me/4Iib',
51         'info_dict': {
52             'id': '4Iib',
53             'ext': 'mp4',
54             'title': 'The Carver',
55             'description': 'md5:e9c24870018ae8113be936645b93ba3c',
56             'duration': 97.859999999999999,
57             'timestamp': 1433203629,
58             'upload_date': '20150602',
59             'uploader': 'Thomas',
60             'thumbnail': 're:^https?://.*\.jpg',
61             'view_count': int,
62             'like_count': int,
63             'comment_count': int,
64         },
65         'params': {
66             'skip_download': True,
67         },
68     }, {
69         # From http://naked-yogi.tumblr.com/post/118312946248/naked-smoking-stretching
70         'url': 'https://vid.me/e/Wmur',
71         'only_matching': True,
72     }]
73
74     def _real_extract(self, url):
75         video_id = self._match_id(url)
76         api_url = 'https://api.vid.me/videoByUrl/' + video_id
77         data = self._download_json(api_url, video_id)
78
79         video_data = data.get('video')
80         if video_data is None:
81             raise ExtractorError('Could not extract the vid.me video data')
82
83         title = video_data.get('title')
84         description = video_data.get('description')
85         thumbnail = video_data.get('thumbnail_url')
86         timestamp = parse_iso8601(video_data.get('date_created'), ' ')
87         duration = float_or_none(video_data.get('duration'))
88         view_count = int_or_none(video_data.get('view_count'))
89         like_count = int_or_none(video_data.get('likes_count'))
90         comment_count = int_or_none(video_data.get('comment_count'))
91
92         uploader = None
93         user_data = video_data.get('user')
94         if user_data is not None:
95             uploader = user_data.get('username')
96
97         formats = [{
98             'format_id': format['type'],
99             'url': format['uri'],
100             'width': int_or_none(format['width']),
101             'height': int_or_none(format['height']),
102         } for format in video_data.get('formats', [])]
103         self._sort_formats(formats)
104
105         return {
106             'id': video_id,
107             'title': title,
108             'description': description,
109             'thumbnail': thumbnail,
110             'timestamp': timestamp,
111             'duration': duration,
112             'view_count': view_count,
113             'like_count': like_count,
114             'comment_count': comment_count,
115             'uploader': uploader,
116             'formats': formats,
117         }