Merge remote-tracking branch 'jaimeMF/yt-playlists'
[youtube-dl] / youtube_dl / extractor / streamcloud.py
1 # coding: utf-8
2 import re
3 import time
4
5 from .common import InfoExtractor
6 from ..utils import (
7     compat_urllib_parse,
8     compat_urllib_request,
9 )
10
11
12 class StreamcloudIE(InfoExtractor):
13     IE_NAME = u'streamcloud.eu'
14     _VALID_URL = r'https?://streamcloud\.eu/(?P<id>[a-zA-Z0-9_-]+)/(?P<fname>[^#?]*)\.html'
15
16     _TEST = {
17         u'url': u'http://streamcloud.eu/skp9j99s4bpz/youtube-dl_test_video_____________-BaW_jenozKc.mp4.html',
18         u'file': u'skp9j99s4bpz.mp4',
19         u'md5': u'6bea4c7fa5daaacc2a946b7146286686',
20         u'info_dict': {
21             u'title': u'youtube-dl test video  \'/\\ ä ↭',
22             u'duration': 9,
23         },
24     }
25
26     def _real_extract(self, url):
27         mobj = re.match(self._VALID_URL, url)
28         video_id = mobj.group('id')
29
30         orig_webpage = self._download_webpage(url, video_id)
31
32         fields = re.findall(r'''(?x)<input\s+
33             type="(?:hidden|submit)"\s+
34             name="([^"]+)"\s+
35             (?:id="[^"]+"\s+)?
36             value="([^"]*)"
37             ''', orig_webpage)
38         post = compat_urllib_parse.urlencode(fields)
39
40         self.to_screen('%s: Waiting for timeout' % video_id)
41         time.sleep(12)
42         headers = {
43             b'Content-Type': b'application/x-www-form-urlencoded',
44         }
45         req = compat_urllib_request.Request(url, post, headers)
46
47         webpage = self._download_webpage(
48             req, video_id, note=u'Downloading video page ...')
49         title = self._html_search_regex(
50             r'<h1[^>]*>([^<]+)<', webpage, u'title')
51         video_url = self._search_regex(
52             r'file:\s*"([^"]+)"', webpage, u'video URL')
53         duration_str = self._search_regex(
54             r'duration:\s*"?([0-9]+)"?', webpage, u'duration', fatal=False)
55         duration = None if duration_str is None else int(duration_str)
56         thumbnail = self._search_regex(
57             r'image:\s*"([^"]+)"', webpage, u'thumbnail URL', fatal=False)
58
59         return {
60             'id': video_id,
61             'title': title,
62             'url': video_url,
63             'duration': duration,
64             'thumbnail': thumbnail,
65         }