xhamster.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. determine_ext,
  8. dict_get,
  9. ExtractorError,
  10. int_or_none,
  11. parse_duration,
  12. try_get,
  13. unified_strdate,
  14. )
  15. class XHamsterIE(InfoExtractor):
  16. _VALID_URL = r'''(?x)
  17. https?://
  18. (?:.+?\.)?xhamster\.com/
  19. (?:
  20. movies/(?P<id>\d+)/(?P<display_id>[^/]*)\.html|
  21. videos/(?P<display_id_2>[^/]*)-(?P<id_2>\d+)
  22. )
  23. '''
  24. _TESTS = [{
  25. 'url': 'http://xhamster.com/movies/1509445/femaleagent_shy_beauty_takes_the_bait.html',
  26. 'md5': '8281348b8d3c53d39fffb377d24eac4e',
  27. 'info_dict': {
  28. 'id': '1509445',
  29. 'display_id': 'femaleagent_shy_beauty_takes_the_bait',
  30. 'ext': 'mp4',
  31. 'title': 'FemaleAgent Shy beauty takes the bait',
  32. 'timestamp': 1350194821,
  33. 'upload_date': '20121014',
  34. 'uploader': 'Ruseful2011',
  35. 'duration': 893,
  36. 'age_limit': 18,
  37. 'categories': ['Fake Hub', 'Amateur', 'MILFs', 'POV', 'Boss', 'Office', 'Oral', 'Reality', 'Sexy'],
  38. },
  39. }, {
  40. 'url': 'http://xhamster.com/movies/2221348/britney_spears_sexy_booty.html?hd',
  41. 'info_dict': {
  42. 'id': '2221348',
  43. 'display_id': 'britney_spears_sexy_booty',
  44. 'ext': 'mp4',
  45. 'title': 'Britney Spears Sexy Booty',
  46. 'timestamp': 1379123460,
  47. 'upload_date': '20130914',
  48. 'uploader': 'jojo747400',
  49. 'duration': 200,
  50. 'age_limit': 18,
  51. 'categories': ['Britney Spears', 'Celebrities', 'HD Videos', 'Sexy', 'Sexy Booty'],
  52. },
  53. 'params': {
  54. 'skip_download': True,
  55. },
  56. }, {
  57. # empty seo
  58. 'url': 'http://xhamster.com/movies/5667973/.html',
  59. 'info_dict': {
  60. 'id': '5667973',
  61. 'ext': 'mp4',
  62. 'title': '....',
  63. 'timestamp': 1454948101,
  64. 'upload_date': '20160208',
  65. 'uploader': 'parejafree',
  66. 'duration': 72,
  67. 'age_limit': 18,
  68. 'categories': ['Amateur', 'Blowjobs'],
  69. },
  70. 'params': {
  71. 'skip_download': True,
  72. },
  73. }, {
  74. 'url': 'https://xhamster.com/movies/2272726/amber_slayed_by_the_knight.html',
  75. 'only_matching': True,
  76. }, {
  77. # This video is visible for marcoalfa123456's friends only
  78. 'url': 'https://it.xhamster.com/movies/7263980/la_mia_vicina.html',
  79. 'only_matching': True,
  80. }, {
  81. # new URL schema
  82. 'url': 'https://pt.xhamster.com/videos/euro-pedal-pumping-7937821',
  83. 'only_matching': True,
  84. }]
  85. def _real_extract(self, url):
  86. mobj = re.match(self._VALID_URL, url)
  87. video_id = mobj.group('id') or mobj.group('id_2')
  88. display_id = mobj.group('display_id') or mobj.group('display_id_2')
  89. webpage = self._download_webpage(url, video_id)
  90. error = self._html_search_regex(
  91. r'<div[^>]+id=["\']videoClosed["\'][^>]*>(.+?)</div>',
  92. webpage, 'error', default=None)
  93. if error:
  94. raise ExtractorError(error, expected=True)
  95. age_limit = self._rta_search(webpage)
  96. def get_height(s):
  97. return int_or_none(self._search_regex(
  98. r'^(\d+)[pP]', s, 'height', default=None))
  99. initials = self._parse_json(
  100. self._search_regex(
  101. r'window\.initials\s*=\s*({.+?})\s*;\s*\n', webpage, 'initials',
  102. default='{}'),
  103. video_id, fatal=False)
  104. if initials:
  105. video = initials['videoModel']
  106. title = video['title']
  107. formats = []
  108. for format_id, formats_dict in video['sources'].items():
  109. if not isinstance(formats_dict, dict):
  110. continue
  111. for quality, format_item in formats_dict.items():
  112. if format_id == 'download':
  113. # Download link takes some time to be generated,
  114. # skipping for now
  115. continue
  116. if not isinstance(format_item, dict):
  117. continue
  118. format_url = format_item.get('link')
  119. filesize = int_or_none(
  120. format_item.get('size'), invscale=1000000)
  121. else:
  122. format_url = format_item
  123. filesize = None
  124. if not isinstance(format_url, compat_str):
  125. continue
  126. formats.append({
  127. 'format_id': '%s-%s' % (format_id, quality),
  128. 'url': format_url,
  129. 'ext': determine_ext(format_url, 'mp4'),
  130. 'height': get_height(quality),
  131. 'filesize': filesize,
  132. })
  133. self._sort_formats(formats)
  134. categories_list = video.get('categories')
  135. if isinstance(categories_list, list):
  136. categories = []
  137. for c in categories_list:
  138. if not isinstance(c, dict):
  139. continue
  140. c_name = c.get('name')
  141. if isinstance(c_name, compat_str):
  142. categories.append(c_name)
  143. else:
  144. categories = None
  145. return {
  146. 'id': video_id,
  147. 'display_id': display_id,
  148. 'title': title,
  149. 'description': video.get('description'),
  150. 'timestamp': int_or_none(video.get('created')),
  151. 'uploader': try_get(
  152. video, lambda x: x['author']['name'], compat_str),
  153. 'thumbnail': video.get('thumbURL'),
  154. 'duration': int_or_none(video.get('duration')),
  155. 'view_count': int_or_none(video.get('views')),
  156. 'like_count': int_or_none(try_get(
  157. video, lambda x: x['rating']['likes'], int)),
  158. 'dislike_count': int_or_none(try_get(
  159. video, lambda x: x['rating']['dislikes'], int)),
  160. 'comment_count': int_or_none(video.get('views')),
  161. 'age_limit': age_limit,
  162. 'categories': categories,
  163. 'formats': formats,
  164. }
  165. # Old layout fallback
  166. title = self._html_search_regex(
  167. [r'<h1[^>]*>([^<]+)</h1>',
  168. r'<meta[^>]+itemprop=".*?caption.*?"[^>]+content="(.+?)"',
  169. r'<title[^>]*>(.+?)(?:,\s*[^,]*?\s*Porn\s*[^,]*?:\s*xHamster[^<]*| - xHamster\.com)</title>'],
  170. webpage, 'title')
  171. formats = []
  172. format_urls = set()
  173. sources = self._parse_json(
  174. self._search_regex(
  175. r'sources\s*:\s*({.+?})\s*,?\s*\n', webpage, 'sources',
  176. default='{}'),
  177. video_id, fatal=False)
  178. for format_id, format_url in sources.items():
  179. if not isinstance(format_url, compat_str):
  180. continue
  181. if format_url in format_urls:
  182. continue
  183. format_urls.add(format_url)
  184. formats.append({
  185. 'format_id': format_id,
  186. 'url': format_url,
  187. 'height': get_height(format_id),
  188. })
  189. video_url = self._search_regex(
  190. [r'''file\s*:\s*(?P<q>["'])(?P<mp4>.+?)(?P=q)''',
  191. r'''<a\s+href=(?P<q>["'])(?P<mp4>.+?)(?P=q)\s+class=["']mp4Thumb''',
  192. r'''<video[^>]+file=(?P<q>["'])(?P<mp4>.+?)(?P=q)[^>]*>'''],
  193. webpage, 'video url', group='mp4', default=None)
  194. if video_url and video_url not in format_urls:
  195. formats.append({
  196. 'url': video_url,
  197. })
  198. self._sort_formats(formats)
  199. # Only a few videos have an description
  200. mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
  201. description = mobj.group(1) if mobj else None
  202. upload_date = unified_strdate(self._search_regex(
  203. r'hint=["\'](\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}',
  204. webpage, 'upload date', fatal=False))
  205. uploader = self._html_search_regex(
  206. r'<span[^>]+itemprop=["\']author[^>]+><a[^>]+><span[^>]+>([^<]+)',
  207. webpage, 'uploader', default='anonymous')
  208. thumbnail = self._search_regex(
  209. [r'''thumb\s*:\s*(?P<q>["'])(?P<thumbnail>.+?)(?P=q)''',
  210. r'''<video[^>]+poster=(?P<q>["'])(?P<thumbnail>.+?)(?P=q)[^>]*>'''],
  211. webpage, 'thumbnail', fatal=False, group='thumbnail')
  212. duration = parse_duration(self._search_regex(
  213. [r'<[^<]+\bitemprop=["\']duration["\'][^<]+\bcontent=["\'](.+?)["\']',
  214. r'Runtime:\s*</span>\s*([\d:]+)'], webpage,
  215. 'duration', fatal=False))
  216. view_count = int_or_none(self._search_regex(
  217. r'content=["\']User(?:View|Play)s:(\d+)',
  218. webpage, 'view count', fatal=False))
  219. mobj = re.search(r'hint=[\'"](?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes', webpage)
  220. (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
  221. mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
  222. comment_count = mobj.group('commentcount') if mobj else 0
  223. categories_html = self._search_regex(
  224. r'(?s)<table.+?(<span>Categories:.+?)</table>', webpage,
  225. 'categories', default=None)
  226. categories = [clean_html(category) for category in re.findall(
  227. r'<a[^>]+>(.+?)</a>', categories_html)] if categories_html else None
  228. return {
  229. 'id': video_id,
  230. 'display_id': display_id,
  231. 'title': title,
  232. 'description': description,
  233. 'upload_date': upload_date,
  234. 'uploader': uploader,
  235. 'thumbnail': thumbnail,
  236. 'duration': duration,
  237. 'view_count': view_count,
  238. 'like_count': int_or_none(like_count),
  239. 'dislike_count': int_or_none(dislike_count),
  240. 'comment_count': int_or_none(comment_count),
  241. 'age_limit': age_limit,
  242. 'categories': categories,
  243. 'formats': formats,
  244. }
  245. class XHamsterEmbedIE(InfoExtractor):
  246. _VALID_URL = r'https?://(?:www\.)?xhamster\.com/xembed\.php\?video=(?P<id>\d+)'
  247. _TEST = {
  248. 'url': 'http://xhamster.com/xembed.php?video=3328539',
  249. 'info_dict': {
  250. 'id': '3328539',
  251. 'ext': 'mp4',
  252. 'title': 'Pen Masturbation',
  253. 'upload_date': '20140728',
  254. 'uploader_id': 'anonymous',
  255. 'duration': 5,
  256. 'age_limit': 18,
  257. }
  258. }
  259. @staticmethod
  260. def _extract_urls(webpage):
  261. return [url for _, url in re.findall(
  262. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?xhamster\.com/xembed\.php\?video=\d+)\1',
  263. webpage)]
  264. def _real_extract(self, url):
  265. video_id = self._match_id(url)
  266. webpage = self._download_webpage(url, video_id)
  267. video_url = self._search_regex(
  268. r'href="(https?://xhamster\.com/(?:movies/{0}/[^"]*\.html|videos/[^/]*-{0})[^"]*)"'.format(video_id),
  269. webpage, 'xhamster url', default=None)
  270. if not video_url:
  271. vars = self._parse_json(
  272. self._search_regex(r'vars\s*:\s*({.+?})\s*,\s*\n', webpage, 'vars'),
  273. video_id)
  274. video_url = dict_get(vars, ('downloadLink', 'homepageLink', 'commentsLink', 'shareUrl'))
  275. return self.url_result(video_url, 'XHamster')