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