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