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.

175 lines
6.3 KiB

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