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