Fix imports and general cleanup
[youtube-dl] / youtube_dl / extractor / pornhub.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,
9     compat_urllib_parse_urlparse,
10     compat_urllib_request,
11 )
12 from ..utils import (
13     str_to_int,
14 )
15 from ..aes import (
16     aes_decrypt_text
17 )
18
19
20 class PornHubIE(InfoExtractor):
21     _VALID_URL = r'https?://(?:www\.)?pornhub\.com/view_video\.php\?viewkey=(?P<id>[0-9a-f]+)'
22     _TEST = {
23         'url': 'http://www.pornhub.com/view_video.php?viewkey=648719015',
24         'md5': '882f488fa1f0026f023f33576004a2ed',
25         'info_dict': {
26             'id': '648719015',
27             'ext': 'mp4',
28             "uploader": "Babes",
29             "title": "Seductive Indian beauty strips down and fingers her pink pussy",
30             "age_limit": 18
31         }
32     }
33
34     def _extract_count(self, pattern, webpage, name):
35         count = self._html_search_regex(pattern, webpage, '%s count' % name, fatal=False)
36         if count:
37             count = str_to_int(count)
38         return count
39
40     def _real_extract(self, url):
41         video_id = self._match_id(url)
42
43         req = compat_urllib_request.Request(url)
44         req.add_header('Cookie', 'age_verified=1')
45         webpage = self._download_webpage(req, video_id)
46
47         video_title = self._html_search_regex(r'<h1 [^>]+>([^<]+)', webpage, 'title')
48         video_uploader = self._html_search_regex(
49             r'(?s)From:&nbsp;.+?<(?:a href="/users/|a href="/channels/|<span class="username)[^>]+>(.+?)<',
50             webpage, 'uploader', fatal=False)
51         thumbnail = self._html_search_regex(r'"image_url":"([^"]+)', webpage, 'thumbnail', fatal=False)
52         if thumbnail:
53             thumbnail = compat_urllib_parse.unquote(thumbnail)
54
55         view_count = self._extract_count(r'<span class="count">([\d,\.]+)</span> views', webpage, 'view')
56         like_count = self._extract_count(r'<span class="votesUp">([\d,\.]+)</span>', webpage, 'like')
57         dislike_count = self._extract_count(r'<span class="votesDown">([\d,\.]+)</span>', webpage, 'dislike')
58         comment_count = self._extract_count(
59             r'All comments \(<var class="videoCommentCount">([\d,\.]+)</var>', webpage, 'comment')
60
61         video_urls = list(map(compat_urllib_parse.unquote, re.findall(r'"quality_[0-9]{3}p":"([^"]+)', webpage)))
62         if webpage.find('"encrypted":true') != -1:
63             password = compat_urllib_parse.unquote_plus(self._html_search_regex(r'"video_title":"([^"]+)', webpage, 'password'))
64             video_urls = list(map(lambda s: aes_decrypt_text(s, password, 32).decode('utf-8'), video_urls))
65
66         formats = []
67         for video_url in video_urls:
68             path = compat_urllib_parse_urlparse(video_url).path
69             extension = os.path.splitext(path)[1][1:]
70             format = path.split('/')[5].split('_')[:2]
71             format = "-".join(format)
72
73             m = re.match(r'^(?P<height>[0-9]+)P-(?P<tbr>[0-9]+)K$', format)
74             if m is None:
75                 height = None
76                 tbr = None
77             else:
78                 height = int(m.group('height'))
79                 tbr = int(m.group('tbr'))
80
81             formats.append({
82                 'url': video_url,
83                 'ext': extension,
84                 'format': format,
85                 'format_id': format,
86                 'tbr': tbr,
87                 'height': height,
88             })
89         self._sort_formats(formats)
90
91         return {
92             'id': video_id,
93             'uploader': video_uploader,
94             'title': video_title,
95             'thumbnail': thumbnail,
96             'view_count': view_count,
97             'like_count': like_count,
98             'dislike_count': dislike_count,
99             'comment_count': comment_count,
100             'formats': formats,
101             'age_limit': 18,
102         }