Merge pull request #3059 from marcwebbie/gorillavid
[youtube-dl] / youtube_dl / extractor / veoh.py
1 from __future__ import unicode_literals
2
3 import re
4 import json
5
6 from .common import InfoExtractor
7 from ..utils import (
8     compat_urllib_request,
9     int_or_none,
10     ExtractorError,
11 )
12
13
14 class VeohIE(InfoExtractor):
15     _VALID_URL = r'http://(?:www\.)?veoh\.com/(?:watch|iphone/#_Watch)/(?P<id>(?:v|yapi-)[\da-zA-Z]+)'
16
17     _TESTS = [
18         {
19             'url': 'http://www.veoh.com/watch/v56314296nk7Zdmz3',
20             'md5': '620e68e6a3cff80086df3348426c9ca3',
21             'info_dict': {
22                 'id': '56314296',
23                 'ext': 'mp4',
24                 'title': 'Straight Backs Are Stronger',
25                 'uploader': 'LUMOback',
26                 'description': 'At LUMOback, we believe straight backs are stronger.  The LUMOback Posture & Movement Sensor:  It gently vibrates when you slouch, inspiring improved posture and mobility.  Use the app to track your data and improve your posture over time. ',
27             },
28         },
29         {
30             'url': 'http://www.veoh.com/watch/v27701988pbTc4wzN?h1=Chile+workers+cover+up+to+avoid+skin+damage',
31             'md5': '4a6ff84b87d536a6a71e6aa6c0ad07fa',
32             'info_dict': {
33                 'id': '27701988',
34                 'ext': 'mp4',
35                 'title': 'Chile workers cover up to avoid skin damage',
36                 'description': 'md5:2bd151625a60a32822873efc246ba20d',
37                 'uploader': 'afp-news',
38                 'duration': 123,
39             },
40         },
41         {
42             'url': 'http://www.veoh.com/watch/v69525809F6Nc4frX',
43             'md5': '4fde7b9e33577bab2f2f8f260e30e979',
44             'note': 'Embedded ooyala video',
45             'info_dict': {
46                 'id': '69525809',
47                 'ext': 'mp4',
48                 'title': 'Doctors Alter Plan For Preteen\'s Weight Loss Surgery',
49                 'description': 'md5:f5a11c51f8fb51d2315bca0937526891',
50                 'uploader': 'newsy-videos',
51             },
52         },
53     ]
54
55     def _extract_formats(self, source):
56         formats = []
57         link = source.get('aowPermalink')
58         if link:
59             formats.append({
60                 'url': link,
61                 'ext': 'mp4',
62                 'format_id': 'aow',
63             })
64         link = source.get('fullPreviewHashLowPath')
65         if link:
66             formats.append({
67                 'url': link,
68                 'format_id': 'low',
69             })
70         link = source.get('fullPreviewHashHighPath')
71         if link:
72             formats.append({
73                 'url': link,
74                 'format_id': 'high',
75             })
76         return formats
77
78     def _extract_video(self, source):
79         return {
80             'id': source.get('videoId'),
81             'title': source.get('title'),
82             'description': source.get('description'),
83             'thumbnail': source.get('highResImage') or source.get('medResImage'),
84             'uploader': source.get('username'),
85             'duration': int_or_none(source.get('length')),
86             'view_count': int_or_none(source.get('views')),
87             'age_limit': 18 if source.get('isMature') == 'true' or source.get('isSexy') == 'true' else 0,
88             'formats': self._extract_formats(source),
89         }
90
91     def _real_extract(self, url):
92         mobj = re.match(self._VALID_URL, url)
93         video_id = mobj.group('id')
94
95         if video_id.startswith('v'):
96             rsp = self._download_xml(
97                 r'http://www.veoh.com/api/findByPermalink?permalink=%s' % video_id, video_id, 'Downloading video XML')
98             stat = rsp.get('stat')
99             if stat == 'ok':
100                 return self._extract_video(rsp.find('./videoList/video'))
101             elif stat == 'fail':
102                 raise ExtractorError(
103                     '%s said: %s' % (self.IE_NAME, rsp.find('./errorList/error').get('errorMessage')), expected=True)
104
105         webpage = self._download_webpage(url, video_id)
106         age_limit = 0
107         if 'class="adultwarning-container"' in webpage:
108             self.report_age_confirmation()
109             age_limit = 18
110             request = compat_urllib_request.Request(url)
111             request.add_header('Cookie', 'confirmedAdult=true')
112             webpage = self._download_webpage(request, video_id)
113
114         m_youtube = re.search(r'http://www\.youtube\.com/v/(.*?)(\&|"|\?)', webpage)
115         if m_youtube is not None:
116             youtube_id = m_youtube.group(1)
117             self.to_screen('%s: detected Youtube video.' % video_id)
118             return self.url_result(youtube_id, 'Youtube')
119
120         info = json.loads(
121             self._search_regex(r'videoDetailsJSON = \'({.*?})\';', webpage, 'info').replace('\\\'', '\''))
122
123         video = self._extract_video(info)
124         video['age_limit'] = age_limit
125
126         return video