[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / streamcloud.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     urlencode_postdata,
10 )
11
12
13 class StreamcloudIE(InfoExtractor):
14     IE_NAME = 'streamcloud.eu'
15     _VALID_URL = r'https?://streamcloud\.eu/(?P<id>[a-zA-Z0-9_-]+)(?:/(?P<fname>[^#?]*)\.html)?'
16
17     _TESTS = [{
18         'url': 'http://streamcloud.eu/skp9j99s4bpz/youtube-dl_test_video_____________-BaW_jenozKc.mp4.html',
19         'md5': '6bea4c7fa5daaacc2a946b7146286686',
20         'info_dict': {
21             'id': 'skp9j99s4bpz',
22             'ext': 'mp4',
23             'title': 'youtube-dl test video  \'/\\ ä ↭',
24         },
25         'skip': 'Only available from the EU'
26     }, {
27         'url': 'http://streamcloud.eu/ua8cmfh1nbe6/NSHIP-148--KUC-NG--H264-.mp4.html',
28         'only_matching': True,
29     }]
30
31     def _real_extract(self, url):
32         video_id = self._match_id(url)
33         url = 'http://streamcloud.eu/%s' % video_id
34
35         orig_webpage = self._download_webpage(url, video_id)
36
37         if '>File Not Found<' in orig_webpage:
38             raise ExtractorError(
39                 'Video %s does not exist' % video_id, expected=True)
40
41         fields = re.findall(r'''(?x)<input\s+
42             type="(?:hidden|submit)"\s+
43             name="([^"]+)"\s+
44             (?:id="[^"]+"\s+)?
45             value="([^"]*)"
46             ''', orig_webpage)
47
48         self._sleep(6, video_id)
49
50         webpage = self._download_webpage(
51             url, video_id, data=urlencode_postdata(fields), headers={
52                 b'Content-Type': b'application/x-www-form-urlencoded',
53             })
54
55         try:
56             title = self._html_search_regex(
57                 r'<h1[^>]*>([^<]+)<', webpage, 'title')
58             video_url = self._search_regex(
59                 r'file:\s*"([^"]+)"', webpage, 'video URL')
60         except ExtractorError:
61             message = self._html_search_regex(
62                 r'(?s)<div[^>]+class=(["\']).*?msgboxinfo.*?\1[^>]*>(?P<message>.+?)</div>',
63                 webpage, 'message', default=None, group='message')
64             if message:
65                 raise ExtractorError('%s said: %s' % (self.IE_NAME, message), expected=True)
66             raise
67         thumbnail = self._search_regex(
68             r'image:\s*"([^"]+)"', webpage, 'thumbnail URL', fatal=False)
69
70         return {
71             'id': video_id,
72             'title': title,
73             'url': video_url,
74             'thumbnail': thumbnail,
75             'http_headers': {
76                 'Referer': url,
77             },
78         }