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.

129 lines
4.5 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. compat_urllib_parse,
  6. unescapeHTML,
  7. ExtractorError,
  8. )
  9. class XHamsterIE(InfoExtractor):
  10. """Information Extractor for xHamster"""
  11. _VALID_URL = r'(?:http://)?(?:www\.)?xhamster\.com/movies/(?P<id>[0-9]+)/(?P<seo>.+?)\.html(?:\?.*)?'
  12. _TESTS = [{
  13. 'url': 'http://xhamster.com/movies/1509445/femaleagent_shy_beauty_takes_the_bait.html',
  14. 'file': '1509445.mp4',
  15. 'md5': '8281348b8d3c53d39fffb377d24eac4e',
  16. 'info_dict': {
  17. "upload_date": "20121014",
  18. "uploader_id": "Ruseful2011",
  19. "title": "FemaleAgent Shy beauty takes the bait",
  20. "age_limit": 18,
  21. }
  22. },
  23. {
  24. 'url': 'http://xhamster.com/movies/2221348/britney_spears_sexy_booty.html?hd',
  25. 'file': '2221348.flv',
  26. 'md5': 'e767b9475de189320f691f49c679c4c7',
  27. 'info_dict': {
  28. "upload_date": "20130914",
  29. "uploader_id": "jojo747400",
  30. "title": "Britney Spears Sexy Booty",
  31. "age_limit": 18,
  32. }
  33. }]
  34. def _real_extract(self,url):
  35. def extract_video_url(webpage):
  36. mobj = re.search(r'\'srv\': \'(?P<server>[^\']*)\',\s*\'file\': \'(?P<file>[^\']+)\',', webpage)
  37. if mobj is None:
  38. raise ExtractorError('Unable to extract media URL')
  39. if len(mobj.group('server')) == 0:
  40. return compat_urllib_parse.unquote(mobj.group('file'))
  41. else:
  42. return mobj.group('server')+'/key='+mobj.group('file')
  43. def extract_mp4_video_url(webpage):
  44. mp4 = re.search(r'<a href=\"(.+?)\" class=\"mp4Play\"',webpage)
  45. if mp4 is None:
  46. return None
  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. video_title = self._html_search_regex(
  57. 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. video_description = mobj.group(1) if mobj else None
  61. mobj = re.search(r'hint=\'(?P<upload_date_Y>[0-9]{4})-(?P<upload_date_m>[0-9]{2})-(?P<upload_date_d>[0-9]{2}) [0-9]{2}:[0-9]{2}:[0-9]{2} [A-Z]{3,4}\'', webpage)
  62. if mobj:
  63. video_upload_date = mobj.group('upload_date_Y')+mobj.group('upload_date_m')+mobj.group('upload_date_d')
  64. else:
  65. video_upload_date = None
  66. self._downloader.report_warning('Unable to extract upload date')
  67. video_uploader_id = self._html_search_regex(
  68. r'<a href=\'/user/[^>]+>(?P<uploader_id>[^<]+)',
  69. webpage, 'uploader id', default='anonymous')
  70. video_thumbnail = self._search_regex(
  71. r'\'image\':\'(?P<thumbnail>[^\']+)\'',
  72. webpage, 'thumbnail', fatal=False)
  73. age_limit = self._rta_search(webpage)
  74. hd = is_hd(webpage)
  75. video_url = extract_video_url(webpage)
  76. formats = [{
  77. 'url': video_url,
  78. 'format_id': 'hd' if hd else 'sd',
  79. 'preference': 0,
  80. }]
  81. video_mp4_url = extract_mp4_video_url(webpage)
  82. if video_mp4_url is not None:
  83. formats.append({
  84. 'url': video_mp4_url,
  85. 'ext': 'mp4',
  86. 'format_id': 'mp4-hd' if hd else 'mp4-sd',
  87. 'preference': 1,
  88. })
  89. if not hd:
  90. webpage = self._download_webpage(
  91. mrss_url + '?hd', video_id, note='Downloading HD webpage')
  92. if is_hd(webpage):
  93. video_url = extract_video_url(webpage)
  94. formats.append({
  95. 'url': video_url,
  96. 'format_id': 'hd',
  97. 'preference': 2,
  98. })
  99. self._sort_formats(formats)
  100. return {
  101. 'id': video_id,
  102. 'title': video_title,
  103. 'formats': formats,
  104. 'description': video_description,
  105. 'upload_date': video_upload_date,
  106. 'uploader_id': video_uploader_id,
  107. 'thumbnail': video_thumbnail,
  108. 'age_limit': age_limit,
  109. }