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