]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/cloudy.py
Merge tag 'upstream/2015.07.21'
[youtubedl] / 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 ..compat import (
8 compat_parse_qs,
9 compat_urllib_parse,
10 compat_HTTPError,
11 )
12 from ..utils import (
13 ExtractorError,
14 HEADRequest,
15 remove_end,
16 )
17
18
19 class CloudyIE(InfoExtractor):
20 _IE_DESC = 'cloudy.ec and videoraj.ch'
21 _VALID_URL = r'''(?x)
22 https?://(?:www\.)?(?P<host>cloudy\.ec|videoraj\.ch)/
23 (?:v/|embed\.php\?id=)
24 (?P<id>[A-Za-z0-9]+)
25 '''
26 _EMBED_URL = 'http://www.%s/embed.php?id=%s'
27 _API_URL = 'http://www.%s/api/player.api.php?%s'
28 _MAX_TRIES = 2
29 _TESTS = [
30 {
31 'url': 'https://www.cloudy.ec/v/af511e2527aac',
32 'md5': '5cb253ace826a42f35b4740539bedf07',
33 'info_dict': {
34 'id': 'af511e2527aac',
35 'ext': 'flv',
36 'title': 'Funny Cats and Animals Compilation june 2013',
37 }
38 },
39 {
40 'url': 'http://www.videoraj.ch/v/47f399fd8bb60',
41 'md5': '7d0f8799d91efd4eda26587421c3c3b0',
42 'info_dict': {
43 'id': '47f399fd8bb60',
44 'ext': 'flv',
45 'title': 'Burning a New iPhone 5 with Gasoline - Will it Survive?',
46 }
47 }
48 ]
49
50 def _extract_video(self, video_host, video_id, file_key, error_url=None, try_num=0):
51
52 if try_num > self._MAX_TRIES - 1:
53 raise ExtractorError('Unable to extract video URL', expected=True)
54
55 form = {
56 'file': video_id,
57 'key': file_key,
58 }
59
60 if error_url:
61 form.update({
62 'numOfErrors': try_num,
63 'errorCode': '404',
64 'errorUrl': error_url,
65 })
66
67 data_url = self._API_URL % (video_host, compat_urllib_parse.urlencode(form))
68 player_data = self._download_webpage(
69 data_url, video_id, 'Downloading player data')
70 data = compat_parse_qs(player_data)
71
72 try_num += 1
73
74 if 'error' in data:
75 raise ExtractorError(
76 '%s error: %s' % (self.IE_NAME, ' '.join(data['error_msg'])),
77 expected=True)
78
79 title = data.get('title', [None])[0]
80 if title:
81 title = remove_end(title, '&asdasdas').strip()
82
83 video_url = data.get('url', [None])[0]
84
85 if video_url:
86 try:
87 self._request_webpage(HEADRequest(video_url), video_id, 'Checking video URL')
88 except ExtractorError as e:
89 if isinstance(e.cause, compat_HTTPError) and e.cause.code in [404, 410]:
90 self.report_warning('Invalid video URL, requesting another', video_id)
91 return self._extract_video(video_host, video_id, file_key, video_url, try_num)
92
93 return {
94 'id': video_id,
95 'url': video_url,
96 'title': title,
97 }
98
99 def _real_extract(self, url):
100 mobj = re.match(self._VALID_URL, url)
101 video_host = mobj.group('host')
102 video_id = mobj.group('id')
103
104 url = self._EMBED_URL % (video_host, video_id)
105 webpage = self._download_webpage(url, video_id)
106
107 file_key = self._search_regex(
108 [r'key\s*:\s*"([^"]+)"', r'filekey\s*=\s*"([^"]+)"'],
109 webpage, 'file_key')
110
111 return self._extract_video(video_host, video_id, file_key)