Merge remote-tracking branch '5moufl/behindkink'
[youtube-dl] / youtube_dl / extractor / hostingbulk.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     compat_urllib_request,
10     int_or_none,
11     urlencode_postdata,
12 )
13
14
15 class HostingBulkIE(InfoExtractor):
16     _VALID_URL = r'''(?x)
17         https?://(?:www\.)?hostingbulk\.com/
18         (?:embed-)?(?P<id>[A-Za-z0-9]{12})(?:-\d+x\d+)?\.html'''
19     _FILE_DELETED_REGEX = r'<b>File Not Found</b>'
20     _TEST = {
21         'url': 'http://hostingbulk.com/n0ulw1hv20fm.html',
22         'md5': '6c8653c8ecf7ebfa83b76e24b7b2fe3f',
23         'info_dict': {
24             'id': 'n0ulw1hv20fm',
25             'ext': 'mp4',
26             'title': 'md5:5afeba33f48ec87219c269e054afd622',
27             'filesize': 6816081,
28             'thumbnail': 're:^http://.*\.jpg$',
29         }
30     }
31
32     def _real_extract(self, url):
33         mobj = re.match(self._VALID_URL, url)
34         video_id = mobj.group('id')
35
36         url = 'http://hostingbulk.com/{0:}.html'.format(video_id)
37
38         # Custom request with cookie to set language to English, so our file
39         # deleted regex would work.
40         request = compat_urllib_request.Request(
41             url, headers={'Cookie': 'lang=english'})
42         webpage = self._download_webpage(request, video_id)
43
44         if re.search(self._FILE_DELETED_REGEX, webpage) is not None:
45             raise ExtractorError('Video %s does not exist' % video_id,
46                                  expected=True)
47
48         title = self._html_search_regex(r'<h3>(.*?)</h3>', webpage, 'title')
49         filesize = int_or_none(
50             self._search_regex(
51                 r'<small>\((\d+)\sbytes?\)</small>',
52                 webpage,
53                 'filesize',
54                 fatal=False
55             )
56         )
57         thumbnail = self._search_regex(
58             r'<img src="([^"]+)".+?class="pic"',
59             webpage, 'thumbnail', fatal=False)
60
61         fields = dict(re.findall(r'''(?x)<input\s+
62             type="hidden"\s+
63             name="([^"]+)"\s+
64             value="([^"]*)"
65             ''', webpage))
66
67         request = compat_urllib_request.Request(url, urlencode_postdata(fields))
68         request.add_header('Content-type', 'application/x-www-form-urlencoded')
69         response = self._request_webpage(request, video_id,
70                                          'Submiting download request')
71         video_url = response.geturl()
72
73         formats = [{
74             'format_id': 'sd',
75             'filesize': filesize,
76             'url': video_url,
77         }]
78
79         return {
80             'id': video_id,
81             'title': title,
82             'thumbnail': thumbnail,
83             'formats': formats,
84         }