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.

233 lines
8.5 KiB

10 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import compat_str
  5. from ..utils import (
  6. clean_html,
  7. dict_get,
  8. ExtractorError,
  9. int_or_none,
  10. parse_duration,
  11. unified_strdate,
  12. )
  13. class XHamsterIE(InfoExtractor):
  14. _VALID_URL = r'''(?x)
  15. https?://
  16. (?:.+?\.)?xhamster\.com/
  17. (?:
  18. movies/(?P<id>\d+)/(?P<display_id>[^/]*)\.html|
  19. videos/(?P<display_id_2>[^/]*)-(?P<id_2>\d+)
  20. )
  21. '''
  22. _TESTS = [{
  23. 'url': 'http://xhamster.com/movies/1509445/femaleagent_shy_beauty_takes_the_bait.html',
  24. 'md5': '8281348b8d3c53d39fffb377d24eac4e',
  25. 'info_dict': {
  26. 'id': '1509445',
  27. 'display_id': 'femaleagent_shy_beauty_takes_the_bait',
  28. 'ext': 'mp4',
  29. 'title': 'FemaleAgent Shy beauty takes the bait',
  30. 'upload_date': '20121014',
  31. 'uploader': 'Ruseful2011',
  32. 'duration': 893,
  33. 'age_limit': 18,
  34. 'categories': ['Fake Hub', 'Amateur', 'MILFs', 'POV', 'Boss', 'Office', 'Oral', 'Reality', 'Sexy'],
  35. },
  36. }, {
  37. 'url': 'http://xhamster.com/movies/2221348/britney_spears_sexy_booty.html?hd',
  38. 'info_dict': {
  39. 'id': '2221348',
  40. 'display_id': 'britney_spears_sexy_booty',
  41. 'ext': 'mp4',
  42. 'title': 'Britney Spears Sexy Booty',
  43. 'upload_date': '20130914',
  44. 'uploader': 'jojo747400',
  45. 'duration': 200,
  46. 'age_limit': 18,
  47. 'categories': ['Britney Spears', 'Celebrities', 'HD Videos', 'Sexy', 'Sexy Booty'],
  48. },
  49. 'params': {
  50. 'skip_download': True,
  51. },
  52. }, {
  53. # empty seo
  54. 'url': 'http://xhamster.com/movies/5667973/.html',
  55. 'info_dict': {
  56. 'id': '5667973',
  57. 'ext': 'mp4',
  58. 'title': '....',
  59. 'upload_date': '20160208',
  60. 'uploader': 'parejafree',
  61. 'duration': 72,
  62. 'age_limit': 18,
  63. 'categories': ['Amateur', 'Blowjobs'],
  64. },
  65. 'params': {
  66. 'skip_download': True,
  67. },
  68. }, {
  69. 'url': 'https://xhamster.com/movies/2272726/amber_slayed_by_the_knight.html',
  70. 'only_matching': True,
  71. }, {
  72. # This video is visible for marcoalfa123456's friends only
  73. 'url': 'https://it.xhamster.com/movies/7263980/la_mia_vicina.html',
  74. 'only_matching': True,
  75. }, {
  76. # new URL schema
  77. 'url': 'https://pt.xhamster.com/videos/euro-pedal-pumping-7937821',
  78. 'only_matching': True,
  79. }]
  80. def _real_extract(self, url):
  81. mobj = re.match(self._VALID_URL, url)
  82. video_id = mobj.group('id') or mobj.group('id_2')
  83. display_id = mobj.group('display_id') or mobj.group('display_id_2')
  84. webpage = self._download_webpage(url, video_id)
  85. error = self._html_search_regex(
  86. r'<div[^>]+id=["\']videoClosed["\'][^>]*>(.+?)</div>',
  87. webpage, 'error', default=None)
  88. if error:
  89. raise ExtractorError(error, expected=True)
  90. title = self._html_search_regex(
  91. [r'<h1[^>]*>([^<]+)</h1>',
  92. r'<meta[^>]+itemprop=".*?caption.*?"[^>]+content="(.+?)"',
  93. r'<title[^>]*>(.+?)(?:,\s*[^,]*?\s*Porn\s*[^,]*?:\s*xHamster[^<]*| - xHamster\.com)</title>'],
  94. webpage, 'title')
  95. formats = []
  96. format_urls = set()
  97. sources = self._parse_json(
  98. self._search_regex(
  99. r'sources\s*:\s*({.+?})\s*,?\s*\n', webpage, 'sources',
  100. default='{}'),
  101. video_id, fatal=False)
  102. for format_id, format_url in sources.items():
  103. if not isinstance(format_url, compat_str):
  104. continue
  105. if format_url in format_urls:
  106. continue
  107. format_urls.add(format_url)
  108. formats.append({
  109. 'format_id': format_id,
  110. 'url': format_url,
  111. 'height': int_or_none(self._search_regex(
  112. r'^(\d+)[pP]', format_id, 'height', default=None))
  113. })
  114. video_url = self._search_regex(
  115. [r'''file\s*:\s*(?P<q>["'])(?P<mp4>.+?)(?P=q)''',
  116. r'''<a\s+href=(?P<q>["'])(?P<mp4>.+?)(?P=q)\s+class=["']mp4Thumb''',
  117. r'''<video[^>]+file=(?P<q>["'])(?P<mp4>.+?)(?P=q)[^>]*>'''],
  118. webpage, 'video url', group='mp4', default=None)
  119. if video_url and video_url not in format_urls:
  120. formats.append({
  121. 'url': video_url,
  122. })
  123. self._sort_formats(formats)
  124. # Only a few videos have an description
  125. mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
  126. description = mobj.group(1) if mobj else None
  127. upload_date = unified_strdate(self._search_regex(
  128. r'hint=["\'](\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}',
  129. webpage, 'upload date', fatal=False))
  130. uploader = self._html_search_regex(
  131. r'<span[^>]+itemprop=["\']author[^>]+><a[^>]+><span[^>]+>([^<]+)',
  132. webpage, 'uploader', default='anonymous')
  133. thumbnail = self._search_regex(
  134. [r'''thumb\s*:\s*(?P<q>["'])(?P<thumbnail>.+?)(?P=q)''',
  135. r'''<video[^>]+poster=(?P<q>["'])(?P<thumbnail>.+?)(?P=q)[^>]*>'''],
  136. webpage, 'thumbnail', fatal=False, group='thumbnail')
  137. duration = parse_duration(self._search_regex(
  138. [r'<[^<]+\bitemprop=["\']duration["\'][^<]+\bcontent=["\'](.+?)["\']',
  139. r'Runtime:\s*</span>\s*([\d:]+)'], webpage,
  140. 'duration', fatal=False))
  141. view_count = int_or_none(self._search_regex(
  142. r'content=["\']User(?:View|Play)s:(\d+)',
  143. webpage, 'view count', fatal=False))
  144. mobj = re.search(r'hint=[\'"](?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes', webpage)
  145. (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
  146. mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
  147. comment_count = mobj.group('commentcount') if mobj else 0
  148. age_limit = self._rta_search(webpage)
  149. categories_html = self._search_regex(
  150. r'(?s)<table.+?(<span>Categories:.+?)</table>', webpage,
  151. 'categories', default=None)
  152. categories = [clean_html(category) for category in re.findall(
  153. r'<a[^>]+>(.+?)</a>', categories_html)] if categories_html else None
  154. return {
  155. 'id': video_id,
  156. 'display_id': display_id,
  157. 'title': title,
  158. 'description': description,
  159. 'upload_date': upload_date,
  160. 'uploader': uploader,
  161. 'thumbnail': thumbnail,
  162. 'duration': duration,
  163. 'view_count': view_count,
  164. 'like_count': int_or_none(like_count),
  165. 'dislike_count': int_or_none(dislike_count),
  166. 'comment_count': int_or_none(comment_count),
  167. 'age_limit': age_limit,
  168. 'categories': categories,
  169. 'formats': formats,
  170. }
  171. class XHamsterEmbedIE(InfoExtractor):
  172. _VALID_URL = r'https?://(?:www\.)?xhamster\.com/xembed\.php\?video=(?P<id>\d+)'
  173. _TEST = {
  174. 'url': 'http://xhamster.com/xembed.php?video=3328539',
  175. 'info_dict': {
  176. 'id': '3328539',
  177. 'ext': 'mp4',
  178. 'title': 'Pen Masturbation',
  179. 'upload_date': '20140728',
  180. 'uploader_id': 'anonymous',
  181. 'duration': 5,
  182. 'age_limit': 18,
  183. }
  184. }
  185. @staticmethod
  186. def _extract_urls(webpage):
  187. return [url for _, url in re.findall(
  188. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?xhamster\.com/xembed\.php\?video=\d+)\1',
  189. webpage)]
  190. def _real_extract(self, url):
  191. video_id = self._match_id(url)
  192. webpage = self._download_webpage(url, video_id)
  193. video_url = self._search_regex(
  194. r'href="(https?://xhamster\.com/(?:movies/{0}/[^"]*\.html|videos/[^/]*-{0})[^"]*)"'.format(video_id),
  195. webpage, 'xhamster url', default=None)
  196. if not video_url:
  197. vars = self._parse_json(
  198. self._search_regex(r'vars\s*:\s*({.+?})\s*,\s*\n', webpage, 'vars'),
  199. video_id)
  200. video_url = dict_get(vars, ('downloadLink', 'homepageLink', 'commentsLink', 'shareUrl'))
  201. return self.url_result(video_url, 'XHamster')