[cloudy] Add support for videoraj.ch
[youtube-dl] / youtube_dl / extractor / cloudy.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     ExtractorError,
9     compat_parse_qs,
10     compat_urllib_parse,
11     remove_end,
12 )
13
14
15 class CloudyIE(InfoExtractor):
16     _IE_DESC = 'cloudy.ec and videoraj.ch'
17     _VALID_URL = r'''(?x)
18         https?://(?:www\.)?(?P<host>cloudy\.ec|videoraj\.ch)/
19         (?:v/|embed\.php\?id=)
20         (?P<id>[A-Za-z0-9]+)
21         '''
22     _EMBED_URL = 'http://www.%s/embed.php?id=%s'
23     _API_URL = 'http://www.%s/api/player.api.php?%s'
24     _TESTS = [
25         {
26             'url': 'https://www.cloudy.ec/v/af511e2527aac',
27             'md5': '5cb253ace826a42f35b4740539bedf07',
28             'info_dict': {
29                 'id': 'af511e2527aac',
30                 'ext': 'flv',
31                 'title': 'Funny Cats and Animals Compilation june 2013',
32             }
33         },
34         {
35             'url': 'http://www.videoraj.ch/v/47f399fd8bb60',
36             'md5': '7d0f8799d91efd4eda26587421c3c3b0',
37             'info_dict': {
38                 'id': '47f399fd8bb60',
39                 'ext': 'flv',
40                 'title': 'Burning a New iPhone 5 with Gasoline - Will it Survive?',
41             }
42         }
43     ]
44
45     def _real_extract(self, url):
46         mobj = re.match(self._VALID_URL, url)
47         video_host = mobj.group('host')
48         video_id = mobj.group('id')
49
50         url = self._EMBED_URL % (video_host, video_id)
51         webpage = self._download_webpage(url, video_id)
52
53         file_key = self._search_regex(
54             r'filekey\s*=\s*"([^"]+)"', webpage, 'file_key')
55         data_url = self._API_URL % (video_host, compat_urllib_parse.urlencode({
56             'file': video_id,
57             'key': file_key,
58         }))
59         player_data = self._download_webpage(
60             data_url, video_id, 'Downloading player data')
61         data = compat_parse_qs(player_data)
62
63         if 'error' in data:
64             raise ExtractorError(
65                 '%s error: %s' % (self.IE_NAME, ' '.join(data['error_msg'])),
66                 expected=True)
67
68         title = data.get('title', [None])[0]
69         if title:
70             title = remove_end(title, '&asdasdas').strip()
71
72         formats = []
73         video_url = data.get('url', [None])[0]
74         if video_url:
75             formats.append({
76                 'format_id': 'sd',
77                 'url': video_url,
78             })
79
80         return {
81             'id': video_id,
82             'title': title,
83             'formats': formats,
84         }