[compat] Add compat_urllib_parse_urlencode and eliminate encode_dict
[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 ..compat import compat_urllib_parse_urlencode
8 from ..utils import (
9     ExtractorError,
10     int_or_none,
11     sanitized_Request,
12 )
13
14
15 class XFileShareIE(InfoExtractor):
16     IE_DESC = 'XFileShare based sites: GorillaVid.in, daclips.in, movpod.in, fastvideo.in, realvid.net, filehoot.com and vidto.me'
17     _VALID_URL = r'''(?x)
18         https?://(?P<host>(?:www\.)?
19             (?:daclips\.in|gorillavid\.in|movpod\.in|fastvideo\.in|realvid\.net|filehoot\.com|vidto\.me|powerwatch\.pw))/
20         (?:embed-)?(?P<id>[0-9a-zA-Z]+)(?:-[0-9]+x[0-9]+\.html)?
21     '''
22
23     _FILE_NOT_FOUND_REGEX = r'>(?:404 - )?File Not Found<'
24
25     _TESTS = [{
26         'url': 'http://gorillavid.in/06y9juieqpmi',
27         'md5': '5ae4a3580620380619678ee4875893ba',
28         'info_dict': {
29             'id': '06y9juieqpmi',
30             'ext': 'flv',
31             'title': 'Rebecca Black My Moment Official Music Video Reaction-6GK87Rc8bzQ',
32             'thumbnail': 're:http://.*\.jpg',
33         },
34     }, {
35         'url': 'http://gorillavid.in/embed-z08zf8le23c6-960x480.html',
36         'only_matching': True,
37     }, {
38         'url': 'http://daclips.in/3rso4kdn6f9m',
39         'md5': '1ad8fd39bb976eeb66004d3a4895f106',
40         'info_dict': {
41             'id': '3rso4kdn6f9m',
42             'ext': 'mp4',
43             'title': 'Micro Pig piglets ready on 16th July 2009-bG0PdrCdxUc',
44             'thumbnail': 're:http://.*\.jpg',
45         }
46     }, {
47         # video with countdown timeout
48         'url': 'http://fastvideo.in/1qmdn1lmsmbw',
49         'md5': '8b87ec3f6564a3108a0e8e66594842ba',
50         'info_dict': {
51             'id': '1qmdn1lmsmbw',
52             'ext': 'mp4',
53             'title': 'Man of Steel - Trailer',
54             'thumbnail': 're:http://.*\.jpg',
55         },
56     }, {
57         'url': 'http://realvid.net/ctn2y6p2eviw',
58         'md5': 'b2166d2cf192efd6b6d764c18fd3710e',
59         'info_dict': {
60             'id': 'ctn2y6p2eviw',
61             'ext': 'flv',
62             'title': 'rdx 1955',
63             'thumbnail': 're:http://.*\.jpg',
64         },
65     }, {
66         'url': 'http://movpod.in/0wguyyxi1yca',
67         'only_matching': True,
68     }, {
69         'url': 'http://filehoot.com/3ivfabn7573c.html',
70         'info_dict': {
71             'id': '3ivfabn7573c',
72             'ext': 'mp4',
73             'title': 'youtube-dl test video \'äBaW_jenozKc.mp4.mp4',
74             'thumbnail': 're:http://.*\.jpg',
75         }
76     }, {
77         'url': 'http://vidto.me/ku5glz52nqe1.html',
78         'info_dict': {
79             'id': 'ku5glz52nqe1',
80             'ext': 'mp4',
81             'title': 'test'
82         }
83     }, {
84         'url': 'http://powerwatch.pw/duecjibvicbu',
85         'info_dict': {
86             'id': 'duecjibvicbu',
87             'ext': 'mp4',
88             'title': 'Big Buck Bunny trailer',
89         },
90     }]
91
92     def _real_extract(self, url):
93         mobj = re.match(self._VALID_URL, url)
94         video_id = mobj.group('id')
95
96         url = 'http://%s/%s' % (mobj.group('host'), video_id)
97         webpage = self._download_webpage(url, video_id)
98
99         if re.search(self._FILE_NOT_FOUND_REGEX, webpage) is not None:
100             raise ExtractorError('Video %s does not exist' % video_id, expected=True)
101
102         fields = self._hidden_inputs(webpage)
103
104         if fields['op'] == 'download1':
105             countdown = int_or_none(self._search_regex(
106                 r'<span id="countdown_str">(?:[Ww]ait)?\s*<span id="cxc">(\d+)</span>\s*(?:seconds?)?</span>',
107                 webpage, 'countdown', default=None))
108             if countdown:
109                 self._sleep(countdown, video_id)
110
111             post = compat_urllib_parse_urlencode(fields)
112
113             req = sanitized_Request(url, post)
114             req.add_header('Content-type', 'application/x-www-form-urlencoded')
115
116             webpage = self._download_webpage(req, video_id, 'Downloading video page')
117
118         title = (self._search_regex(
119             [r'style="z-index: [0-9]+;">([^<]+)</span>',
120              r'<td nowrap>([^<]+)</td>',
121              r'h4-fine[^>]*>([^<]+)<',
122              r'>Watch (.+) ',
123              r'<h2 class="video-page-head">([^<]+)</h2>'],
124             webpage, 'title', default=None) or self._og_search_title(webpage)).strip()
125         video_url = self._search_regex(
126             [r'file\s*:\s*["\'](http[^"\']+)["\'],',
127              r'file_link\s*=\s*\'(https?:\/\/[0-9a-zA-z.\/\-_]+)'],
128             webpage, 'file url')
129         thumbnail = self._search_regex(
130             r'image\s*:\s*["\'](http[^"\']+)["\'],', webpage, 'thumbnail', default=None)
131
132         formats = [{
133             'format_id': 'sd',
134             'url': video_url,
135             'quality': 1,
136         }]
137
138         return {
139             'id': video_id,
140             'title': title,
141             'thumbnail': thumbnail,
142             'formats': formats,
143         }