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