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.

144 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(
  64. self._search_regex(r'"video_title":"([^"]+)', webpage, 'password'))
  65. video_urls = list(map(lambda s: aes_decrypt_text(s, password, 32).decode('utf-8'), video_urls))
  66. formats = []
  67. for video_url in video_urls:
  68. path = compat_urllib_parse_urlparse(video_url).path
  69. extension = os.path.splitext(path)[1][1:]
  70. format = path.split('/')[5].split('_')[:2]
  71. format = "-".join(format)
  72. m = re.match(r'^(?P<height>[0-9]+)P-(?P<tbr>[0-9]+)K$', format)
  73. if m is None:
  74. height = None
  75. tbr = None
  76. else:
  77. height = int(m.group('height'))
  78. tbr = int(m.group('tbr'))
  79. formats.append({
  80. 'url': video_url,
  81. 'ext': extension,
  82. 'format': format,
  83. 'format_id': format,
  84. 'tbr': tbr,
  85. 'height': height,
  86. })
  87. self._sort_formats(formats)
  88. return {
  89. 'id': video_id,
  90. 'uploader': video_uploader,
  91. 'title': video_title,
  92. 'thumbnail': thumbnail,
  93. 'view_count': view_count,
  94. 'like_count': like_count,
  95. 'dislike_count': dislike_count,
  96. 'comment_count': comment_count,
  97. 'formats': formats,
  98. 'age_limit': 18,
  99. }
  100. class PornHubPlaylistIE(InfoExtractor):
  101. _VALID_URL = r'https?://(?:www\.)?pornhub\.com/playlist/(?P<id>\d+)'
  102. _TESTS = [{
  103. 'url': 'http://www.pornhub.com/playlist/6201671',
  104. 'info_dict': {
  105. 'id': '6201671',
  106. 'title': 'P0p4',
  107. },
  108. 'playlist_mincount': 35,
  109. }]
  110. def _real_extract(self, url):
  111. playlist_id = self._match_id(url)
  112. webpage = self._download_webpage(url, playlist_id)
  113. entries = [
  114. self.url_result('http://www.pornhub.com/%s' % video_url, 'PornHub')
  115. for video_url in set(re.findall('href="/?(view_video\.php\?viewkey=\d+[^"]*)"', webpage))
  116. ]
  117. playlist = self._parse_json(
  118. self._search_regex(
  119. r'playlistObject\s*=\s*({.+?});', webpage, 'playlist'),
  120. playlist_id)
  121. return self.playlist_result(
  122. entries, playlist_id, playlist.get('title'), playlist.get('description'))