Merge remote-tracking branch 'AGSPhoenix/teamcoco-fix'
[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 )
11
12
13 class VeohIE(InfoExtractor):
14     _VALID_URL = r'http://(?:www\.)?veoh\.com/(?:watch|iphone/#_Watch)/(?P<id>(?:v|yapi-)[\da-zA-Z]+)'
15
16     _TESTS = [
17         {
18             'url': 'http://www.veoh.com/watch/v56314296nk7Zdmz3',
19             'md5': '620e68e6a3cff80086df3348426c9ca3',
20             'info_dict': {
21                 'id': '56314296',
22                 'ext': 'mp4',
23                 'title': 'Straight Backs Are Stronger',
24                 'uploader': 'LUMOback',
25                 '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. ',
26             },
27         },
28         {
29             'url': 'http://www.veoh.com/watch/v27701988pbTc4wzN?h1=Chile+workers+cover+up+to+avoid+skin+damage',
30             'md5': '4a6ff84b87d536a6a71e6aa6c0ad07fa',
31             'info_dict': {
32                 'id': '27701988',
33                 'ext': 'mp4',
34                 'title': 'Chile workers cover up to avoid skin damage',
35                 'description': 'md5:2bd151625a60a32822873efc246ba20d',
36                 'uploader': 'afp-news',
37                 'duration': 123,
38             },
39         },
40         {
41             'url': 'http://www.veoh.com/watch/v69525809F6Nc4frX',
42             'md5': '4fde7b9e33577bab2f2f8f260e30e979',
43             'note': 'Embedded ooyala video',
44             'info_dict': {
45                 'id': '69525809',
46                 'ext': 'mp4',
47                 'title': 'Doctors Alter Plan For Preteen\'s Weight Loss Surgery',
48                 'description': 'md5:f5a11c51f8fb51d2315bca0937526891',
49                 'uploader': 'newsy-videos',
50             },
51         },
52     ]
53
54     def _extract_formats(self, source):
55         formats = []
56         link = source.get('aowPermalink')
57         if link:
58             formats.append({
59                 'url': link,
60                 'ext': 'mp4',
61                 'format_id': 'aow',
62             })
63         link = source.get('fullPreviewHashLowPath')
64         if link:
65             formats.append({
66                 'url': link,
67                 'format_id': 'low',
68             })
69         link = source.get('fullPreviewHashHighPath')
70         if link:
71             formats.append({
72                 'url': link,
73                 'format_id': 'high',
74             })
75         return formats
76
77     def _extract_video(self, source):
78         return {
79             'id': source.get('videoId'),
80             'title': source.get('title'),
81             'description': source.get('description'),
82             'thumbnail': source.get('highResImage') or source.get('medResImage'),
83             'uploader': source.get('username'),
84             'duration': int_or_none(source.get('length')),
85             'view_count': int_or_none(source.get('views')),
86             'age_limit': 18 if source.get('isMature') == 'true' or source.get('isSexy') == 'true' else 0,
87             'formats': self._extract_formats(source),
88         }
89
90     def _real_extract(self, url):
91         mobj = re.match(self._VALID_URL, url)
92         video_id = mobj.group('id')
93
94         if video_id.startswith('v'):
95             rsp = self._download_xml(
96                 r'http://www.veoh.com/api/findByPermalink?permalink=%s' % video_id, video_id, 'Downloading video XML')
97             if rsp.get('stat') == 'ok':
98                 return self._extract_video(rsp.find('./videoList/video'))
99
100         webpage = self._download_webpage(url, video_id)
101         age_limit = 0
102         if 'class="adultwarning-container"' in webpage:
103             self.report_age_confirmation()
104             age_limit = 18
105             request = compat_urllib_request.Request(url)
106             request.add_header('Cookie', 'confirmedAdult=true')
107             webpage = self._download_webpage(request, video_id)
108
109         m_youtube = re.search(r'http://www\.youtube\.com/v/(.*?)(\&|"|\?)', webpage)
110         if m_youtube is not None:
111             youtube_id = m_youtube.group(1)
112             self.to_screen('%s: detected Youtube video.' % video_id)
113             return self.url_result(youtube_id, 'Youtube')
114
115         info = json.loads(
116             self._search_regex(r'videoDetailsJSON = \'({.*?})\';', webpage, 'info').replace('\\\'', '\''))
117
118         video = self._extract_video(info)
119         video['age_limit'] = age_limit
120
121         return video