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.

130 lines
4.6 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://(?:.+?\.)?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. def _real_extract(self,url):
  41. def extract_video_url(webpage):
  42. mp4 = re.search(r'<video\s+.*?file="([^"]+)".*?>', webpage)
  43. if mp4 is None:
  44. raise ExtractorError('Unable to extract media URL')
  45. else:
  46. return mp4.group(1)
  47. def is_hd(webpage):
  48. return '<div class=\'icon iconHD\'' in webpage
  49. mobj = re.match(self._VALID_URL, url)
  50. video_id = mobj.group('id')
  51. seo = mobj.group('seo')
  52. mrss_url = 'http://xhamster.com/movies/%s/%s.html' % (video_id, seo)
  53. webpage = self._download_webpage(mrss_url, video_id)
  54. title = self._html_search_regex(r'<title>(?P<title>.+?) - xHamster\.com</title>', webpage, 'title')
  55. # Only a few videos have an description
  56. mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
  57. description = mobj.group(1) if mobj else None
  58. upload_date = self._html_search_regex(r'hint=\'(\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}\'',
  59. webpage, 'upload date', fatal=False)
  60. if upload_date:
  61. upload_date = unified_strdate(upload_date)
  62. uploader_id = self._html_search_regex(r'<a href=\'/user/[^>]+>(?P<uploader_id>[^<]+)',
  63. webpage, 'uploader id', default='anonymous')
  64. thumbnail = self._html_search_regex(r'<video\s+.*?poster="([^"]+)".*?>', webpage, 'thumbnail', fatal=False)
  65. duration = parse_duration(self._html_search_regex(r'<span>Runtime:</span> (\d+:\d+)</div>',
  66. webpage, 'duration', fatal=False))
  67. view_count = self._html_search_regex(r'<span>Views:</span> ([^<]+)</div>', webpage, 'view count', fatal=False)
  68. if view_count:
  69. view_count = str_to_int(view_count)
  70. mobj = re.search(r"hint='(?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes'", webpage)
  71. (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
  72. mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
  73. comment_count = mobj.group('commentcount') if mobj else 0
  74. age_limit = self._rta_search(webpage)
  75. hd = is_hd(webpage)
  76. video_url = extract_video_url(webpage)
  77. formats = [{
  78. 'url': video_url,
  79. 'format_id': 'hd' if hd else 'sd',
  80. 'preference': 1,
  81. }]
  82. if not hd:
  83. mrss_url = self._search_regex(r'<link rel="canonical" href="([^"]+)', webpage, 'mrss_url')
  84. webpage = self._download_webpage(mrss_url + '?hd', video_id, note='Downloading HD webpage')
  85. if is_hd(webpage):
  86. video_url = extract_video_url(webpage)
  87. formats.append({
  88. 'url': video_url,
  89. 'format_id': 'hd',
  90. 'preference': 2,
  91. })
  92. self._sort_formats(formats)
  93. return {
  94. 'id': video_id,
  95. 'title': title,
  96. 'description': description,
  97. 'upload_date': upload_date,
  98. 'uploader_id': uploader_id,
  99. 'thumbnail': thumbnail,
  100. 'duration': duration,
  101. 'view_count': view_count,
  102. 'like_count': int_or_none(like_count),
  103. 'dislike_count': int_or_none(dislike_count),
  104. 'comment_count': int_or_none(comment_count),
  105. 'age_limit': age_limit,
  106. 'formats': formats,
  107. }