xhamster.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import itertools
  4. import re
  5. from .common import InfoExtractor
  6. from ..compat import compat_str
  7. from ..utils import (
  8. clean_html,
  9. determine_ext,
  10. dict_get,
  11. extract_attributes,
  12. ExtractorError,
  13. float_or_none,
  14. int_or_none,
  15. parse_duration,
  16. str_or_none,
  17. try_get,
  18. unified_strdate,
  19. url_or_none,
  20. urljoin,
  21. )
  22. class XHamsterIE(InfoExtractor):
  23. _DOMAINS = r'(?:xhamster\.(?:com|one|desi)|xhms\.pro|xhamster\d+\.com|xhday\.com)'
  24. _VALID_URL = r'''(?x)
  25. https?://
  26. (?:.+?\.)?%s/
  27. (?:
  28. movies/(?P<id>[\dA-Za-z]+)/(?P<display_id>[^/]*)\.html|
  29. videos/(?P<display_id_2>[^/]*)-(?P<id_2>[\dA-Za-z]+)
  30. )
  31. ''' % _DOMAINS
  32. _TESTS = [{
  33. 'url': 'https://xhamster.com/videos/femaleagent-shy-beauty-takes-the-bait-1509445',
  34. 'md5': '34e1ab926db5dc2750fed9e1f34304bb',
  35. 'info_dict': {
  36. 'id': '1509445',
  37. 'display_id': 'femaleagent-shy-beauty-takes-the-bait',
  38. 'ext': 'mp4',
  39. 'title': 'FemaleAgent Shy beauty takes the bait',
  40. 'timestamp': 1350194821,
  41. 'upload_date': '20121014',
  42. 'uploader': 'Ruseful2011',
  43. 'uploader_id': 'ruseful2011',
  44. 'duration': 893,
  45. 'age_limit': 18,
  46. },
  47. }, {
  48. 'url': 'https://xhamster.com/videos/britney-spears-sexy-booty-2221348?hd=',
  49. 'info_dict': {
  50. 'id': '2221348',
  51. 'display_id': 'britney-spears-sexy-booty',
  52. 'ext': 'mp4',
  53. 'title': 'Britney Spears Sexy Booty',
  54. 'timestamp': 1379123460,
  55. 'upload_date': '20130914',
  56. 'uploader': 'jojo747400',
  57. 'duration': 200,
  58. 'age_limit': 18,
  59. },
  60. 'params': {
  61. 'skip_download': True,
  62. },
  63. }, {
  64. # empty seo, unavailable via new URL schema
  65. 'url': 'http://xhamster.com/movies/5667973/.html',
  66. 'info_dict': {
  67. 'id': '5667973',
  68. 'ext': 'mp4',
  69. 'title': '....',
  70. 'timestamp': 1454948101,
  71. 'upload_date': '20160208',
  72. 'uploader': 'parejafree',
  73. 'uploader_id': 'parejafree',
  74. 'duration': 72,
  75. 'age_limit': 18,
  76. },
  77. 'params': {
  78. 'skip_download': True,
  79. },
  80. }, {
  81. # mobile site
  82. 'url': 'https://m.xhamster.com/videos/cute-teen-jacqueline-solo-masturbation-8559111',
  83. 'only_matching': True,
  84. }, {
  85. 'url': 'https://xhamster.com/movies/2272726/amber_slayed_by_the_knight.html',
  86. 'only_matching': True,
  87. }, {
  88. # This video is visible for marcoalfa123456's friends only
  89. 'url': 'https://it.xhamster.com/movies/7263980/la_mia_vicina.html',
  90. 'only_matching': True,
  91. }, {
  92. # new URL schema
  93. 'url': 'https://pt.xhamster.com/videos/euro-pedal-pumping-7937821',
  94. 'only_matching': True,
  95. }, {
  96. 'url': 'https://xhamster.one/videos/femaleagent-shy-beauty-takes-the-bait-1509445',
  97. 'only_matching': True,
  98. }, {
  99. 'url': 'https://xhamster.desi/videos/femaleagent-shy-beauty-takes-the-bait-1509445',
  100. 'only_matching': True,
  101. }, {
  102. 'url': 'https://xhamster2.com/videos/femaleagent-shy-beauty-takes-the-bait-1509445',
  103. 'only_matching': True,
  104. }, {
  105. 'url': 'https://xhamster11.com/videos/femaleagent-shy-beauty-takes-the-bait-1509445',
  106. 'only_matching': True,
  107. }, {
  108. 'url': 'https://xhamster26.com/videos/femaleagent-shy-beauty-takes-the-bait-1509445',
  109. 'only_matching': True,
  110. }, {
  111. 'url': 'http://xhamster.com/movies/1509445/femaleagent_shy_beauty_takes_the_bait.html',
  112. 'only_matching': True,
  113. }, {
  114. 'url': 'http://xhamster.com/movies/2221348/britney_spears_sexy_booty.html?hd',
  115. 'only_matching': True,
  116. }, {
  117. 'url': 'http://de.xhamster.com/videos/skinny-girl-fucks-herself-hard-in-the-forest-xhnBJZx',
  118. 'only_matching': True,
  119. }, {
  120. 'url': 'https://xhday.com/videos/strapless-threesome-xhh7yVf',
  121. 'only_matching': True,
  122. }]
  123. def _real_extract(self, url):
  124. mobj = re.match(self._VALID_URL, url)
  125. video_id = mobj.group('id') or mobj.group('id_2')
  126. display_id = mobj.group('display_id') or mobj.group('display_id_2')
  127. desktop_url = re.sub(r'^(https?://(?:.+?\.)?)m\.', r'\1', url)
  128. webpage, urlh = self._download_webpage_handle(desktop_url, video_id)
  129. error = self._html_search_regex(
  130. r'<div[^>]+id=["\']videoClosed["\'][^>]*>(.+?)</div>',
  131. webpage, 'error', default=None)
  132. if error:
  133. raise ExtractorError(error, expected=True)
  134. age_limit = self._rta_search(webpage)
  135. def get_height(s):
  136. return int_or_none(self._search_regex(
  137. r'^(\d+)[pP]', s, 'height', default=None))
  138. initials = self._parse_json(
  139. self._search_regex(
  140. (r'window\.initials\s*=\s*({.+?})\s*;\s*</script>',
  141. r'window\.initials\s*=\s*({.+?})\s*;'), webpage, 'initials',
  142. default='{}'),
  143. video_id, fatal=False)
  144. if initials:
  145. video = initials['videoModel']
  146. title = video['title']
  147. formats = []
  148. format_urls = set()
  149. format_sizes = {}
  150. sources = try_get(video, lambda x: x['sources'], dict) or {}
  151. for format_id, formats_dict in sources.items():
  152. if not isinstance(formats_dict, dict):
  153. continue
  154. download_sources = try_get(sources, lambda x: x['download'], dict) or {}
  155. for quality, format_dict in download_sources.items():
  156. if not isinstance(format_dict, dict):
  157. continue
  158. format_sizes[quality] = float_or_none(format_dict.get('size'))
  159. for quality, format_item in formats_dict.items():
  160. if format_id == 'download':
  161. # Download link takes some time to be generated,
  162. # skipping for now
  163. continue
  164. format_url = format_item
  165. format_url = url_or_none(format_url)
  166. if not format_url or format_url in format_urls:
  167. continue
  168. format_urls.add(format_url)
  169. formats.append({
  170. 'format_id': '%s-%s' % (format_id, quality),
  171. 'url': format_url,
  172. 'ext': determine_ext(format_url, 'mp4'),
  173. 'height': get_height(quality),
  174. 'filesize': format_sizes.get(quality),
  175. 'http_headers': {
  176. 'Referer': urlh.geturl(),
  177. },
  178. })
  179. xplayer_sources = try_get(
  180. initials, lambda x: x['xplayerSettings']['sources'], dict)
  181. if xplayer_sources:
  182. hls_sources = xplayer_sources.get('hls')
  183. if isinstance(hls_sources, dict):
  184. for hls_format_key in ('url', 'fallback'):
  185. hls_url = hls_sources.get(hls_format_key)
  186. if not hls_url:
  187. continue
  188. hls_url = urljoin(url, hls_url)
  189. if not hls_url or hls_url in format_urls:
  190. continue
  191. format_urls.add(hls_url)
  192. formats.extend(self._extract_m3u8_formats(
  193. hls_url, video_id, 'mp4', entry_protocol='m3u8_native',
  194. m3u8_id='hls', fatal=False))
  195. standard_sources = xplayer_sources.get('standard')
  196. if isinstance(standard_sources, dict):
  197. for format_id, formats_list in standard_sources.items():
  198. if not isinstance(formats_list, list):
  199. continue
  200. for standard_format in formats_list:
  201. if not isinstance(standard_format, dict):
  202. continue
  203. for standard_format_key in ('url', 'fallback'):
  204. standard_url = standard_format.get(standard_format_key)
  205. if not standard_url:
  206. continue
  207. standard_url = urljoin(url, standard_url)
  208. if not standard_url or standard_url in format_urls:
  209. continue
  210. format_urls.add(standard_url)
  211. ext = determine_ext(standard_url, 'mp4')
  212. if ext == 'm3u8':
  213. formats.extend(self._extract_m3u8_formats(
  214. standard_url, video_id, 'mp4', entry_protocol='m3u8_native',
  215. m3u8_id='hls', fatal=False))
  216. continue
  217. quality = (str_or_none(standard_format.get('quality'))
  218. or str_or_none(standard_format.get('label'))
  219. or '')
  220. formats.append({
  221. 'format_id': '%s-%s' % (format_id, quality),
  222. 'url': standard_url,
  223. 'ext': ext,
  224. 'height': get_height(quality),
  225. 'filesize': format_sizes.get(quality),
  226. 'http_headers': {
  227. 'Referer': standard_url,
  228. },
  229. })
  230. self._sort_formats(formats, field_preference=('height', 'width', 'tbr', 'format_id'))
  231. categories_list = video.get('categories')
  232. if isinstance(categories_list, list):
  233. categories = []
  234. for c in categories_list:
  235. if not isinstance(c, dict):
  236. continue
  237. c_name = c.get('name')
  238. if isinstance(c_name, compat_str):
  239. categories.append(c_name)
  240. else:
  241. categories = None
  242. uploader_url = url_or_none(try_get(video, lambda x: x['author']['pageURL']))
  243. return {
  244. 'id': video_id,
  245. 'display_id': display_id,
  246. 'title': title,
  247. 'description': video.get('description'),
  248. 'timestamp': int_or_none(video.get('created')),
  249. 'uploader': try_get(
  250. video, lambda x: x['author']['name'], compat_str),
  251. 'uploader_url': uploader_url,
  252. 'uploader_id': uploader_url.split('/')[-1] if uploader_url else None,
  253. 'thumbnail': video.get('thumbURL'),
  254. 'duration': int_or_none(video.get('duration')),
  255. 'view_count': int_or_none(video.get('views')),
  256. 'like_count': int_or_none(try_get(
  257. video, lambda x: x['rating']['likes'], int)),
  258. 'dislike_count': int_or_none(try_get(
  259. video, lambda x: x['rating']['dislikes'], int)),
  260. 'comment_count': int_or_none(video.get('views')),
  261. 'age_limit': age_limit if age_limit is not None else 18,
  262. 'categories': categories,
  263. 'formats': formats,
  264. }
  265. # Old layout fallback
  266. title = self._html_search_regex(
  267. [r'<h1[^>]*>([^<]+)</h1>',
  268. r'<meta[^>]+itemprop=".*?caption.*?"[^>]+content="(.+?)"',
  269. r'<title[^>]*>(.+?)(?:,\s*[^,]*?\s*Porn\s*[^,]*?:\s*xHamster[^<]*| - xHamster\.com)</title>'],
  270. webpage, 'title')
  271. formats = []
  272. format_urls = set()
  273. sources = self._parse_json(
  274. self._search_regex(
  275. r'sources\s*:\s*({.+?})\s*,?\s*\n', webpage, 'sources',
  276. default='{}'),
  277. video_id, fatal=False)
  278. for format_id, format_url in sources.items():
  279. format_url = url_or_none(format_url)
  280. if not format_url:
  281. continue
  282. if format_url in format_urls:
  283. continue
  284. format_urls.add(format_url)
  285. formats.append({
  286. 'format_id': format_id,
  287. 'url': format_url,
  288. 'height': get_height(format_id),
  289. })
  290. video_url = self._search_regex(
  291. [r'''file\s*:\s*(?P<q>["'])(?P<mp4>.+?)(?P=q)''',
  292. r'''<a\s+href=(?P<q>["'])(?P<mp4>.+?)(?P=q)\s+class=["']mp4Thumb''',
  293. r'''<video[^>]+file=(?P<q>["'])(?P<mp4>.+?)(?P=q)[^>]*>'''],
  294. webpage, 'video url', group='mp4', default=None)
  295. if video_url and video_url not in format_urls:
  296. formats.append({
  297. 'url': video_url,
  298. })
  299. self._sort_formats(formats)
  300. # Only a few videos have an description
  301. mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
  302. description = mobj.group(1) if mobj else None
  303. upload_date = unified_strdate(self._search_regex(
  304. r'hint=["\'](\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}',
  305. webpage, 'upload date', fatal=False))
  306. uploader = self._html_search_regex(
  307. r'<span[^>]+itemprop=["\']author[^>]+><a[^>]+><span[^>]+>([^<]+)',
  308. webpage, 'uploader', default='anonymous')
  309. thumbnail = self._search_regex(
  310. [r'''["']thumbUrl["']\s*:\s*(?P<q>["'])(?P<thumbnail>.+?)(?P=q)''',
  311. r'''<video[^>]+"poster"=(?P<q>["'])(?P<thumbnail>.+?)(?P=q)[^>]*>'''],
  312. webpage, 'thumbnail', fatal=False, group='thumbnail')
  313. duration = parse_duration(self._search_regex(
  314. [r'<[^<]+\bitemprop=["\']duration["\'][^<]+\bcontent=["\'](.+?)["\']',
  315. r'Runtime:\s*</span>\s*([\d:]+)'], webpage,
  316. 'duration', fatal=False))
  317. view_count = int_or_none(self._search_regex(
  318. r'content=["\']User(?:View|Play)s:(\d+)',
  319. webpage, 'view count', fatal=False))
  320. mobj = re.search(r'hint=[\'"](?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes', webpage)
  321. (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
  322. mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
  323. comment_count = mobj.group('commentcount') if mobj else 0
  324. categories_html = self._search_regex(
  325. r'(?s)<table.+?(<span>Categories:.+?)</table>', webpage,
  326. 'categories', default=None)
  327. categories = [clean_html(category) for category in re.findall(
  328. r'<a[^>]+>(.+?)</a>', categories_html)] if categories_html else None
  329. return {
  330. 'id': video_id,
  331. 'display_id': display_id,
  332. 'title': title,
  333. 'description': description,
  334. 'upload_date': upload_date,
  335. 'uploader': uploader,
  336. 'uploader_id': uploader.lower() if uploader else None,
  337. 'thumbnail': thumbnail,
  338. 'duration': duration,
  339. 'view_count': view_count,
  340. 'like_count': int_or_none(like_count),
  341. 'dislike_count': int_or_none(dislike_count),
  342. 'comment_count': int_or_none(comment_count),
  343. 'age_limit': age_limit,
  344. 'categories': categories,
  345. 'formats': formats,
  346. }
  347. class XHamsterEmbedIE(InfoExtractor):
  348. _VALID_URL = r'https?://(?:.+?\.)?%s/xembed\.php\?video=(?P<id>\d+)' % XHamsterIE._DOMAINS
  349. _TEST = {
  350. 'url': 'http://xhamster.com/xembed.php?video=3328539',
  351. 'info_dict': {
  352. 'id': '3328539',
  353. 'ext': 'mp4',
  354. 'title': 'Pen Masturbation',
  355. 'timestamp': 1406581861,
  356. 'upload_date': '20140728',
  357. 'uploader': 'ManyakisArt',
  358. 'duration': 5,
  359. 'age_limit': 18,
  360. }
  361. }
  362. @staticmethod
  363. def _extract_urls(webpage):
  364. return [url for _, url in re.findall(
  365. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?xhamster\.com/xembed\.php\?video=\d+)\1',
  366. webpage)]
  367. def _real_extract(self, url):
  368. video_id = self._match_id(url)
  369. webpage = self._download_webpage(url, video_id)
  370. video_url = self._search_regex(
  371. r'href="(https?://xhamster\.com/(?:movies/{0}/[^"]*\.html|videos/[^/]*-{0})[^"]*)"'.format(video_id),
  372. webpage, 'xhamster url', default=None)
  373. if not video_url:
  374. vars = self._parse_json(
  375. self._search_regex(r'vars\s*:\s*({.+?})\s*,\s*\n', webpage, 'vars'),
  376. video_id)
  377. video_url = dict_get(vars, ('downloadLink', 'homepageLink', 'commentsLink', 'shareUrl'))
  378. return self.url_result(video_url, 'XHamster')
  379. class XHamsterUserIE(InfoExtractor):
  380. _VALID_URL = r'https?://(?:.+?\.)?%s/users/(?P<id>[^/?#&]+)' % XHamsterIE._DOMAINS
  381. _TESTS = [{
  382. # Paginated user profile
  383. 'url': 'https://xhamster.com/users/netvideogirls/videos',
  384. 'info_dict': {
  385. 'id': 'netvideogirls',
  386. },
  387. 'playlist_mincount': 267,
  388. }, {
  389. # Non-paginated user profile
  390. 'url': 'https://xhamster.com/users/firatkaan/videos',
  391. 'info_dict': {
  392. 'id': 'firatkaan',
  393. },
  394. 'playlist_mincount': 1,
  395. }, {
  396. 'url': 'https://xhday.com/users/mobhunter',
  397. 'only_matching': True,
  398. }]
  399. def _entries(self, user_id):
  400. next_page_url = 'https://xhamster.com/users/%s/videos/1' % user_id
  401. for pagenum in itertools.count(1):
  402. page = self._download_webpage(
  403. next_page_url, user_id, 'Downloading page %s' % pagenum)
  404. for video_tag in re.findall(
  405. r'(<a[^>]+class=["\'].*?\bvideo-thumb__image-container[^>]+>)',
  406. page):
  407. video = extract_attributes(video_tag)
  408. video_url = url_or_none(video.get('href'))
  409. if not video_url or not XHamsterIE.suitable(video_url):
  410. continue
  411. video_id = XHamsterIE._match_id(video_url)
  412. yield self.url_result(
  413. video_url, ie=XHamsterIE.ie_key(), video_id=video_id)
  414. mobj = re.search(r'<a[^>]+data-page=["\']next[^>]+>', page)
  415. if not mobj:
  416. break
  417. next_page = extract_attributes(mobj.group(0))
  418. next_page_url = url_or_none(next_page.get('href'))
  419. if not next_page_url:
  420. break
  421. def _real_extract(self, url):
  422. user_id = self._match_id(url)
  423. return self.playlist_result(self._entries(user_id), user_id)