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.

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