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.

102 lines
3.7 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. str_to_int,
  12. )
  13. from ..aes import (
  14. aes_decrypt_text
  15. )
  16. class PornHubIE(InfoExtractor):
  17. _VALID_URL = r'https?://(?:www\.)?pornhub\.com/view_video\.php\?viewkey=(?P<id>[0-9a-f]+)'
  18. _TEST = {
  19. 'url': 'http://www.pornhub.com/view_video.php?viewkey=648719015',
  20. 'md5': '882f488fa1f0026f023f33576004a2ed',
  21. 'info_dict': {
  22. 'id': '648719015',
  23. 'ext': 'mp4',
  24. "uploader": "Babes",
  25. "title": "Seductive Indian beauty strips down and fingers her pink pussy",
  26. "age_limit": 18
  27. }
  28. }
  29. def _extract_count(self, pattern, webpage, name):
  30. count = self._html_search_regex(pattern, webpage, '%s count' % name, fatal=False)
  31. if count:
  32. count = str_to_int(count)
  33. return count
  34. def _real_extract(self, url):
  35. video_id = self._match_id(url)
  36. req = compat_urllib_request.Request(url)
  37. req.add_header('Cookie', 'age_verified=1')
  38. webpage = self._download_webpage(req, video_id)
  39. video_title = self._html_search_regex(r'<h1 [^>]+>([^<]+)', webpage, 'title')
  40. video_uploader = self._html_search_regex(
  41. r'(?s)From:&nbsp;.+?<(?:a href="/users/|a href="/channels/|<span class="username)[^>]+>(.+?)<',
  42. webpage, 'uploader', fatal=False)
  43. thumbnail = self._html_search_regex(r'"image_url":"([^"]+)', webpage, 'thumbnail', fatal=False)
  44. if thumbnail:
  45. thumbnail = compat_urllib_parse.unquote(thumbnail)
  46. view_count = self._extract_count(r'<span class="count">([\d,\.]+)</span> views', webpage, 'view')
  47. like_count = self._extract_count(r'<span class="votesUp">([\d,\.]+)</span>', webpage, 'like')
  48. dislike_count = self._extract_count(r'<span class="votesDown">([\d,\.]+)</span>', webpage, 'dislike')
  49. comment_count = self._extract_count(
  50. r'All comments \(<var class="videoCommentCount">([\d,\.]+)</var>', webpage, 'comment')
  51. video_urls = list(map(compat_urllib_parse.unquote, re.findall(r'"quality_[0-9]{3}p":"([^"]+)', webpage)))
  52. if webpage.find('"encrypted":true') != -1:
  53. password = compat_urllib_parse.unquote_plus(self._html_search_regex(r'"video_title":"([^"]+)', webpage, 'password'))
  54. video_urls = list(map(lambda s: aes_decrypt_text(s, password, 32).decode('utf-8'), video_urls))
  55. formats = []
  56. for video_url in video_urls:
  57. path = compat_urllib_parse_urlparse(video_url).path
  58. extension = os.path.splitext(path)[1][1:]
  59. format = path.split('/')[5].split('_')[:2]
  60. format = "-".join(format)
  61. m = re.match(r'^(?P<height>[0-9]+)P-(?P<tbr>[0-9]+)K$', format)
  62. if m is None:
  63. height = None
  64. tbr = None
  65. else:
  66. height = int(m.group('height'))
  67. tbr = int(m.group('tbr'))
  68. formats.append({
  69. 'url': video_url,
  70. 'ext': extension,
  71. 'format': format,
  72. 'format_id': format,
  73. 'tbr': tbr,
  74. 'height': height,
  75. })
  76. self._sort_formats(formats)
  77. return {
  78. 'id': video_id,
  79. 'uploader': video_uploader,
  80. 'title': video_title,
  81. 'thumbnail': thumbnail,
  82. 'view_count': view_count,
  83. 'like_count': like_count,
  84. 'dislike_count': dislike_count,
  85. 'comment_count': comment_count,
  86. 'formats': formats,
  87. 'age_limit': 18,
  88. }