You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
5.5 KiB

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