[cloudy] Add new extractor. Closes #3743
[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 )
12
13
14 class CloudyIE(InfoExtractor):
15     _VALID_URL = r'''(?x)
16         https?://(?:www\.)?cloudy\.ec/
17         (?:v/|embed\.php\?id=)
18         (?P<id>[A-Za-z0-9]+)
19         '''
20     _API_URL = 'http://www.cloudy.ec/api/player.api.php?%s'
21     _TEST = {
22         'url': 'https://www.cloudy.ec/v/af511e2527aac',
23         'md5': '5cb253ace826a42f35b4740539bedf07',
24         'info_dict': {
25             'id': 'af511e2527aac',
26             'ext': 'flv',
27             'title': 'Funny Cats and Animals Compilation june 2013',
28         }
29     }
30
31     def _real_extract(self, url):
32         mobj = re.match(self._VALID_URL, url)
33         video_id = mobj.group('id')
34
35         url = 'http://www.cloudy.ec/embed.php?id=%s' % video_id
36         webpage = self._download_webpage(url, video_id)
37
38         file_key = self._search_regex(
39             r'filekey\s*=\s*"([^"]+)"', webpage, 'file_key')
40         data_url = self._API_URL % compat_urllib_parse.urlencode({
41             'file': video_id,
42             'key': file_key,
43         })
44         player_data = self._download_webpage(
45             data_url, video_id, 'Downloading player data')
46         data = compat_parse_qs(player_data)
47
48         if 'error' in data:
49             raise ExtractorError(
50                 '%s error: %s' % (self.IE_NAME, ' '.join(data['error_msg'])),
51                 expected=True)
52
53         title = data.get('title', [None])[0]
54         if title:
55             title = title.replace('&asdasdas', '').strip()
56
57         formats = []
58         formats.append({
59             'format_id': 'sd',
60             'url': data.get('url', [None])[0],
61         })
62
63         return {
64             'id': video_id,
65             'title': title,
66             'formats': formats,
67         }