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.

182 lines
6.6 KiB

10 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. dict_get,
  6. float_or_none,
  7. int_or_none,
  8. unified_strdate,
  9. )
  10. class XHamsterIE(InfoExtractor):
  11. _VALID_URL = r'(?P<proto>https?)://(?:.+?\.)?xhamster\.com/movies/(?P<id>[0-9]+)/(?P<seo>.+?)\.html(?:\?.*)?'
  12. _TESTS = [
  13. {
  14. 'url': 'http://xhamster.com/movies/1509445/femaleagent_shy_beauty_takes_the_bait.html',
  15. 'info_dict': {
  16. 'id': '1509445',
  17. 'ext': 'mp4',
  18. 'title': 'FemaleAgent Shy beauty takes the bait',
  19. 'upload_date': '20121014',
  20. 'uploader': 'Ruseful2011',
  21. 'duration': 893.52,
  22. 'age_limit': 18,
  23. }
  24. },
  25. {
  26. 'url': 'http://xhamster.com/movies/2221348/britney_spears_sexy_booty.html?hd',
  27. 'info_dict': {
  28. 'id': '2221348',
  29. 'ext': 'mp4',
  30. 'title': 'Britney Spears Sexy Booty',
  31. 'upload_date': '20130914',
  32. 'uploader': 'jojo747400',
  33. 'duration': 200.48,
  34. 'age_limit': 18,
  35. }
  36. },
  37. {
  38. 'url': 'https://xhamster.com/movies/2272726/amber_slayed_by_the_knight.html',
  39. 'only_matching': True,
  40. },
  41. ]
  42. def _real_extract(self, url):
  43. def extract_video_url(webpage, name):
  44. return self._search_regex(
  45. [r'''file\s*:\s*(?P<q>["'])(?P<mp4>.+?)(?P=q)''',
  46. r'''<a\s+href=(?P<q>["'])(?P<mp4>.+?)(?P=q)\s+class=["']mp4Thumb''',
  47. r'''<video[^>]+file=(?P<q>["'])(?P<mp4>.+?)(?P=q)[^>]*>'''],
  48. webpage, name, group='mp4')
  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. proto = mobj.group('proto')
  55. mrss_url = '%s://xhamster.com/movies/%s/%s.html' % (proto, video_id, seo)
  56. webpage = self._download_webpage(mrss_url, video_id)
  57. title = self._html_search_regex(
  58. [r'<h1[^>]*>([^<]+)</h1>',
  59. r'<meta[^>]+itemprop=".*?caption.*?"[^>]+content="(.+?)"',
  60. r'<title[^>]*>(.+?)(?:,\s*[^,]*?\s*Porn\s*[^,]*?:\s*xHamster[^<]*| - xHamster\.com)</title>'],
  61. webpage, 'title')
  62. # Only a few videos have an description
  63. mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
  64. description = mobj.group(1) if mobj else None
  65. upload_date = unified_strdate(self._search_regex(
  66. r'hint=["\'](\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}',
  67. webpage, 'upload date', fatal=False))
  68. uploader = self._html_search_regex(
  69. r'<span[^>]+itemprop=["\']author[^>]+><a[^>]+href=["\'].+?xhamster\.com/user/[^>]+>(?P<uploader>.+?)</a>',
  70. webpage, 'uploader', default='anonymous')
  71. thumbnail = self._search_regex(
  72. [r'''thumb\s*:\s*(?P<q>["'])(?P<thumbnail>.+?)(?P=q)''',
  73. r'''<video[^>]+poster=(?P<q>["'])(?P<thumbnail>.+?)(?P=q)[^>]*>'''],
  74. webpage, 'thumbnail', fatal=False, group='thumbnail')
  75. duration = float_or_none(self._search_regex(
  76. r'(["\'])duration\1\s*:\s*(["\'])(?P<duration>.+?)\2',
  77. webpage, 'duration', fatal=False, group='duration'))
  78. view_count = int_or_none(self._search_regex(
  79. r'content=["\']User(?:View|Play)s:(\d+)',
  80. webpage, 'view count', fatal=False))
  81. mobj = re.search(r"hint='(?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes'", webpage)
  82. (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
  83. mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
  84. comment_count = mobj.group('commentcount') if mobj else 0
  85. age_limit = self._rta_search(webpage)
  86. hd = is_hd(webpage)
  87. format_id = 'hd' if hd else 'sd'
  88. video_url = extract_video_url(webpage, format_id)
  89. formats = [{
  90. 'url': video_url,
  91. 'format_id': 'hd' if hd else 'sd',
  92. 'preference': 1,
  93. }]
  94. if not hd:
  95. mrss_url = self._search_regex(r'<link rel="canonical" href="([^"]+)', webpage, 'mrss_url')
  96. webpage = self._download_webpage(mrss_url + '?hd', video_id, note='Downloading HD webpage')
  97. if is_hd(webpage):
  98. video_url = extract_video_url(webpage, 'hd')
  99. formats.append({
  100. 'url': video_url,
  101. 'format_id': 'hd',
  102. 'preference': 2,
  103. })
  104. self._sort_formats(formats)
  105. return {
  106. 'id': video_id,
  107. 'title': title,
  108. 'description': description,
  109. 'upload_date': upload_date,
  110. 'uploader': uploader,
  111. 'thumbnail': thumbnail,
  112. 'duration': duration,
  113. 'view_count': view_count,
  114. 'like_count': int_or_none(like_count),
  115. 'dislike_count': int_or_none(dislike_count),
  116. 'comment_count': int_or_none(comment_count),
  117. 'age_limit': age_limit,
  118. 'formats': formats,
  119. }
  120. class XHamsterEmbedIE(InfoExtractor):
  121. _VALID_URL = r'https?://(?:www\.)?xhamster\.com/xembed\.php\?video=(?P<id>\d+)'
  122. _TEST = {
  123. 'url': 'http://xhamster.com/xembed.php?video=3328539',
  124. 'info_dict': {
  125. 'id': '3328539',
  126. 'ext': 'mp4',
  127. 'title': 'Pen Masturbation',
  128. 'upload_date': '20140728',
  129. 'uploader_id': 'anonymous',
  130. 'duration': 5,
  131. 'age_limit': 18,
  132. }
  133. }
  134. @staticmethod
  135. def _extract_urls(webpage):
  136. return [url for _, url in re.findall(
  137. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?xhamster\.com/xembed\.php\?video=\d+)\1',
  138. webpage)]
  139. def _real_extract(self, url):
  140. video_id = self._match_id(url)
  141. webpage = self._download_webpage(url, video_id)
  142. video_url = self._search_regex(
  143. r'href="(https?://xhamster\.com/movies/%s/[^"]+\.html[^"]*)"' % video_id,
  144. webpage, 'xhamster url', default=None)
  145. if not video_url:
  146. vars = self._parse_json(
  147. self._search_regex(r'vars\s*:\s*({.+?})\s*,\s*\n', webpage, 'vars'),
  148. video_id)
  149. video_url = dict_get(vars, ('downloadLink', 'homepageLink', 'commentsLink', 'shareUrl'))
  150. return self.url_result(video_url, 'XHamster')