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.

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