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.

167 lines
5.9 KiB

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