logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

xhamster.py (19539B)


  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|xhvid\.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. 'url': 'https://xhvid.com/videos/lk-mm-xhc6wn6',
  124. 'only_matching': True,
  125. }]
  126. def _real_extract(self, url):
  127. mobj = re.match(self._VALID_URL, url)
  128. video_id = mobj.group('id') or mobj.group('id_2')
  129. display_id = mobj.group('display_id') or mobj.group('display_id_2')
  130. desktop_url = re.sub(r'^(https?://(?:.+?\.)?)m\.', r'\1', url)
  131. webpage, urlh = self._download_webpage_handle(desktop_url, video_id)
  132. error = self._html_search_regex(
  133. r'<div[^>]+id=["\']videoClosed["\'][^>]*>(.+?)</div>',
  134. webpage, 'error', default=None)
  135. if error:
  136. raise ExtractorError(error, expected=True)
  137. age_limit = self._rta_search(webpage)
  138. def get_height(s):
  139. return int_or_none(self._search_regex(
  140. r'^(\d+)[pP]', s, 'height', default=None))
  141. initials = self._parse_json(
  142. self._search_regex(
  143. (r'window\.initials\s*=\s*({.+?})\s*;\s*</script>',
  144. r'window\.initials\s*=\s*({.+?})\s*;'), webpage, 'initials',
  145. default='{}'),
  146. video_id, fatal=False)
  147. if initials:
  148. video = initials['videoModel']
  149. title = video['title']
  150. formats = []
  151. format_urls = set()
  152. format_sizes = {}
  153. sources = try_get(video, lambda x: x['sources'], dict) or {}
  154. for format_id, formats_dict in sources.items():
  155. if not isinstance(formats_dict, dict):
  156. continue
  157. download_sources = try_get(sources, lambda x: x['download'], dict) or {}
  158. for quality, format_dict in download_sources.items():
  159. if not isinstance(format_dict, dict):
  160. continue
  161. format_sizes[quality] = float_or_none(format_dict.get('size'))
  162. for quality, format_item in formats_dict.items():
  163. if format_id == 'download':
  164. # Download link takes some time to be generated,
  165. # skipping for now
  166. continue
  167. format_url = format_item
  168. format_url = url_or_none(format_url)
  169. if not format_url or format_url in format_urls:
  170. continue
  171. format_urls.add(format_url)
  172. formats.append({
  173. 'format_id': '%s-%s' % (format_id, quality),
  174. 'url': format_url,
  175. 'ext': determine_ext(format_url, 'mp4'),
  176. 'height': get_height(quality),
  177. 'filesize': format_sizes.get(quality),
  178. 'http_headers': {
  179. 'Referer': urlh.geturl(),
  180. },
  181. })
  182. xplayer_sources = try_get(
  183. initials, lambda x: x['xplayerSettings']['sources'], dict)
  184. if xplayer_sources:
  185. hls_sources = xplayer_sources.get('hls')
  186. if isinstance(hls_sources, dict):
  187. for hls_format_key in ('url', 'fallback'):
  188. hls_url = hls_sources.get(hls_format_key)
  189. if not hls_url:
  190. continue
  191. hls_url = urljoin(url, hls_url)
  192. if not hls_url or hls_url in format_urls:
  193. continue
  194. format_urls.add(hls_url)
  195. formats.extend(self._extract_m3u8_formats(
  196. hls_url, video_id, 'mp4', entry_protocol='m3u8_native',
  197. m3u8_id='hls', fatal=False))
  198. standard_sources = xplayer_sources.get('standard')
  199. if isinstance(standard_sources, dict):
  200. for format_id, formats_list in standard_sources.items():
  201. if not isinstance(formats_list, list):
  202. continue
  203. for standard_format in formats_list:
  204. if not isinstance(standard_format, dict):
  205. continue
  206. for standard_format_key in ('url', 'fallback'):
  207. standard_url = standard_format.get(standard_format_key)
  208. if not standard_url:
  209. continue
  210. standard_url = urljoin(url, standard_url)
  211. if not standard_url or standard_url in format_urls:
  212. continue
  213. format_urls.add(standard_url)
  214. ext = determine_ext(standard_url, 'mp4')
  215. if ext == 'm3u8':
  216. formats.extend(self._extract_m3u8_formats(
  217. standard_url, video_id, 'mp4', entry_protocol='m3u8_native',
  218. m3u8_id='hls', fatal=False))
  219. continue
  220. quality = (str_or_none(standard_format.get('quality'))
  221. or str_or_none(standard_format.get('label'))
  222. or '')
  223. formats.append({
  224. 'format_id': '%s-%s' % (format_id, quality),
  225. 'url': standard_url,
  226. 'ext': ext,
  227. 'height': get_height(quality),
  228. 'filesize': format_sizes.get(quality),
  229. 'http_headers': {
  230. 'Referer': standard_url,
  231. },
  232. })
  233. self._sort_formats(formats, field_preference=('height', 'width', 'tbr', 'format_id'))
  234. categories_list = video.get('categories')
  235. if isinstance(categories_list, list):
  236. categories = []
  237. for c in categories_list:
  238. if not isinstance(c, dict):
  239. continue
  240. c_name = c.get('name')
  241. if isinstance(c_name, compat_str):
  242. categories.append(c_name)
  243. else:
  244. categories = None
  245. uploader_url = url_or_none(try_get(video, lambda x: x['author']['pageURL']))
  246. return {
  247. 'id': video_id,
  248. 'display_id': display_id,
  249. 'title': title,
  250. 'description': video.get('description'),
  251. 'timestamp': int_or_none(video.get('created')),
  252. 'uploader': try_get(
  253. video, lambda x: x['author']['name'], compat_str),
  254. 'uploader_url': uploader_url,
  255. 'uploader_id': uploader_url.split('/')[-1] if uploader_url else None,
  256. 'thumbnail': video.get('thumbURL'),
  257. 'duration': int_or_none(video.get('duration')),
  258. 'view_count': int_or_none(video.get('views')),
  259. 'like_count': int_or_none(try_get(
  260. video, lambda x: x['rating']['likes'], int)),
  261. 'dislike_count': int_or_none(try_get(
  262. video, lambda x: x['rating']['dislikes'], int)),
  263. 'comment_count': int_or_none(video.get('views')),
  264. 'age_limit': age_limit if age_limit is not None else 18,
  265. 'categories': categories,
  266. 'formats': formats,
  267. }
  268. # Old layout fallback
  269. title = self._html_search_regex(
  270. [r'<h1[^>]*>([^<]+)</h1>',
  271. r'<meta[^>]+itemprop=".*?caption.*?"[^>]+content="(.+?)"',
  272. r'<title[^>]*>(.+?)(?:,\s*[^,]*?\s*Porn\s*[^,]*?:\s*xHamster[^<]*| - xHamster\.com)</title>'],
  273. webpage, 'title')
  274. formats = []
  275. format_urls = set()
  276. sources = self._parse_json(
  277. self._search_regex(
  278. r'sources\s*:\s*({.+?})\s*,?\s*\n', webpage, 'sources',
  279. default='{}'),
  280. video_id, fatal=False)
  281. for format_id, format_url in sources.items():
  282. format_url = url_or_none(format_url)
  283. if not format_url:
  284. continue
  285. if format_url in format_urls:
  286. continue
  287. format_urls.add(format_url)
  288. formats.append({
  289. 'format_id': format_id,
  290. 'url': format_url,
  291. 'height': get_height(format_id),
  292. })
  293. video_url = self._search_regex(
  294. [r'''file\s*:\s*(?P<q>["'])(?P<mp4>.+?)(?P=q)''',
  295. r'''<a\s+href=(?P<q>["'])(?P<mp4>.+?)(?P=q)\s+class=["']mp4Thumb''',
  296. r'''<video[^>]+file=(?P<q>["'])(?P<mp4>.+?)(?P=q)[^>]*>'''],
  297. webpage, 'video url', group='mp4', default=None)
  298. if video_url and video_url not in format_urls:
  299. formats.append({
  300. 'url': video_url,
  301. })
  302. self._sort_formats(formats)
  303. # Only a few videos have an description
  304. mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
  305. description = mobj.group(1) if mobj else None
  306. upload_date = unified_strdate(self._search_regex(
  307. r'hint=["\'](\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}',
  308. webpage, 'upload date', fatal=False))
  309. uploader = self._html_search_regex(
  310. r'<span[^>]+itemprop=["\']author[^>]+><a[^>]+><span[^>]+>([^<]+)',
  311. webpage, 'uploader', default='anonymous')
  312. thumbnail = self._search_regex(
  313. [r'''["']thumbUrl["']\s*:\s*(?P<q>["'])(?P<thumbnail>.+?)(?P=q)''',
  314. r'''<video[^>]+"poster"=(?P<q>["'])(?P<thumbnail>.+?)(?P=q)[^>]*>'''],
  315. webpage, 'thumbnail', fatal=False, group='thumbnail')
  316. duration = parse_duration(self._search_regex(
  317. [r'<[^<]+\bitemprop=["\']duration["\'][^<]+\bcontent=["\'](.+?)["\']',
  318. r'Runtime:\s*</span>\s*([\d:]+)'], webpage,
  319. 'duration', fatal=False))
  320. view_count = int_or_none(self._search_regex(
  321. r'content=["\']User(?:View|Play)s:(\d+)',
  322. webpage, 'view count', fatal=False))
  323. mobj = re.search(r'hint=[\'"](?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes', webpage)
  324. (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
  325. mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
  326. comment_count = mobj.group('commentcount') if mobj else 0
  327. categories_html = self._search_regex(
  328. r'(?s)<table.+?(<span>Categories:.+?)</table>', webpage,
  329. 'categories', default=None)
  330. categories = [clean_html(category) for category in re.findall(
  331. r'<a[^>]+>(.+?)</a>', categories_html)] if categories_html else None
  332. return {
  333. 'id': video_id,
  334. 'display_id': display_id,
  335. 'title': title,
  336. 'description': description,
  337. 'upload_date': upload_date,
  338. 'uploader': uploader,
  339. 'uploader_id': uploader.lower() if uploader else None,
  340. 'thumbnail': thumbnail,
  341. 'duration': duration,
  342. 'view_count': view_count,
  343. 'like_count': int_or_none(like_count),
  344. 'dislike_count': int_or_none(dislike_count),
  345. 'comment_count': int_or_none(comment_count),
  346. 'age_limit': age_limit,
  347. 'categories': categories,
  348. 'formats': formats,
  349. }
  350. class XHamsterEmbedIE(InfoExtractor):
  351. _VALID_URL = r'https?://(?:.+?\.)?%s/xembed\.php\?video=(?P<id>\d+)' % XHamsterIE._DOMAINS
  352. _TEST = {
  353. 'url': 'http://xhamster.com/xembed.php?video=3328539',
  354. 'info_dict': {
  355. 'id': '3328539',
  356. 'ext': 'mp4',
  357. 'title': 'Pen Masturbation',
  358. 'timestamp': 1406581861,
  359. 'upload_date': '20140728',
  360. 'uploader': 'ManyakisArt',
  361. 'duration': 5,
  362. 'age_limit': 18,
  363. }
  364. }
  365. @staticmethod
  366. def _extract_urls(webpage):
  367. return [url for _, url in re.findall(
  368. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?xhamster\.com/xembed\.php\?video=\d+)\1',
  369. webpage)]
  370. def _real_extract(self, url):
  371. video_id = self._match_id(url)
  372. webpage = self._download_webpage(url, video_id)
  373. video_url = self._search_regex(
  374. r'href="(https?://xhamster\.com/(?:movies/{0}/[^"]*\.html|videos/[^/]*-{0})[^"]*)"'.format(video_id),
  375. webpage, 'xhamster url', default=None)
  376. if not video_url:
  377. vars = self._parse_json(
  378. self._search_regex(r'vars\s*:\s*({.+?})\s*,\s*\n', webpage, 'vars'),
  379. video_id)
  380. video_url = dict_get(vars, ('downloadLink', 'homepageLink', 'commentsLink', 'shareUrl'))
  381. return self.url_result(video_url, 'XHamster')
  382. class XHamsterUserIE(InfoExtractor):
  383. _VALID_URL = r'https?://(?:.+?\.)?%s/users/(?P<id>[^/?#&]+)' % XHamsterIE._DOMAINS
  384. _TESTS = [{
  385. # Paginated user profile
  386. 'url': 'https://xhamster.com/users/netvideogirls/videos',
  387. 'info_dict': {
  388. 'id': 'netvideogirls',
  389. },
  390. 'playlist_mincount': 267,
  391. }, {
  392. # Non-paginated user profile
  393. 'url': 'https://xhamster.com/users/firatkaan/videos',
  394. 'info_dict': {
  395. 'id': 'firatkaan',
  396. },
  397. 'playlist_mincount': 1,
  398. }, {
  399. 'url': 'https://xhday.com/users/mobhunter',
  400. 'only_matching': True,
  401. }, {
  402. 'url': 'https://xhvid.com/users/pelushe21',
  403. 'only_matching': True,
  404. }]
  405. def _entries(self, user_id):
  406. next_page_url = 'https://xhamster.com/users/%s/videos/1' % user_id
  407. for pagenum in itertools.count(1):
  408. page = self._download_webpage(
  409. next_page_url, user_id, 'Downloading page %s' % pagenum)
  410. for video_tag in re.findall(
  411. r'(<a[^>]+class=["\'].*?\bvideo-thumb__image-container[^>]+>)',
  412. page):
  413. video = extract_attributes(video_tag)
  414. video_url = url_or_none(video.get('href'))
  415. if not video_url or not XHamsterIE.suitable(video_url):
  416. continue
  417. video_id = XHamsterIE._match_id(video_url)
  418. yield self.url_result(
  419. video_url, ie=XHamsterIE.ie_key(), video_id=video_id)
  420. mobj = re.search(r'<a[^>]+data-page=["\']next[^>]+>', page)
  421. if not mobj:
  422. break
  423. next_page = extract_attributes(mobj.group(0))
  424. next_page_url = url_or_none(next_page.get('href'))
  425. if not next_page_url:
  426. break
  427. def _real_extract(self, url):
  428. user_id = self._match_id(url)
  429. return self.playlist_result(self._entries(user_id), user_id)