pornhub.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import itertools
  4. import os
  5. import re
  6. from .common import InfoExtractor
  7. from ..compat import (
  8. compat_HTTPError,
  9. compat_urllib_parse_unquote,
  10. compat_urllib_parse_unquote_plus,
  11. compat_urllib_parse_urlparse,
  12. )
  13. from ..utils import (
  14. ExtractorError,
  15. int_or_none,
  16. orderedSet,
  17. sanitized_Request,
  18. str_to_int,
  19. )
  20. from ..aes import (
  21. aes_decrypt_text
  22. )
  23. class PornHubIE(InfoExtractor):
  24. _VALID_URL = r'https?://(?:[a-z]+\.)?pornhub\.com/(?:view_video\.php\?viewkey=|embed/)(?P<id>[0-9a-z]+)'
  25. _TESTS = [{
  26. 'url': 'http://www.pornhub.com/view_video.php?viewkey=648719015',
  27. 'md5': '1e19b41231a02eba417839222ac9d58e',
  28. 'info_dict': {
  29. 'id': '648719015',
  30. 'ext': 'mp4',
  31. 'title': 'Seductive Indian beauty strips down and fingers her pink pussy',
  32. 'uploader': 'Babes',
  33. 'duration': 361,
  34. 'view_count': int,
  35. 'like_count': int,
  36. 'dislike_count': int,
  37. 'comment_count': int,
  38. 'age_limit': 18,
  39. },
  40. }, {
  41. # non-ASCII title
  42. 'url': 'http://www.pornhub.com/view_video.php?viewkey=1331683002',
  43. 'info_dict': {
  44. 'id': '1331683002',
  45. 'ext': 'mp4',
  46. 'title': '重庆婷婷女王足交',
  47. 'uploader': 'cj397186295',
  48. 'duration': 1753,
  49. 'view_count': int,
  50. 'like_count': int,
  51. 'dislike_count': int,
  52. 'comment_count': int,
  53. 'age_limit': 18,
  54. },
  55. 'params': {
  56. 'skip_download': True,
  57. },
  58. }, {
  59. 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph557bbb6676d2d',
  60. 'only_matching': True,
  61. }, {
  62. # removed at the request of cam4.com
  63. 'url': 'http://fr.pornhub.com/view_video.php?viewkey=ph55ca2f9760862',
  64. 'only_matching': True,
  65. }, {
  66. # removed at the request of the copyright owner
  67. 'url': 'http://www.pornhub.com/view_video.php?viewkey=788152859',
  68. 'only_matching': True,
  69. }, {
  70. # removed by uploader
  71. 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph572716d15a111',
  72. 'only_matching': True,
  73. }]
  74. @classmethod
  75. def _extract_url(cls, webpage):
  76. mobj = re.search(
  77. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?pornhub\.com/embed/\d+)\1', webpage)
  78. if mobj:
  79. return mobj.group('url')
  80. def _extract_count(self, pattern, webpage, name):
  81. return str_to_int(self._search_regex(
  82. pattern, webpage, '%s count' % name, fatal=False))
  83. def _real_extract(self, url):
  84. video_id = self._match_id(url)
  85. req = sanitized_Request(
  86. 'http://www.pornhub.com/view_video.php?viewkey=%s' % video_id)
  87. req.add_header('Cookie', 'age_verified=1')
  88. webpage = self._download_webpage(req, video_id)
  89. error_msg = self._html_search_regex(
  90. r'(?s)<div[^>]+class=(["\']).*?\bremoved\b.*?\1[^>]*>(?P<error>.+?)</div>',
  91. webpage, 'error message', default=None, group='error')
  92. if error_msg:
  93. error_msg = re.sub(r'\s+', ' ', error_msg)
  94. raise ExtractorError(
  95. 'PornHub said: %s' % error_msg,
  96. expected=True, video_id=video_id)
  97. # video_title from flashvars contains whitespace instead of non-ASCII (see
  98. # http://www.pornhub.com/view_video.php?viewkey=1331683002), not relying
  99. # on that anymore.
  100. title = self._html_search_meta(
  101. 'twitter:title', webpage, default=None) or self._search_regex(
  102. (r'<h1[^>]+class=["\']title["\'][^>]*>(?P<title>[^<]+)',
  103. r'<div[^>]+data-video-title=(["\'])(?P<title>.+?)\1',
  104. r'shareTitle\s*=\s*(["\'])(?P<title>.+?)\1'),
  105. webpage, 'title', group='title')
  106. flashvars = self._parse_json(
  107. self._search_regex(
  108. r'var\s+flashvars_\d+\s*=\s*({.+?});', webpage, 'flashvars', default='{}'),
  109. video_id)
  110. if flashvars:
  111. thumbnail = flashvars.get('image_url')
  112. duration = int_or_none(flashvars.get('video_duration'))
  113. else:
  114. title, thumbnail, duration = [None] * 3
  115. video_uploader = self._html_search_regex(
  116. r'(?s)From:&nbsp;.+?<(?:a href="/users/|a href="/channels/|span class="username)[^>]+>(.+?)<',
  117. webpage, 'uploader', fatal=False)
  118. view_count = self._extract_count(
  119. r'<span class="count">([\d,\.]+)</span> views', webpage, 'view')
  120. like_count = self._extract_count(
  121. r'<span class="votesUp">([\d,\.]+)</span>', webpage, 'like')
  122. dislike_count = self._extract_count(
  123. r'<span class="votesDown">([\d,\.]+)</span>', webpage, 'dislike')
  124. comment_count = self._extract_count(
  125. r'All Comments\s*<span>\(([\d,.]+)\)', webpage, 'comment')
  126. video_urls = list(map(compat_urllib_parse_unquote, re.findall(r"player_quality_[0-9]{3}p\s*=\s*'([^']+)'", webpage)))
  127. if webpage.find('"encrypted":true') != -1:
  128. password = compat_urllib_parse_unquote_plus(
  129. self._search_regex(r'"video_title":"([^"]+)', webpage, 'password'))
  130. video_urls = list(map(lambda s: aes_decrypt_text(s, password, 32).decode('utf-8'), video_urls))
  131. formats = []
  132. for video_url in video_urls:
  133. path = compat_urllib_parse_urlparse(video_url).path
  134. extension = os.path.splitext(path)[1][1:]
  135. format = path.split('/')[5].split('_')[:2]
  136. format = '-'.join(format)
  137. m = re.match(r'^(?P<height>[0-9]+)[pP]-(?P<tbr>[0-9]+)[kK]$', format)
  138. if m is None:
  139. height = None
  140. tbr = None
  141. else:
  142. height = int(m.group('height'))
  143. tbr = int(m.group('tbr'))
  144. formats.append({
  145. 'url': video_url,
  146. 'ext': extension,
  147. 'format': format,
  148. 'format_id': format,
  149. 'tbr': tbr,
  150. 'height': height,
  151. })
  152. self._sort_formats(formats)
  153. return {
  154. 'id': video_id,
  155. 'uploader': video_uploader,
  156. 'title': title,
  157. 'thumbnail': thumbnail,
  158. 'duration': duration,
  159. 'view_count': view_count,
  160. 'like_count': like_count,
  161. 'dislike_count': dislike_count,
  162. 'comment_count': comment_count,
  163. 'formats': formats,
  164. 'age_limit': 18,
  165. }
  166. class PornHubPlaylistBaseIE(InfoExtractor):
  167. def _extract_entries(self, webpage):
  168. return [
  169. self.url_result(
  170. 'http://www.pornhub.com/%s' % video_url,
  171. PornHubIE.ie_key(), video_title=title)
  172. for video_url, title in orderedSet(re.findall(
  173. r'href="/?(view_video\.php\?.*\bviewkey=[\da-z]+[^"]*)"[^>]*\s+title="([^"]+)"',
  174. webpage))
  175. ]
  176. def _real_extract(self, url):
  177. playlist_id = self._match_id(url)
  178. webpage = self._download_webpage(url, playlist_id)
  179. entries = self._extract_entries(webpage)
  180. playlist = self._parse_json(
  181. self._search_regex(
  182. r'playlistObject\s*=\s*({.+?});', webpage, 'playlist'),
  183. playlist_id)
  184. return self.playlist_result(
  185. entries, playlist_id, playlist.get('title'), playlist.get('description'))
  186. class PornHubPlaylistIE(PornHubPlaylistBaseIE):
  187. _VALID_URL = r'https?://(?:www\.)?pornhub\.com/playlist/(?P<id>\d+)'
  188. _TESTS = [{
  189. 'url': 'http://www.pornhub.com/playlist/6201671',
  190. 'info_dict': {
  191. 'id': '6201671',
  192. 'title': 'P0p4',
  193. },
  194. 'playlist_mincount': 35,
  195. }]
  196. class PornHubUserVideosIE(PornHubPlaylistBaseIE):
  197. _VALID_URL = r'https?://(?:www\.)?pornhub\.com/users/(?P<id>[^/]+)/videos'
  198. _TESTS = [{
  199. 'url': 'http://www.pornhub.com/users/zoe_ph/videos/public',
  200. 'info_dict': {
  201. 'id': 'zoe_ph',
  202. },
  203. 'playlist_mincount': 171,
  204. }, {
  205. 'url': 'http://www.pornhub.com/users/rushandlia/videos',
  206. 'only_matching': True,
  207. }]
  208. def _real_extract(self, url):
  209. user_id = self._match_id(url)
  210. entries = []
  211. for page_num in itertools.count(1):
  212. try:
  213. webpage = self._download_webpage(
  214. url, user_id, 'Downloading page %d' % page_num,
  215. query={'page': page_num})
  216. except ExtractorError as e:
  217. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 404:
  218. break
  219. page_entries = self._extract_entries(webpage)
  220. if not page_entries:
  221. break
  222. entries.extend(page_entries)
  223. return self.playlist_result(entries, user_id)