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.

132 lines
4.7 KiB

  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. """Information Extractor for xHamster"""
  13. _VALID_URL = r'http://(?:www\.)?xhamster\.com/movies/(?P<id>[0-9]+)/(?P<seo>.+?)\.html(?:\?.*)?'
  14. _TESTS = [
  15. {
  16. 'url': 'http://xhamster.com/movies/1509445/femaleagent_shy_beauty_takes_the_bait.html',
  17. 'md5': '8281348b8d3c53d39fffb377d24eac4e',
  18. 'info_dict': {
  19. 'id': '1509445',
  20. 'ext': 'mp4',
  21. 'title': 'FemaleAgent Shy beauty takes the bait',
  22. 'upload_date': '20121014',
  23. 'uploader_id': 'Ruseful2011',
  24. 'duration': 893,
  25. 'age_limit': 18,
  26. }
  27. },
  28. {
  29. 'url': 'http://xhamster.com/movies/2221348/britney_spears_sexy_booty.html?hd',
  30. 'md5': '4cbd8d56708ecb4fb4124c23e4acb81a',
  31. 'info_dict': {
  32. 'id': '2221348',
  33. 'ext': 'mp4',
  34. 'title': 'Britney Spears Sexy Booty',
  35. 'upload_date': '20130914',
  36. 'uploader_id': 'jojo747400',
  37. 'duration': 200,
  38. 'age_limit': 18,
  39. }
  40. }
  41. ]
  42. def _real_extract(self,url):
  43. def extract_video_url(webpage):
  44. mp4 = re.search(r'<video\s+.*?file="([^"]+)".*?>', webpage)
  45. if mp4 is None:
  46. raise ExtractorError('Unable to extract media URL')
  47. else:
  48. return mp4.group(1)
  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. mrss_url = 'http://xhamster.com/movies/%s/%s.html' % (video_id, seo)
  55. webpage = self._download_webpage(mrss_url, video_id)
  56. title = self._html_search_regex(r'<title>(?P<title>.+?) - xHamster\.com</title>', webpage, 'title')
  57. # Only a few videos have an description
  58. mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
  59. description = mobj.group(1) if mobj else None
  60. upload_date = self._html_search_regex(r'hint=\'(\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}\'',
  61. webpage, 'upload date', fatal=False)
  62. if upload_date:
  63. upload_date = unified_strdate(upload_date)
  64. uploader_id = self._html_search_regex(r'<a href=\'/user/[^>]+>(?P<uploader_id>[^<]+)',
  65. webpage, 'uploader id', default='anonymous')
  66. thumbnail = self._html_search_regex(r'<video\s+.*?poster="([^"]+)".*?>', webpage, 'thumbnail', fatal=False)
  67. duration = parse_duration(self._html_search_regex(r'<span>Runtime:</span> (\d+:\d+)</div>',
  68. webpage, 'duration', fatal=False))
  69. view_count = self._html_search_regex(r'<span>Views:</span> ([^<]+)</div>', webpage, 'view count', fatal=False)
  70. if view_count:
  71. view_count = str_to_int(view_count)
  72. mobj = re.search(r"hint='(?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes'", webpage)
  73. (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
  74. mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
  75. comment_count = mobj.group('commentcount') if mobj else 0
  76. age_limit = self._rta_search(webpage)
  77. hd = is_hd(webpage)
  78. video_url = extract_video_url(webpage)
  79. formats = [{
  80. 'url': video_url,
  81. 'format_id': 'hd' if hd else 'sd',
  82. 'preference': 1,
  83. }]
  84. if not hd:
  85. mrss_url = self._search_regex(r'<link rel="canonical" href="([^"]+)', webpage, 'mrss_url')
  86. webpage = self._download_webpage(mrss_url + '?hd', video_id, note='Downloading HD webpage')
  87. if is_hd(webpage):
  88. video_url = extract_video_url(webpage)
  89. formats.append({
  90. 'url': video_url,
  91. 'format_id': 'hd',
  92. 'preference': 2,
  93. })
  94. self._sort_formats(formats)
  95. return {
  96. 'id': video_id,
  97. 'title': title,
  98. 'description': description,
  99. 'upload_date': upload_date,
  100. 'uploader_id': uploader_id,
  101. 'thumbnail': thumbnail,
  102. 'duration': duration,
  103. 'view_count': view_count,
  104. 'like_count': int_or_none(like_count),
  105. 'dislike_count': int_or_none(dislike_count),
  106. 'comment_count': int_or_none(comment_count),
  107. 'age_limit': age_limit,
  108. 'formats': formats,
  109. }