[chilloutzone] Simplify (#2338)
[youtube-dl] / youtube_dl / extractor / chilloutzone.py
1 from __future__ import unicode_literals
2
3 import re
4 import base64
5 import json
6
7 from .common import InfoExtractor
8 from ..utils import clean_html
9
10
11 class ChilloutzoneIE(InfoExtractor):
12     _VALID_URL = r'https?://(?:www\.)?chilloutzone\.net/video/(?P<id>[\w|-]+)\.html'
13     _TEST = {
14         'url': 'http://www.chilloutzone.net/video/enemene-meck-alle-katzen-weg.html',
15         'md5': 'a76f3457e813ea0037e5244f509e66d1',
16         'info_dict': {
17             'id': 'enemene-meck-alle-katzen-weg',
18             'ext': 'mp4',
19             'title': 'Enemene Meck - Alle Katzen weg',
20             'description': 'Ist das der Umkehrschluss des Niesenden Panda-Babys?',
21         },
22     }
23
24     def _real_extract(self, url):
25         mobj = re.match(self._VALID_URL, url)
26         video_id = mobj.group('id')
27
28         webpage = self._download_webpage(url, video_id)
29
30         base64_video_info = self._html_search_regex(
31             r'var cozVidData = "(.+?)";', webpage, 'video data')
32         decoded_video_info = base64.b64decode(base64_video_info).decode("utf-8")
33         video_info_dict = json.loads(decoded_video_info)
34
35         # get video information from dict
36         video_url = video_info_dict['mediaUrl']
37         description = clean_html(video_info_dict.get('description'))
38         title = video_info_dict['title']
39         native_platform = video_info_dict['nativePlatform']
40         native_video_id = video_info_dict['nativeVideoId']
41         source_priority = video_info_dict['sourcePriority']
42
43         # If nativePlatform is None a fallback mechanism is used (i.e. youtube embed)
44         if native_platform is None:
45             youtube_url = self._html_search_regex(
46                 r'<iframe.* src="((?:https?:)?//(?:[^.]+\.)?youtube\.com/.+?)"',
47                 webpage, 'fallback video URL', default=None)
48             if youtube_url is not None:
49                 return self.url_result(youtube_url, ie='Youtube')
50
51         # Non Fallback: Decide to use native source (e.g. youtube or vimeo) or
52         # the own CDN
53         if source_priority == 'native':
54             if native_platform == 'youtube':
55                 return self.url_result(video_id, ie='Youtube')
56             if native_platform == 'vimeo':
57                 return self.url_result(
58                     'http://vimeo.com/' + native_video_id, ie='Vimeo')
59
60         if not video_url:
61             raise ExtractorError('No video found')
62
63         return {
64             'id': video_id,
65             'url': video_url,
66             'ext': 'mp4',
67             'title': title,
68             'description': description,
69         }