Fix imports and general cleanup
[youtube-dl] / youtube_dl / extractor / keezmovies.py
1 from __future__ import unicode_literals
2
3 import os
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import (
8     compat_urllib_parse_urlparse,
9     compat_urllib_request,
10     compat_urllib_parse,
11 )
12 from ..aes import (
13     aes_decrypt_text
14 )
15
16
17 class KeezMoviesIE(InfoExtractor):
18     _VALID_URL = r'https?://(?:www\.)?keezmovies\.com/video/.+?(?P<id>[0-9]+)(?:[/?&]|$)'
19     _TEST = {
20         'url': 'http://www.keezmovies.com/video/petite-asian-lady-mai-playing-in-bathtub-1214711',
21         'file': '1214711.mp4',
22         'md5': '6e297b7e789329923fcf83abb67c9289',
23         'info_dict': {
24             'title': 'Petite Asian Lady Mai Playing In Bathtub',
25             'age_limit': 18,
26         }
27     }
28
29     def _real_extract(self, url):
30         video_id = self._match_id(url)
31
32         req = compat_urllib_request.Request(url)
33         req.add_header('Cookie', 'age_verified=1')
34         webpage = self._download_webpage(req, video_id)
35
36         # embedded video
37         mobj = re.search(r'href="([^"]+)"></iframe>', webpage)
38         if mobj:
39             embedded_url = mobj.group(1)
40             return self.url_result(embedded_url)
41
42         video_title = self._html_search_regex(r'<h1 [^>]*>([^<]+)', webpage, 'title')
43         video_url = compat_urllib_parse.unquote(self._html_search_regex(r'video_url=(.+?)&amp;', webpage, 'video_url'))
44         if 'encrypted=true' in webpage:
45             password = self._html_search_regex(r'video_title=(.+?)&amp;', webpage, 'password')
46             video_url = aes_decrypt_text(video_url, password, 32).decode('utf-8')
47         path = compat_urllib_parse_urlparse(video_url).path
48         extension = os.path.splitext(path)[1][1:]
49         format = path.split('/')[4].split('_')[:2]
50         format = "-".join(format)
51
52         age_limit = self._rta_search(webpage)
53
54         return {
55             'id': video_id,
56             'title': video_title,
57             'url': video_url,
58             'ext': extension,
59             'format': format,
60             'format_id': format,
61             'age_limit': age_limit,
62         }