Merge remote-tracking branch 'Dineshs91/f4m-2.0'
[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 ..compat import (
8     compat_urllib_request,
9 )
10 from ..utils import (
11     ExtractorError,
12     int_or_none,
13     urlencode_postdata,
14 )
15
16
17 class HostingBulkIE(InfoExtractor):
18     _VALID_URL = r'''(?x)
19         https?://(?:www\.)?hostingbulk\.com/
20         (?:embed-)?(?P<id>[A-Za-z0-9]{12})(?:-\d+x\d+)?\.html'''
21     _FILE_DELETED_REGEX = r'<b>File Not Found</b>'
22     _TEST = {
23         'url': 'http://hostingbulk.com/n0ulw1hv20fm.html',
24         'md5': '6c8653c8ecf7ebfa83b76e24b7b2fe3f',
25         'info_dict': {
26             'id': 'n0ulw1hv20fm',
27             'ext': 'mp4',
28             'title': 'md5:5afeba33f48ec87219c269e054afd622',
29             'filesize': 6816081,
30             'thumbnail': 're:^http://.*\.jpg$',
31         }
32     }
33
34     def _real_extract(self, url):
35         video_id = self._match_id(url)
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         }