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.

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