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.

111 lines
4.1 KiB

  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. compat_urllib_parse,
  5. unescapeHTML,
  6. determine_ext,
  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. u'url': u'http://xhamster.com/movies/1509445/femaleagent_shy_beauty_takes_the_bait.html',
  14. u'file': u'1509445.flv',
  15. u'md5': u'9f48e0e8d58e3076bb236ff412ab62fa',
  16. u'info_dict': {
  17. u"upload_date": u"20121014",
  18. u"uploader_id": u"Ruseful2011",
  19. u"title": u"FemaleAgent Shy beauty takes the bait",
  20. u"age_limit": 18,
  21. }
  22. },
  23. {
  24. u'url': u'http://xhamster.com/movies/2221348/britney_spears_sexy_booty.html?hd',
  25. u'file': u'2221348.flv',
  26. u'md5': u'e767b9475de189320f691f49c679c4c7',
  27. u'info_dict': {
  28. u"upload_date": u"20130914",
  29. u"uploader_id": u"jojo747400",
  30. u"title": u"Britney Spears Sexy Booty",
  31. u"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(u'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 is_hd(webpage):
  44. return webpage.find('<div class=\'icon iconHD\'>') != -1
  45. mobj = re.match(self._VALID_URL, url)
  46. video_id = mobj.group('id')
  47. seo = mobj.group('seo')
  48. mrss_url = 'http://xhamster.com/movies/%s/%s.html' % (video_id, seo)
  49. webpage = self._download_webpage(mrss_url, video_id)
  50. video_title = self._html_search_regex(r'<title>(?P<title>.+?) - xHamster\.com</title>',
  51. webpage, u'title')
  52. # Only a few videos have an description
  53. mobj = re.search('<span>Description: </span>(?P<description>[^<]+)', webpage)
  54. if mobj:
  55. video_description = unescapeHTML(mobj.group('description'))
  56. else:
  57. video_description = None
  58. 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)
  59. if mobj:
  60. video_upload_date = mobj.group('upload_date_Y')+mobj.group('upload_date_m')+mobj.group('upload_date_d')
  61. else:
  62. video_upload_date = None
  63. self._downloader.report_warning(u'Unable to extract upload date')
  64. video_uploader_id = self._html_search_regex(r'<a href=\'/user/[^>]+>(?P<uploader_id>[^<]+)',
  65. webpage, u'uploader id', default=u'anonymous')
  66. video_thumbnail = self._search_regex(r'\'image\':\'(?P<thumbnail>[^\']+)\'',
  67. webpage, u'thumbnail', fatal=False)
  68. age_limit = self._rta_search(webpage)
  69. video_url = extract_video_url(webpage)
  70. hd = is_hd(webpage)
  71. formats = [{
  72. 'url': video_url,
  73. 'ext': determine_ext(video_url),
  74. 'format': 'hd' if hd else 'sd',
  75. 'format_id': 'hd' if hd else 'sd',
  76. }]
  77. if not hd:
  78. webpage = self._download_webpage(mrss_url+'?hd', video_id)
  79. if is_hd(webpage):
  80. video_url = extract_video_url(webpage)
  81. formats.append({
  82. 'url': video_url,
  83. 'ext': determine_ext(video_url),
  84. 'format': 'hd',
  85. 'format_id': 'hd',
  86. })
  87. return {
  88. 'id': video_id,
  89. 'title': video_title,
  90. 'formats': formats,
  91. 'description': video_description,
  92. 'upload_date': video_upload_date,
  93. 'uploader_id': video_uploader_id,
  94. 'thumbnail': video_thumbnail,
  95. 'age_limit': age_limit,
  96. }