[xfileshare] Fix test
[youtube-dl] / youtube_dl / extractor / xfileshare.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     decode_packed_codes,
9     ExtractorError,
10     int_or_none,
11     NO_DEFAULT,
12     sanitized_Request,
13     urlencode_postdata,
14 )
15
16
17 class XFileShareIE(InfoExtractor):
18     _SITES = (
19         ('daclips.in', 'DaClips'),
20         ('filehoot.com', 'FileHoot'),
21         ('gorillavid.in', 'GorillaVid'),
22         ('movpod.in', 'MovPod'),
23         ('powerwatch.pw', 'PowerWatch'),
24         ('rapidvideo.ws', 'Rapidvideo.ws'),
25         ('thevideobee.to', 'TheVideoBee'),
26         ('vidto.me', 'Vidto'),
27         ('streamin.to', 'Streamin.To'),
28         ('xvidstage.com', 'XVIDSTAGE'),
29     )
30
31     IE_DESC = 'XFileShare based sites: %s' % ', '.join(list(zip(*_SITES))[1])
32     _VALID_URL = (r'https?://(?P<host>(?:www\.)?(?:%s))/(?:embed-)?(?P<id>[0-9a-zA-Z]+)'
33                   % '|'.join(re.escape(site) for site in list(zip(*_SITES))[0]))
34
35     _FILE_NOT_FOUND_REGEX = r'>(?:404 - )?File Not Found<'
36
37     _TESTS = [{
38         'url': 'http://gorillavid.in/06y9juieqpmi',
39         'md5': '5ae4a3580620380619678ee4875893ba',
40         'info_dict': {
41             'id': '06y9juieqpmi',
42             'ext': 'mp4',
43             'title': 'Rebecca Black My Moment Official Music Video Reaction-6GK87Rc8bzQ',
44             'thumbnail': 're:http://.*\.jpg',
45         },
46     }, {
47         'url': 'http://gorillavid.in/embed-z08zf8le23c6-960x480.html',
48         'only_matching': True,
49     }, {
50         'url': 'http://daclips.in/3rso4kdn6f9m',
51         'md5': '1ad8fd39bb976eeb66004d3a4895f106',
52         'info_dict': {
53             'id': '3rso4kdn6f9m',
54             'ext': 'mp4',
55             'title': 'Micro Pig piglets ready on 16th July 2009-bG0PdrCdxUc',
56             'thumbnail': 're:http://.*\.jpg',
57         }
58     }, {
59         'url': 'http://movpod.in/0wguyyxi1yca',
60         'only_matching': True,
61     }, {
62         'url': 'http://filehoot.com/3ivfabn7573c.html',
63         'info_dict': {
64             'id': '3ivfabn7573c',
65             'ext': 'mp4',
66             'title': 'youtube-dl test video \'äBaW_jenozKc.mp4.mp4',
67             'thumbnail': 're:http://.*\.jpg',
68         },
69         'skip': 'Video removed',
70     }, {
71         'url': 'http://vidto.me/ku5glz52nqe1.html',
72         'info_dict': {
73             'id': 'ku5glz52nqe1',
74             'ext': 'mp4',
75             'title': 'test'
76         }
77     }, {
78         'url': 'http://powerwatch.pw/duecjibvicbu',
79         'info_dict': {
80             'id': 'duecjibvicbu',
81             'ext': 'mp4',
82             'title': 'Big Buck Bunny trailer',
83         },
84     }, {
85         'url': 'http://xvidstage.com/e0qcnl03co6z',
86         'info_dict': {
87             'id': 'e0qcnl03co6z',
88             'ext': 'mp4',
89             'title': 'Chucky Prank 2015.mp4',
90         },
91     }]
92
93     def _real_extract(self, url):
94         mobj = re.match(self._VALID_URL, url)
95         video_id = mobj.group('id')
96
97         url = 'http://%s/%s' % (mobj.group('host'), video_id)
98         webpage = self._download_webpage(url, video_id)
99
100         if re.search(self._FILE_NOT_FOUND_REGEX, webpage) is not None:
101             raise ExtractorError('Video %s does not exist' % video_id, expected=True)
102
103         fields = self._hidden_inputs(webpage)
104
105         if fields['op'] == 'download1':
106             countdown = int_or_none(self._search_regex(
107                 r'<span id="countdown_str">(?:[Ww]ait)?\s*<span id="cxc">(\d+)</span>\s*(?:seconds?)?</span>',
108                 webpage, 'countdown', default=None))
109             if countdown:
110                 self._sleep(countdown, video_id)
111
112             post = urlencode_postdata(fields)
113
114             req = sanitized_Request(url, post)
115             req.add_header('Content-type', 'application/x-www-form-urlencoded')
116
117             webpage = self._download_webpage(req, video_id, 'Downloading video page')
118
119         title = (self._search_regex(
120             [r'style="z-index: [0-9]+;">([^<]+)</span>',
121              r'<td nowrap>([^<]+)</td>',
122              r'h4-fine[^>]*>([^<]+)<',
123              r'>Watch (.+) ',
124              r'<h2 class="video-page-head">([^<]+)</h2>'],
125             webpage, 'title', default=None) or self._og_search_title(webpage)).strip()
126
127         def extract_video_url(default=NO_DEFAULT):
128             return self._search_regex(
129                 (r'file\s*:\s*(["\'])(?P<url>http.+?)\1,',
130                  r'file_link\s*=\s*(["\'])(?P<url>http.+?)\1',
131                  r'addVariable\((\\?["\'])file\1\s*,\s*(\\?["\'])(?P<url>http.+?)\2\)',
132                  r'<embed[^>]+src=(["\'])(?P<url>http.+?)\1'),
133                 webpage, 'file url', default=default, group='url')
134
135         video_url = extract_video_url(default=None)
136
137         if not video_url:
138             webpage = decode_packed_codes(self._search_regex(
139                 r"(}\('(.+)',(\d+),(\d+),'[^']*\b(?:file|embed)\b[^']*'\.split\('\|'\))",
140                 webpage, 'packed code'))
141             video_url = extract_video_url()
142
143         thumbnail = self._search_regex(
144             r'image\s*:\s*["\'](http[^"\']+)["\'],', webpage, 'thumbnail', default=None)
145
146         formats = [{
147             'format_id': 'sd',
148             'url': video_url,
149             'quality': 1,
150         }]
151
152         return {
153             'id': video_id,
154             'title': title,
155             'thumbnail': thumbnail,
156             'formats': formats,
157         }