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