Merge remote-tracking branch 'Dineshs91/f4m-2.0'
[youtube-dl] / youtube_dl / extractor / sockshare.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from ..compat import (
7     compat_urllib_parse,
8     compat_urllib_request,
9 )
10 from ..utils import (
11     determine_ext,
12     ExtractorError,
13 )
14
15 from .common import InfoExtractor
16
17
18 class SockshareIE(InfoExtractor):
19     _VALID_URL = r'https?://(?:www\.)?sockshare\.com/file/(?P<id>[0-9A-Za-z]+)'
20     _FILE_DELETED_REGEX = r'This file doesn\'t exist, or has been removed\.</div>'
21     _TEST = {
22         'url': 'http://www.sockshare.com/file/437BE28B89D799D7',
23         'md5': '9d0bf1cfb6dbeaa8d562f6c97506c5bd',
24         'info_dict': {
25             'id': '437BE28B89D799D7',
26             'title': 'big_buck_bunny_720p_surround.avi',
27             'ext': 'avi',
28             'thumbnail': 're:^http://.*\.jpg$',
29         }
30     }
31
32     def _real_extract(self, url):
33         video_id = self._match_id(url)
34         url = 'http://sockshare.com/file/%s' % video_id
35         webpage = self._download_webpage(url, video_id)
36
37         if re.search(self._FILE_DELETED_REGEX, webpage) is not None:
38             raise ExtractorError('Video %s does not exist' % video_id,
39                                  expected=True)
40
41         confirm_hash = self._html_search_regex(r'''(?x)<input\s+
42             type="hidden"\s+
43             value="([^"]*)"\s+
44             name="hash"
45             ''', webpage, 'hash')
46
47         fields = {
48             "hash": confirm_hash,
49             "confirm": "Continue as Free User"
50         }
51
52         post = compat_urllib_parse.urlencode(fields)
53         req = compat_urllib_request.Request(url, post)
54         # Apparently, this header is required for confirmation to work.
55         req.add_header('Host', 'www.sockshare.com')
56         req.add_header('Content-type', 'application/x-www-form-urlencoded')
57
58         webpage = self._download_webpage(
59             req, video_id, 'Downloading video page')
60
61         video_url = self._html_search_regex(
62             r'<a href="([^"]*)".+class="download_file_link"',
63             webpage, 'file url')
64         video_url = "http://www.sockshare.com" + video_url
65         title = self._html_search_regex((
66             r'<h1>(.+)<strong>',
67             r'var name = "([^"]+)";'),
68             webpage, 'title', default=None)
69         thumbnail = self._html_search_regex(
70             r'<img\s+src="([^"]*)".+?name="bg"',
71             webpage, 'thumbnail')
72
73         formats = [{
74             'format_id': 'sd',
75             'url': video_url,
76             'ext': determine_ext(title),
77         }]
78
79         return {
80             'id': video_id,
81             'title': title,
82             'thumbnail': thumbnail,
83             'formats': formats,
84         }