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.

143 lines
5.0 KiB

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