]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/pornhub.py
debian/control: Prefer mplayer to mplayer2 in Recommends.
[youtubedl] / 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_unquote,
9 compat_urllib_parse_unquote_plus,
10 compat_urllib_parse_urlparse,
11 compat_urllib_request,
12 )
13 from ..utils import (
14 ExtractorError,
15 str_to_int,
16 )
17 from ..aes import (
18 aes_decrypt_text
19 )
20
21
22 class PornHubIE(InfoExtractor):
23 _VALID_URL = r'https?://(?:www\.)?pornhub\.com/(?:view_video\.php\?viewkey=|embed/)(?P<id>[0-9a-z]+)'
24 _TESTS = [{
25 'url': 'http://www.pornhub.com/view_video.php?viewkey=648719015',
26 'md5': '882f488fa1f0026f023f33576004a2ed',
27 'info_dict': {
28 'id': '648719015',
29 'ext': 'mp4',
30 "uploader": "Babes",
31 "title": "Seductive Indian beauty strips down and fingers her pink pussy",
32 "age_limit": 18
33 }
34 }, {
35 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph557bbb6676d2d',
36 'only_matching': True,
37 }]
38
39 @classmethod
40 def _extract_url(cls, webpage):
41 mobj = re.search(
42 r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?pornhub\.com/embed/\d+)\1', webpage)
43 if mobj:
44 return mobj.group('url')
45
46 def _extract_count(self, pattern, webpage, name):
47 return str_to_int(self._search_regex(
48 pattern, webpage, '%s count' % name, fatal=False))
49
50 def _real_extract(self, url):
51 video_id = self._match_id(url)
52
53 req = compat_urllib_request.Request(
54 'http://www.pornhub.com/view_video.php?viewkey=%s' % video_id)
55 req.add_header('Cookie', 'age_verified=1')
56 webpage = self._download_webpage(req, video_id)
57
58 error_msg = self._html_search_regex(
59 r'(?s)<div class="userMessageSection[^"]*".*?>(.*?)</div>',
60 webpage, 'error message', default=None)
61 if error_msg:
62 error_msg = re.sub(r'\s+', ' ', error_msg)
63 raise ExtractorError(
64 'PornHub said: %s' % error_msg,
65 expected=True, video_id=video_id)
66
67 video_title = self._html_search_regex(r'<h1 [^>]+>([^<]+)', webpage, 'title')
68 video_uploader = self._html_search_regex(
69 r'(?s)From:&nbsp;.+?<(?:a href="/users/|a href="/channels/|span class="username)[^>]+>(.+?)<',
70 webpage, 'uploader', fatal=False)
71 thumbnail = self._html_search_regex(r'"image_url":"([^"]+)', webpage, 'thumbnail', fatal=False)
72 if thumbnail:
73 thumbnail = compat_urllib_parse_unquote(thumbnail)
74
75 view_count = self._extract_count(
76 r'<span class="count">([\d,\.]+)</span> views', webpage, 'view')
77 like_count = self._extract_count(
78 r'<span class="votesUp">([\d,\.]+)</span>', webpage, 'like')
79 dislike_count = self._extract_count(
80 r'<span class="votesDown">([\d,\.]+)</span>', webpage, 'dislike')
81 comment_count = self._extract_count(
82 r'All Comments\s*<span>\(([\d,.]+)\)', webpage, 'comment')
83
84 video_urls = list(map(compat_urllib_parse_unquote, re.findall(r'"quality_[0-9]{3}p":"([^"]+)', webpage)))
85 if webpage.find('"encrypted":true') != -1:
86 password = compat_urllib_parse_unquote_plus(
87 self._search_regex(r'"video_title":"([^"]+)', webpage, 'password'))
88 video_urls = list(map(lambda s: aes_decrypt_text(s, password, 32).decode('utf-8'), video_urls))
89
90 formats = []
91 for video_url in video_urls:
92 path = compat_urllib_parse_urlparse(video_url).path
93 extension = os.path.splitext(path)[1][1:]
94 format = path.split('/')[5].split('_')[:2]
95 format = "-".join(format)
96
97 m = re.match(r'^(?P<height>[0-9]+)P-(?P<tbr>[0-9]+)K$', format)
98 if m is None:
99 height = None
100 tbr = None
101 else:
102 height = int(m.group('height'))
103 tbr = int(m.group('tbr'))
104
105 formats.append({
106 'url': video_url,
107 'ext': extension,
108 'format': format,
109 'format_id': format,
110 'tbr': tbr,
111 'height': height,
112 })
113 self._sort_formats(formats)
114
115 return {
116 'id': video_id,
117 'uploader': video_uploader,
118 'title': video_title,
119 'thumbnail': thumbnail,
120 'view_count': view_count,
121 'like_count': like_count,
122 'dislike_count': dislike_count,
123 'comment_count': comment_count,
124 'formats': formats,
125 'age_limit': 18,
126 }
127
128
129 class PornHubPlaylistIE(InfoExtractor):
130 _VALID_URL = r'https?://(?:www\.)?pornhub\.com/playlist/(?P<id>\d+)'
131 _TESTS = [{
132 'url': 'http://www.pornhub.com/playlist/6201671',
133 'info_dict': {
134 'id': '6201671',
135 'title': 'P0p4',
136 },
137 'playlist_mincount': 35,
138 }]
139
140 def _real_extract(self, url):
141 playlist_id = self._match_id(url)
142
143 webpage = self._download_webpage(url, playlist_id)
144
145 entries = [
146 self.url_result('http://www.pornhub.com/%s' % video_url, 'PornHub')
147 for video_url in set(re.findall('href="/?(view_video\.php\?viewkey=\d+[^"]*)"', webpage))
148 ]
149
150 playlist = self._parse_json(
151 self._search_regex(
152 r'playlistObject\s*=\s*({.+?});', webpage, 'playlist'),
153 playlist_id)
154
155 return self.playlist_result(
156 entries, playlist_id, playlist.get('title'), playlist.get('description'))