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.

174 lines
6.3 KiB

10 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. unified_strdate,
  6. str_to_int,
  7. int_or_none,
  8. parse_duration,
  9. )
  10. class XHamsterIE(InfoExtractor):
  11. _VALID_URL = r'(?P<proto>https?)://(?:.+?\.)?xhamster\.com/movies/(?P<id>[0-9]+)/(?P<seo>.+?)\.html(?:\?.*)?'
  12. _TESTS = [
  13. {
  14. 'url': 'http://xhamster.com/movies/1509445/femaleagent_shy_beauty_takes_the_bait.html',
  15. 'info_dict': {
  16. 'id': '1509445',
  17. 'ext': 'mp4',
  18. 'title': 'FemaleAgent Shy beauty takes the bait',
  19. 'upload_date': '20121014',
  20. 'uploader': 'Ruseful2011',
  21. 'duration': 893,
  22. 'age_limit': 18,
  23. }
  24. },
  25. {
  26. 'url': 'http://xhamster.com/movies/2221348/britney_spears_sexy_booty.html?hd',
  27. 'info_dict': {
  28. 'id': '2221348',
  29. 'ext': 'mp4',
  30. 'title': 'Britney Spears Sexy Booty',
  31. 'upload_date': '20130914',
  32. 'uploader': 'jojo747400',
  33. 'duration': 200,
  34. 'age_limit': 18,
  35. }
  36. },
  37. {
  38. 'url': 'https://xhamster.com/movies/2272726/amber_slayed_by_the_knight.html',
  39. 'only_matching': True,
  40. },
  41. ]
  42. def _real_extract(self, url):
  43. def extract_video_url(webpage, name):
  44. return self._search_regex(
  45. [r'''file\s*:\s*(?P<q>["'])(?P<mp4>.+?)(?P=q)''',
  46. r'''<a\s+href=(?P<q>["'])(?P<mp4>.+?)(?P=q)\s+class=["']mp4Thumb''',
  47. r'''<video[^>]+file=(?P<q>["'])(?P<mp4>.+?)(?P=q)[^>]*>'''],
  48. webpage, name, group='mp4')
  49. def is_hd(webpage):
  50. return '<div class=\'icon iconHD\'' in webpage
  51. mobj = re.match(self._VALID_URL, url)
  52. video_id = mobj.group('id')
  53. seo = mobj.group('seo')
  54. proto = mobj.group('proto')
  55. mrss_url = '%s://xhamster.com/movies/%s/%s.html' % (proto, video_id, seo)
  56. webpage = self._download_webpage(mrss_url, video_id)
  57. title = self._html_search_regex(
  58. [r'<title>(?P<title>.+?)(?:, (?:[^,]+? )?Porn: xHamster| - xHamster\.com)</title>',
  59. r'<h1>([^<]+)</h1>'], webpage, 'title')
  60. # Only a few videos have an description
  61. mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
  62. description = mobj.group(1) if mobj else None
  63. upload_date = self._html_search_regex(r'hint=\'(\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}\'',
  64. webpage, 'upload date', fatal=False)
  65. if upload_date:
  66. upload_date = unified_strdate(upload_date)
  67. uploader = self._html_search_regex(
  68. r"<a href='[^']+xhamster\.com/user/[^>]+>(?P<uploader>[^<]+)",
  69. webpage, 'uploader', default='anonymous')
  70. thumbnail = self._search_regex(
  71. [r'''thumb\s*:\s*(?P<q>["'])(?P<thumbnail>.+?)(?P=q)''',
  72. r'''<video[^>]+poster=(?P<q>["'])(?P<thumbnail>.+?)(?P=q)[^>]*>'''],
  73. webpage, 'thumbnail', fatal=False, group='thumbnail')
  74. duration = parse_duration(self._html_search_regex(r'<span>Runtime:</span> (\d+:\d+)</div>',
  75. webpage, 'duration', fatal=False))
  76. view_count = self._html_search_regex(r'<span>Views:</span> ([^<]+)</div>', webpage, 'view count', fatal=False)
  77. if view_count:
  78. view_count = str_to_int(view_count)
  79. mobj = re.search(r"hint='(?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes'", webpage)
  80. (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
  81. mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
  82. comment_count = mobj.group('commentcount') if mobj else 0
  83. age_limit = self._rta_search(webpage)
  84. hd = is_hd(webpage)
  85. format_id = 'hd' if hd else 'sd'
  86. video_url = extract_video_url(webpage, format_id)
  87. formats = [{
  88. 'url': video_url,
  89. 'format_id': 'hd' if hd else 'sd',
  90. 'preference': 1,
  91. }]
  92. if not hd:
  93. mrss_url = self._search_regex(r'<link rel="canonical" href="([^"]+)', webpage, 'mrss_url')
  94. webpage = self._download_webpage(mrss_url + '?hd', video_id, note='Downloading HD webpage')
  95. if is_hd(webpage):
  96. video_url = extract_video_url(webpage, 'hd')
  97. formats.append({
  98. 'url': video_url,
  99. 'format_id': 'hd',
  100. 'preference': 2,
  101. })
  102. self._sort_formats(formats)
  103. return {
  104. 'id': video_id,
  105. 'title': title,
  106. 'description': description,
  107. 'upload_date': upload_date,
  108. 'uploader': uploader,
  109. 'thumbnail': thumbnail,
  110. 'duration': duration,
  111. 'view_count': view_count,
  112. 'like_count': int_or_none(like_count),
  113. 'dislike_count': int_or_none(dislike_count),
  114. 'comment_count': int_or_none(comment_count),
  115. 'age_limit': age_limit,
  116. 'formats': formats,
  117. }
  118. class XHamsterEmbedIE(InfoExtractor):
  119. _VALID_URL = r'https?://(?:www\.)?xhamster\.com/xembed\.php\?video=(?P<id>\d+)'
  120. _TEST = {
  121. 'url': 'http://xhamster.com/xembed.php?video=3328539',
  122. 'info_dict': {
  123. 'id': '3328539',
  124. 'ext': 'mp4',
  125. 'title': 'Pen Masturbation',
  126. 'upload_date': '20140728',
  127. 'uploader_id': 'anonymous',
  128. 'duration': 5,
  129. 'age_limit': 18,
  130. }
  131. }
  132. @staticmethod
  133. def _extract_urls(webpage):
  134. return [url for _, url in re.findall(
  135. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?xhamster\.com/xembed\.php\?video=\d+)\1',
  136. webpage)]
  137. def _real_extract(self, url):
  138. video_id = self._match_id(url)
  139. webpage = self._download_webpage(url, video_id)
  140. video_url = self._search_regex(
  141. r'href="(https?://xhamster\.com/movies/%s/[^"]+\.html[^"]*)"' % video_id,
  142. webpage, 'xhamster url')
  143. return self.url_result(video_url, 'XHamster')