[xfileshare] Add support for rapidvideo (closes #13348)
[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     determine_ext,
10     ExtractorError,
11     int_or_none,
12     NO_DEFAULT,
13     sanitized_Request,
14     urlencode_postdata,
15 )
16
17
18 class XFileShareIE(InfoExtractor):
19     _SITES = (
20         (r'daclips\.(?:in|com)', 'DaClips'),
21         (r'filehoot\.com', 'FileHoot'),
22         (r'gorillavid\.(?:in|com)', 'GorillaVid'),
23         (r'movpod\.in', 'MovPod'),
24         (r'powerwatch\.pw', 'PowerWatch'),
25         (r'rapidvideo\.ws', 'Rapidvideo.ws'),
26         (r'thevideobee\.to', 'TheVideoBee'),
27         (r'vidto\.me', 'Vidto'),
28         (r'streamin\.to', 'Streamin.To'),
29         (r'xvidstage\.com', 'XVIDSTAGE'),
30         (r'vidabc\.com', 'Vid ABC'),
31         (r'vidbom\.com', 'VidBom'),
32         (r'vidlo\.us', 'vidlo'),
33         (r'rapidvideo\.(?:cool|org)', 'RapidVideo.TV'),
34     )
35
36     IE_DESC = 'XFileShare based sites: %s' % ', '.join(list(zip(*_SITES))[1])
37     _VALID_URL = (r'https?://(?P<host>(?:www\.)?(?:%s))/(?:embed-)?(?P<id>[0-9a-zA-Z]+)'
38                   % '|'.join(site for site in list(zip(*_SITES))[0]))
39
40     _FILE_NOT_FOUND_REGEXES = (
41         r'>(?:404 - )?File Not Found<',
42         r'>The file was removed by administrator<',
43     )
44
45     _TESTS = [{
46         'url': 'http://gorillavid.in/06y9juieqpmi',
47         'md5': '5ae4a3580620380619678ee4875893ba',
48         'info_dict': {
49             'id': '06y9juieqpmi',
50             'ext': 'mp4',
51             'title': 'Rebecca Black My Moment Official Music Video Reaction-6GK87Rc8bzQ',
52             'thumbnail': r're:http://.*\.jpg',
53         },
54     }, {
55         'url': 'http://gorillavid.in/embed-z08zf8le23c6-960x480.html',
56         'only_matching': True,
57     }, {
58         'url': 'http://daclips.in/3rso4kdn6f9m',
59         'md5': '1ad8fd39bb976eeb66004d3a4895f106',
60         'info_dict': {
61             'id': '3rso4kdn6f9m',
62             'ext': 'mp4',
63             'title': 'Micro Pig piglets ready on 16th July 2009-bG0PdrCdxUc',
64             'thumbnail': r're:http://.*\.jpg',
65         }
66     }, {
67         'url': 'http://movpod.in/0wguyyxi1yca',
68         'only_matching': True,
69     }, {
70         'url': 'http://filehoot.com/3ivfabn7573c.html',
71         'info_dict': {
72             'id': '3ivfabn7573c',
73             'ext': 'mp4',
74             'title': 'youtube-dl test video \'äBaW_jenozKc.mp4.mp4',
75             'thumbnail': r're:http://.*\.jpg',
76         },
77         'skip': 'Video removed',
78     }, {
79         'url': 'http://vidto.me/ku5glz52nqe1.html',
80         'info_dict': {
81             'id': 'ku5glz52nqe1',
82             'ext': 'mp4',
83             'title': 'test'
84         }
85     }, {
86         'url': 'http://powerwatch.pw/duecjibvicbu',
87         'info_dict': {
88             'id': 'duecjibvicbu',
89             'ext': 'mp4',
90             'title': 'Big Buck Bunny trailer',
91         },
92     }, {
93         'url': 'http://xvidstage.com/e0qcnl03co6z',
94         'info_dict': {
95             'id': 'e0qcnl03co6z',
96             'ext': 'mp4',
97             'title': 'Chucky Prank 2015.mp4',
98         },
99     }, {
100         # removed by administrator
101         'url': 'http://xvidstage.com/amfy7atlkx25',
102         'only_matching': True,
103     }, {
104         'url': 'http://vidabc.com/i8ybqscrphfv',
105         'info_dict': {
106             'id': 'i8ybqscrphfv',
107             'ext': 'mp4',
108             'title': 're:Beauty and the Beast 2017',
109         },
110         'params': {
111             'skip_download': True,
112         },
113     }, {
114         'url': 'http://www.rapidvideo.cool/b667kprndr8w',
115         'only_matching': True,
116     }]
117
118     def _real_extract(self, url):
119         mobj = re.match(self._VALID_URL, url)
120         video_id = mobj.group('id')
121
122         url = 'http://%s/%s' % (mobj.group('host'), video_id)
123         webpage = self._download_webpage(url, video_id)
124
125         if any(re.search(p, webpage) for p in self._FILE_NOT_FOUND_REGEXES):
126             raise ExtractorError('Video %s does not exist' % video_id, expected=True)
127
128         fields = self._hidden_inputs(webpage)
129
130         if fields['op'] == 'download1':
131             countdown = int_or_none(self._search_regex(
132                 r'<span id="countdown_str">(?:[Ww]ait)?\s*<span id="cxc">(\d+)</span>\s*(?:seconds?)?</span>',
133                 webpage, 'countdown', default=None))
134             if countdown:
135                 self._sleep(countdown, video_id)
136
137             webpage = self._download_webpage(
138                 url, video_id, 'Downloading video page',
139                 data=urlencode_postdata(fields), headers={
140                     'Referer': url,
141                     'Content-type': 'application/x-www-form-urlencoded',
142                 })
143
144         title = (self._search_regex(
145             (r'style="z-index: [0-9]+;">([^<]+)</span>',
146              r'<td nowrap>([^<]+)</td>',
147              r'h4-fine[^>]*>([^<]+)<',
148              r'>Watch (.+) ',
149              r'<h2 class="video-page-head">([^<]+)</h2>',
150              r'<h2 style="[^"]*color:#403f3d[^"]*"[^>]*>([^<]+)<'),  # streamin.to
151             webpage, 'title', default=None) or self._og_search_title(
152             webpage, default=None) or video_id).strip()
153
154         def extract_formats(default=NO_DEFAULT):
155             urls = []
156             for regex in (
157                     r'file\s*:\s*(["\'])(?P<url>http(?:(?!\1).)+\.(?:m3u8|mp4|flv)(?:(?!\1).)*)\1',
158                     r'file_link\s*=\s*(["\'])(?P<url>http(?:(?!\1).)+)\1',
159                     r'addVariable\((\\?["\'])file\1\s*,\s*(\\?["\'])(?P<url>http(?:(?!\2).)+)\2\)',
160                     r'<embed[^>]+src=(["\'])(?P<url>http(?:(?!\1).)+\.(?:m3u8|mp4|flv)(?:(?!\1).)*)\1'):
161                 for mobj in re.finditer(regex, webpage):
162                     video_url = mobj.group('url')
163                     if video_url not in urls:
164                         urls.append(video_url)
165             formats = []
166             for video_url in urls:
167                 if determine_ext(video_url) == 'm3u8':
168                     formats.extend(self._extract_m3u8_formats(
169                         video_url, video_id, 'mp4',
170                         entry_protocol='m3u8_native', m3u8_id='hls',
171                         fatal=False))
172                 else:
173                     formats.append({
174                         'url': video_url,
175                         'format_id': 'sd',
176                     })
177             if not formats and default is not NO_DEFAULT:
178                 return default
179             self._sort_formats(formats)
180             return formats
181
182         formats = extract_formats(default=None)
183
184         if not formats:
185             webpage = decode_packed_codes(self._search_regex(
186                 r"(}\('(.+)',(\d+),(\d+),'[^']*\b(?:file|embed)\b[^']*'\.split\('\|'\))",
187                 webpage, 'packed code'))
188             formats = extract_formats()
189
190         thumbnail = self._search_regex(
191             r'image\s*:\s*["\'](http[^"\']+)["\'],', webpage, 'thumbnail', default=None)
192
193         return {
194             'id': video_id,
195             'title': title,
196             'thumbnail': thumbnail,
197             'formats': formats,
198         }