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.

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