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