[vube] Fix comment count
[youtube-dl] / youtube_dl / extractor / vube.py
1 from __future__ import unicode_literals
2
3 import json
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import int_or_none
8
9
10 class VubeIE(InfoExtractor):
11     IE_NAME = 'vube'
12     IE_DESC = 'Vube.com'
13     _VALID_URL = r'http://vube\.com/(?:[^/]+/)+(?P<id>[\da-zA-Z]{10})\b'
14
15     _TESTS = [
16         {
17             'url': 'http://vube.com/Chiara+Grispo+Video+Channel/YL2qNPkqon',
18             'md5': 'db7aba89d4603dadd627e9d1973946fe',
19             'info_dict': {
20                 'id': 'YL2qNPkqon',
21                 'ext': 'mp4',
22                 'title': 'Chiara Grispo - Price Tag by Jessie J',
23                 'description': 'md5:8ea652a1f36818352428cb5134933313',
24                 'thumbnail': 're:^http://frame\.thestaticvube\.com/snap/[0-9x]+/102e7e63057-5ebc-4f5c-4065-6ce4ebde131f\.jpg$',
25                 'uploader': 'Chiara.Grispo',
26                 'timestamp': 1388743358,
27                 'upload_date': '20140103',
28                 'duration': 170.56,
29                 'like_count': int,
30                 'dislike_count': int,
31                 'comment_count': int,
32             }
33         },
34         {
35             'url': 'http://vube.com/SerainaMusic/my-7-year-old-sister-and-i-singing-alive-by-krewella/UeBhTudbfS?t=s&n=1',
36             'md5': '5d4a52492d76f72712117ce6b0d98d08',
37             'info_dict': {
38                 'id': 'UeBhTudbfS',
39                 'ext': 'mp4',
40                 'title': 'My 7 year old Sister and I singing "Alive" by Krewella',
41                 'description': 'md5:40bcacb97796339f1690642c21d56f4a',
42                 'thumbnail': 're:^http://frame\.thestaticvube\.com/snap/[0-9x]+/102265d5a9f-0f17-4f6b-5753-adf08484ee1e\.jpg$',
43                 'uploader': 'Seraina',
44                 'timestamp': 1396492438,
45                 'upload_date': '20140403',
46                 'duration': 240.107,
47                 'like_count': int,
48                 'dislike_count': int,
49                 'comment_count': int,
50             }
51         }, {
52             'url': 'http://vube.com/vote/Siren+Gene/0nmsMY5vEq?n=2&t=s',
53             'md5': '0584fc13b50f887127d9d1007589d27f',
54             'info_dict': {
55                 'id': '0nmsMY5vEq',
56                 'ext': 'mp4',
57                 'title': 'Frozen - Let It Go Cover by Siren Gene',
58                 'description': 'My rendition of "Let It Go" originally sung by Idina Menzel.',
59                 'uploader': 'Siren Gene',
60                 'uploader_id': 'Siren',
61                 'thumbnail': 're:^http://frame\.thestaticvube\.com/snap/[0-9x]+/10283ab622a-86c9-4681-51f2-30d1f65774af\.jpg$',
62                 'duration': 221.788,
63                 'like_count': int,
64                 'dislike_count': int,
65                 'comment_count': int,
66             }
67         }
68     ]
69
70     def _real_extract(self, url):
71         mobj = re.match(self._VALID_URL, url)
72         video_id = mobj.group('id')
73
74         webpage = self._download_webpage(url, video_id)
75         data_json = self._search_regex(
76             r'(?s)window\["(?:tapiVideoData|vubeOriginalVideoData)"\]\s*=\s*(\{.*?\n});\n',
77             webpage, 'video data'
78         )
79         data = json.loads(data_json)
80         video = (
81             data.get('video') or
82             data)
83         assert isinstance(video, dict)
84
85         public_id = video['public_id']
86
87         formats = [
88             {
89                 'url': 'http://video.thestaticvube.com/video/%s/%s.mp4' % (fmt['media_resolution_id'], public_id),
90                 'height': int(fmt['height']),
91                 'abr': int(fmt['audio_bitrate']),
92                 'vbr': int(fmt['video_bitrate']),
93                 'format_id': fmt['media_resolution_id']
94             } for fmt in video['mtm'] if fmt['transcoding_status'] == 'processed'
95         ]
96
97         self._sort_formats(formats)
98
99         title = video['title']
100         description = video.get('description')
101         thumbnail = self._proto_relative_url(
102             video.get('thumbnail') or video.get('thumbnail_src'),
103             scheme='http:')
104         uploader = data.get('user', {}).get('channel', {}).get('name') or video.get('user_alias')
105         uploader_id = data.get('user', {}).get('name')
106         timestamp = int_or_none(video.get('upload_time'))
107         duration = video['duration']
108         view_count = video.get('raw_view_count')
109         like_count = video.get('rlikes')
110         if like_count is None:
111             like_count = video.get('total_likes')
112         dislike_count = video.get('rhates')
113         if dislike_count is None:
114             dislike_count = video.get('total_hates')
115
116         comments = video.get('comments')
117         comment_count = None
118         if comments is None:
119             comment_data = self._download_json(
120                 'http://vube.com/api/video/%s/comment' % video_id,
121                 video_id, 'Downloading video comment JSON', fatal=False)
122             if comment_data is not None:
123                 comment_count = int_or_none(comment_data.get('total'))
124         else:
125             comment_count = len(comments)
126
127         return {
128             'id': video_id,
129             'formats': formats,
130             'title': title,
131             'description': description,
132             'thumbnail': thumbnail,
133             'uploader': uploader,
134             'uploader_id': uploader_id,
135             'timestamp': timestamp,
136             'duration': duration,
137             'view_count': view_count,
138             'like_count': like_count,
139             'dislike_count': dislike_count,
140             'comment_count': comment_count,
141         }