[vidto] Add extractor
[youtube-dl] / youtube_dl / extractor / vidto.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import sys
6 from .common import InfoExtractor
7 import time
8
9 from ..utils import (
10     encode_dict,
11 )
12 from ..compat import (
13     compat_chr,
14     compat_parse_qs,
15     compat_urllib_parse,
16     compat_urllib_parse_unquote,
17     compat_urllib_parse_unquote_plus,
18     compat_urllib_parse_urlparse,
19     compat_urllib_request,
20     compat_urlparse,
21     compat_str,
22 )
23
24
25 class VidtoIE(InfoExtractor):
26     IE_NAME = 'vidto'
27     IE_DESC = 'VidTo.me'
28     _VALID_URL = r'https?://(?:www\.)?vidto\.me/(?P<id>[0-9a-zA-Z]+)\.html'
29     _HOST = 'vidto.me'
30     _TEST = {
31         'url': 'http://vidto.me/ku5glz52nqe1.html',
32         'info_dict': {
33             'id': 'ku5glz52nqe1',
34             'ext': 'mp4',
35             'title': 'test.mp4'
36         }
37     }
38
39     def _real_extract(self, url):
40         mobj = re.match(self._VALID_URL, url)
41         video_id = mobj.group('id')
42
43         page = self._download_webpage(
44             'http://%s/%s.html' % (self._HOST, video_id), video_id, 'Downloading video page')
45         hash_regex = r'<input type="hidden" name="hash" value="(.*)">'
46         hash_value = self._search_regex(hash_regex, page, 'hash', fatal=True)
47         title_regex = r'<input type="hidden" name="fname" value="(.*)">'
48         title = self._search_regex(title_regex, page, 'title', fatal=False)
49         id_regex = r'<input type="hidden" name="id" value="(.*)">'
50         id_value = self._search_regex(id_regex, page, 'id', fatal=True)
51         cookies = self._get_cookies('http://%s/%s.html' % (self._HOST, video_id))
52
53
54         form_str = {
55             'op': 'download1',
56             'imhuman': 'Proceed to video',
57             'usr_login': '',
58             'id': id_value,
59             'fname': title,
60             'referer': '',
61             'hash': hash_value,
62         }
63         post_data = compat_urllib_parse.urlencode(encode_dict(form_str)).encode('ascii')
64         req = compat_urllib_request.Request(url, post_data)
65         req.add_header('Content-type', 'application/x-www-form-urlencoded')
66         for key, morsel in cookies.iteritems():
67             req.add_header('Cookie', '%s=%s' % (morsel.key, morsel.value))
68
69         print("Waiting for countdown...")
70         time.sleep(7)
71         post_result = self._download_webpage(
72             req, None,
73             note='Proceed to video...', errnote='unable to proceed', fatal=True)
74
75         file_link_regex = r'file_link ?= ?\'(https?:\/\/[0-9a-zA-z.\/\-_]+)'
76         file_link = self._search_regex(file_link_regex, post_result, 'file_link', fatal=True)
77
78         return {
79             'id': video_id,
80             'url': file_link,
81             'title': title,
82         }