[keezmovies] Improve and modernize
[youtube-dl] / youtube_dl / extractor / keezmovies.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..aes import aes_decrypt_text
7 from ..compat import (
8     compat_str,
9     compat_urllib_parse_unquote,
10 )
11 from ..utils import (
12     determine_ext,
13     ExtractorError,
14     int_or_none,
15     str_to_int,
16     strip_or_none,
17 )
18
19
20 class KeezMoviesIE(InfoExtractor):
21     _VALID_URL = r'https?://(?:www\.)?keezmovies\.com/video/(?:(?P<display_id>[^/]+)-)?(?P<id>\d+)'
22     _TESTS = [{
23         'url': 'http://www.keezmovies.com/video/petite-asian-lady-mai-playing-in-bathtub-1214711',
24         'md5': '1c1e75d22ffa53320f45eeb07bc4cdc0',
25         'info_dict': {
26             'id': '1214711',
27             'display_id': 'petite-asian-lady-mai-playing-in-bathtub',
28             'ext': 'mp4',
29             'title': 'Petite Asian Lady Mai Playing In Bathtub',
30             'thumbnail': 're:^https?://.*\.jpg$',
31             'view_count': int,
32             'age_limit': 18,
33         }
34     }, {
35         'url': 'http://www.keezmovies.com/video/1214711',
36         'only_matching': True,
37     }]
38
39     def _extract_info(self, url):
40         mobj = re.match(self._VALID_URL, url)
41         video_id = mobj.group('id')
42         display_id = mobj.group('display_id') or video_id
43
44         webpage = self._download_webpage(
45             url, display_id, headers={'Cookie': 'age_verified=1'})
46
47         formats = []
48         format_urls = set()
49
50         title = None
51         thumbnail = None
52         duration = None
53         encrypted = False
54
55         def extract_format(format_url, height=None):
56             if not isinstance(format_url, compat_str) or not format_url.startswith('http'):
57                 return
58             if format_url in format_urls:
59                 return
60             format_urls.add(format_url)
61             tbr = int_or_none(self._search_regex(
62                 r'[/_](\d+)[kK][/_]', format_url, 'tbr', default=None))
63             if not height:
64                 height = int_or_none(self._search_regex(
65                     r'[/_](\d+)[pP][/_]', format_url, 'height', default=None))
66             if encrypted:
67                 format_url = aes_decrypt_text(
68                     video_url, title, 32).decode('utf-8')
69             formats.append({
70                 'url': format_url,
71                 'format_id': '%dp' % height if height else None,
72                 'height': height,
73                 'tbr': tbr,
74             })
75
76         flashvars = self._parse_json(
77             self._search_regex(
78                 r'flashvars\s*=\s*({.+?});', webpage,
79                 'flashvars', default='{}'),
80             display_id, fatal=False)
81
82         if flashvars:
83             title = flashvars.get('video_title')
84             thumbnail = flashvars.get('image_url')
85             duration = int_or_none(flashvars.get('video_duration'))
86             encrypted = flashvars.get('encrypted') is True
87             for key, value in flashvars.items():
88                 mobj = re.search(r'quality_(\d+)[pP]', key)
89                 if mobj:
90                     extract_format(value, int(mobj.group(1)))
91             video_url = flashvars.get('video_url')
92             if video_url and determine_ext(video_url, None):
93                 extract_format(video_url)
94
95         video_url = self._html_search_regex(
96             r'flashvars\.video_url\s*=\s*(["\'])(?P<url>http.+?)\1',
97             webpage, 'video url', default=None, group='url')
98         if video_url:
99             extract_format(compat_urllib_parse_unquote(video_url))
100
101         if not formats:
102             if 'title="This video is no longer available"' in webpage:
103                 raise ExtractorError(
104                     'Video %s is no longer available' % video_id, expected=True)
105
106         self._sort_formats(formats)
107
108         if not title:
109             title = self._html_search_regex(
110                 r'<h1[^>]*>([^<]+)', webpage, 'title')
111
112         return webpage, {
113             'id': video_id,
114             'display_id': display_id,
115             'title': strip_or_none(title),
116             'thumbnail': thumbnail,
117             'duration': duration,
118             'age_limit': 18,
119             'formats': formats,
120         }
121
122     def _real_extract(self, url):
123         webpage, info = self._extract_info(url)
124         info['view_count'] = str_to_int(self._search_regex(
125             r'<b>([\d,.]+)</b> Views?', webpage, 'view count', fatal=False))
126         return info