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.

197 lines
6.9 KiB

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