[streamcloud] Detect removed videos (Closes #3768)
[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     sanitized_Request,
10     urlencode_postdata,
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     _TESTS = [{
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         'url': 'http://streamcloud.eu/ua8cmfh1nbe6/NSHIP-148--KUC-NG--H264-.mp4.html',
29         'only_matching': True,
30     }]
31
32     def _real_extract(self, url):
33         video_id = self._match_id(url)
34         url = 'http://streamcloud.eu/%s' % video_id
35
36         orig_webpage = self._download_webpage(url, video_id)
37
38         if '>File Not Found<' in orig_webpage:
39             raise ExtractorError(
40                 'Video %s does not exist' % video_id, expected=True)
41
42         fields = re.findall(r'''(?x)<input\s+
43             type="(?:hidden|submit)"\s+
44             name="([^"]+)"\s+
45             (?:id="[^"]+"\s+)?
46             value="([^"]*)"
47             ''', orig_webpage)
48         post = urlencode_postdata(fields)
49
50         self._sleep(12, video_id)
51         headers = {
52             b'Content-Type': b'application/x-www-form-urlencoded',
53         }
54         req = sanitized_Request(url, post, headers)
55
56         webpage = self._download_webpage(
57             req, video_id, note='Downloading video page ...')
58         title = self._html_search_regex(
59             r'<h1[^>]*>([^<]+)<', webpage, 'title')
60         video_url = self._search_regex(
61             r'file:\s*"([^"]+)"', webpage, 'video URL')
62         thumbnail = self._search_regex(
63             r'image:\s*"([^"]+)"', webpage, 'thumbnail URL', fatal=False)
64
65         return {
66             'id': video_id,
67             'title': title,
68             'url': video_url,
69             'thumbnail': thumbnail,
70         }