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.

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