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