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