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.

172 lines
6.2 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. 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(r'<title>(?P<title>.+?) - xHamster\.com</title>', webpage, 'title')
  58. # Only a few videos have an description
  59. mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
  60. description = mobj.group(1) if mobj else None
  61. upload_date = self._html_search_regex(r'hint=\'(\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}\'',
  62. webpage, 'upload date', fatal=False)
  63. if upload_date:
  64. upload_date = unified_strdate(upload_date)
  65. uploader = self._html_search_regex(
  66. r"<a href='[^']+xhamster\.com/user/[^>]+>(?P<uploader>[^<]+)",
  67. webpage, 'uploader', default='anonymous')
  68. thumbnail = self._search_regex(
  69. [r'''thumb\s*:\s*(?P<q>["'])(?P<thumbnail>.+?)(?P=q)''',
  70. r'''<video[^>]+poster=(?P<q>["'])(?P<thumbnail>.+?)(?P=q)[^>]*>'''],
  71. webpage, 'thumbnail', fatal=False, group='thumbnail')
  72. duration = parse_duration(self._html_search_regex(r'<span>Runtime:</span> (\d+:\d+)</div>',
  73. webpage, 'duration', fatal=False))
  74. view_count = self._html_search_regex(r'<span>Views:</span> ([^<]+)</div>', webpage, 'view count', fatal=False)
  75. if view_count:
  76. view_count = str_to_int(view_count)
  77. mobj = re.search(r"hint='(?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes'", webpage)
  78. (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
  79. mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
  80. comment_count = mobj.group('commentcount') if mobj else 0
  81. age_limit = self._rta_search(webpage)
  82. hd = is_hd(webpage)
  83. format_id = 'hd' if hd else 'sd'
  84. video_url = extract_video_url(webpage, format_id)
  85. formats = [{
  86. 'url': video_url,
  87. 'format_id': 'hd' if hd else 'sd',
  88. 'preference': 1,
  89. }]
  90. if not hd:
  91. mrss_url = self._search_regex(r'<link rel="canonical" href="([^"]+)', webpage, 'mrss_url')
  92. webpage = self._download_webpage(mrss_url + '?hd', video_id, note='Downloading HD webpage')
  93. if is_hd(webpage):
  94. video_url = extract_video_url(webpage, 'hd')
  95. formats.append({
  96. 'url': video_url,
  97. 'format_id': 'hd',
  98. 'preference': 2,
  99. })
  100. self._sort_formats(formats)
  101. return {
  102. 'id': video_id,
  103. 'title': title,
  104. 'description': description,
  105. 'upload_date': upload_date,
  106. 'uploader': uploader,
  107. 'thumbnail': thumbnail,
  108. 'duration': duration,
  109. 'view_count': view_count,
  110. 'like_count': int_or_none(like_count),
  111. 'dislike_count': int_or_none(dislike_count),
  112. 'comment_count': int_or_none(comment_count),
  113. 'age_limit': age_limit,
  114. 'formats': formats,
  115. }
  116. class XHamsterEmbedIE(InfoExtractor):
  117. _VALID_URL = r'https?://(?:www\.)?xhamster\.com/xembed\.php\?video=(?P<id>\d+)'
  118. _TEST = {
  119. 'url': 'http://xhamster.com/xembed.php?video=3328539',
  120. 'info_dict': {
  121. 'id': '3328539',
  122. 'ext': 'mp4',
  123. 'title': 'Pen Masturbation',
  124. 'upload_date': '20140728',
  125. 'uploader_id': 'anonymous',
  126. 'duration': 5,
  127. 'age_limit': 18,
  128. }
  129. }
  130. @staticmethod
  131. def _extract_urls(webpage):
  132. return [url for _, url in re.findall(
  133. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?xhamster\.com/xembed\.php\?video=\d+)\1',
  134. webpage)]
  135. def _real_extract(self, url):
  136. video_id = self._match_id(url)
  137. webpage = self._download_webpage(url, video_id)
  138. video_url = self._search_regex(
  139. r'href="(https?://xhamster\.com/movies/%s/[^"]+\.html[^"]*)"' % video_id,
  140. webpage, 'xhamster url')
  141. return self.url_result(video_url, 'XHamster')