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.

569 lines
20 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import functools
  4. import itertools
  5. import operator
  6. import re
  7. from .common import InfoExtractor
  8. from ..compat import (
  9. compat_HTTPError,
  10. compat_str,
  11. compat_urllib_request,
  12. )
  13. from .openload import PhantomJSwrapper
  14. from ..utils import (
  15. determine_ext,
  16. ExtractorError,
  17. int_or_none,
  18. orderedSet,
  19. remove_quotes,
  20. str_to_int,
  21. url_or_none,
  22. )
  23. class PornHubBaseIE(InfoExtractor):
  24. def _download_webpage_handle(self, *args, **kwargs):
  25. def dl(*args, **kwargs):
  26. return super(PornHubBaseIE, self)._download_webpage_handle(*args, **kwargs)
  27. webpage, urlh = dl(*args, **kwargs)
  28. if any(re.search(p, webpage) for p in (
  29. r'<body\b[^>]+\bonload=["\']go\(\)',
  30. r'document\.cookie\s*=\s*["\']RNKEY=',
  31. r'document\.location\.reload\(true\)')):
  32. url_or_request = args[0]
  33. url = (url_or_request.get_full_url()
  34. if isinstance(url_or_request, compat_urllib_request.Request)
  35. else url_or_request)
  36. phantom = PhantomJSwrapper(self, required_version='2.0')
  37. phantom.get(url, html=webpage)
  38. webpage, urlh = dl(*args, **kwargs)
  39. return webpage, urlh
  40. class PornHubIE(PornHubBaseIE):
  41. IE_DESC = 'PornHub and Thumbzilla'
  42. _VALID_URL = r'''(?x)
  43. https?://
  44. (?:
  45. (?:[^/]+\.)?(?P<host>pornhub\.(?:com|net))/(?:(?:view_video\.php|video/show)\?viewkey=|embed/)|
  46. (?:www\.)?thumbzilla\.com/video/
  47. )
  48. (?P<id>[\da-z]+)
  49. '''
  50. _TESTS = [{
  51. 'url': 'http://www.pornhub.com/view_video.php?viewkey=648719015',
  52. 'md5': '1e19b41231a02eba417839222ac9d58e',
  53. 'info_dict': {
  54. 'id': '648719015',
  55. 'ext': 'mp4',
  56. 'title': 'Seductive Indian beauty strips down and fingers her pink pussy',
  57. 'uploader': 'Babes',
  58. 'upload_date': '20130628',
  59. 'duration': 361,
  60. 'view_count': int,
  61. 'like_count': int,
  62. 'dislike_count': int,
  63. 'comment_count': int,
  64. 'age_limit': 18,
  65. 'tags': list,
  66. 'categories': list,
  67. },
  68. }, {
  69. # non-ASCII title
  70. 'url': 'http://www.pornhub.com/view_video.php?viewkey=1331683002',
  71. 'info_dict': {
  72. 'id': '1331683002',
  73. 'ext': 'mp4',
  74. 'title': '重庆婷婷女王足交',
  75. 'uploader': 'Unknown',
  76. 'upload_date': '20150213',
  77. 'duration': 1753,
  78. 'view_count': int,
  79. 'like_count': int,
  80. 'dislike_count': int,
  81. 'comment_count': int,
  82. 'age_limit': 18,
  83. 'tags': list,
  84. 'categories': list,
  85. },
  86. 'params': {
  87. 'skip_download': True,
  88. },
  89. }, {
  90. # subtitles
  91. 'url': 'https://www.pornhub.com/view_video.php?viewkey=ph5af5fef7c2aa7',
  92. 'info_dict': {
  93. 'id': 'ph5af5fef7c2aa7',
  94. 'ext': 'mp4',
  95. 'title': 'BFFS - Cute Teen Girls Share Cock On the Floor',
  96. 'uploader': 'BFFs',
  97. 'duration': 622,
  98. 'view_count': int,
  99. 'like_count': int,
  100. 'dislike_count': int,
  101. 'comment_count': int,
  102. 'age_limit': 18,
  103. 'tags': list,
  104. 'categories': list,
  105. 'subtitles': {
  106. 'en': [{
  107. "ext": 'srt'
  108. }]
  109. },
  110. },
  111. 'params': {
  112. 'skip_download': True,
  113. },
  114. }, {
  115. 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph557bbb6676d2d',
  116. 'only_matching': True,
  117. }, {
  118. # removed at the request of cam4.com
  119. 'url': 'http://fr.pornhub.com/view_video.php?viewkey=ph55ca2f9760862',
  120. 'only_matching': True,
  121. }, {
  122. # removed at the request of the copyright owner
  123. 'url': 'http://www.pornhub.com/view_video.php?viewkey=788152859',
  124. 'only_matching': True,
  125. }, {
  126. # removed by uploader
  127. 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph572716d15a111',
  128. 'only_matching': True,
  129. }, {
  130. # private video
  131. 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph56fd731fce6b7',
  132. 'only_matching': True,
  133. }, {
  134. 'url': 'https://www.thumbzilla.com/video/ph56c6114abd99a/horny-girlfriend-sex',
  135. 'only_matching': True,
  136. }, {
  137. 'url': 'http://www.pornhub.com/video/show?viewkey=648719015',
  138. 'only_matching': True,
  139. }, {
  140. 'url': 'https://www.pornhub.net/view_video.php?viewkey=203640933',
  141. 'only_matching': True,
  142. }]
  143. @staticmethod
  144. def _extract_urls(webpage):
  145. return re.findall(
  146. r'<iframe[^>]+?src=["\'](?P<url>(?:https?:)?//(?:www\.)?pornhub\.(?:com|net)/embed/[\da-z]+)',
  147. webpage)
  148. def _extract_count(self, pattern, webpage, name):
  149. return str_to_int(self._search_regex(
  150. pattern, webpage, '%s count' % name, fatal=False))
  151. def _real_extract(self, url):
  152. mobj = re.match(self._VALID_URL, url)
  153. host = mobj.group('host') or 'pornhub.com'
  154. video_id = mobj.group('id')
  155. self._set_cookie(host, 'age_verified', '1')
  156. def dl_webpage(platform):
  157. self._set_cookie(host, 'platform', platform)
  158. return self._download_webpage(
  159. 'https://www.%s/view_video.php?viewkey=%s' % (host, video_id),
  160. video_id, 'Downloading %s webpage' % platform)
  161. webpage = dl_webpage('pc')
  162. error_msg = self._html_search_regex(
  163. r'(?s)<div[^>]+class=(["\'])(?:(?!\1).)*\b(?:removed|userMessageSection)\b(?:(?!\1).)*\1[^>]*>(?P<error>.+?)</div>',
  164. webpage, 'error message', default=None, group='error')
  165. if error_msg:
  166. error_msg = re.sub(r'\s+', ' ', error_msg)
  167. raise ExtractorError(
  168. 'PornHub said: %s' % error_msg,
  169. expected=True, video_id=video_id)
  170. # video_title from flashvars contains whitespace instead of non-ASCII (see
  171. # http://www.pornhub.com/view_video.php?viewkey=1331683002), not relying
  172. # on that anymore.
  173. title = self._html_search_meta(
  174. 'twitter:title', webpage, default=None) or self._search_regex(
  175. (r'<h1[^>]+class=["\']title["\'][^>]*>(?P<title>[^<]+)',
  176. r'<div[^>]+data-video-title=(["\'])(?P<title>.+?)\1',
  177. r'shareTitle\s*=\s*(["\'])(?P<title>.+?)\1'),
  178. webpage, 'title', group='title')
  179. video_urls = []
  180. video_urls_set = set()
  181. subtitles = {}
  182. flashvars = self._parse_json(
  183. self._search_regex(
  184. r'var\s+flashvars_\d+\s*=\s*({.+?});', webpage, 'flashvars', default='{}'),
  185. video_id)
  186. if flashvars:
  187. subtitle_url = url_or_none(flashvars.get('closedCaptionsFile'))
  188. if subtitle_url:
  189. subtitles.setdefault('en', []).append({
  190. 'url': subtitle_url,
  191. 'ext': 'srt',
  192. })
  193. thumbnail = flashvars.get('image_url')
  194. duration = int_or_none(flashvars.get('video_duration'))
  195. media_definitions = flashvars.get('mediaDefinitions')
  196. if isinstance(media_definitions, list):
  197. for definition in media_definitions:
  198. if not isinstance(definition, dict):
  199. continue
  200. video_url = definition.get('videoUrl')
  201. if not video_url or not isinstance(video_url, compat_str):
  202. continue
  203. if video_url in video_urls_set:
  204. continue
  205. video_urls_set.add(video_url)
  206. video_urls.append(
  207. (video_url, int_or_none(definition.get('quality'))))
  208. else:
  209. thumbnail, duration = [None] * 2
  210. if not video_urls:
  211. tv_webpage = dl_webpage('tv')
  212. assignments = self._search_regex(
  213. r'(var.+?mediastring.+?)</script>', tv_webpage,
  214. 'encoded url').split(';')
  215. js_vars = {}
  216. def parse_js_value(inp):
  217. inp = re.sub(r'/\*(?:(?!\*/).)*?\*/', '', inp)
  218. if '+' in inp:
  219. inps = inp.split('+')
  220. return functools.reduce(
  221. operator.concat, map(parse_js_value, inps))
  222. inp = inp.strip()
  223. if inp in js_vars:
  224. return js_vars[inp]
  225. return remove_quotes(inp)
  226. for assn in assignments:
  227. assn = assn.strip()
  228. if not assn:
  229. continue
  230. assn = re.sub(r'var\s+', '', assn)
  231. vname, value = assn.split('=', 1)
  232. js_vars[vname] = parse_js_value(value)
  233. video_url = js_vars['mediastring']
  234. if video_url not in video_urls_set:
  235. video_urls.append((video_url, None))
  236. video_urls_set.add(video_url)
  237. for mobj in re.finditer(
  238. r'<a[^>]+\bclass=["\']downloadBtn\b[^>]+\bhref=(["\'])(?P<url>(?:(?!\1).)+)\1',
  239. webpage):
  240. video_url = mobj.group('url')
  241. if video_url not in video_urls_set:
  242. video_urls.append((video_url, None))
  243. video_urls_set.add(video_url)
  244. upload_date = None
  245. formats = []
  246. for video_url, height in video_urls:
  247. if not upload_date:
  248. upload_date = self._search_regex(
  249. r'/(\d{6}/\d{2})/', video_url, 'upload data', default=None)
  250. if upload_date:
  251. upload_date = upload_date.replace('/', '')
  252. if determine_ext(video_url) == 'mpd':
  253. formats.extend(self._extract_mpd_formats(
  254. video_url, video_id, mpd_id='dash', fatal=False))
  255. continue
  256. tbr = None
  257. mobj = re.search(r'(?P<height>\d+)[pP]?_(?P<tbr>\d+)[kK]', video_url)
  258. if mobj:
  259. if not height:
  260. height = int(mobj.group('height'))
  261. tbr = int(mobj.group('tbr'))
  262. formats.append({
  263. 'url': video_url,
  264. 'format_id': '%dp' % height if height else None,
  265. 'height': height,
  266. 'tbr': tbr,
  267. })
  268. self._sort_formats(formats)
  269. video_uploader = self._html_search_regex(
  270. r'(?s)From:&nbsp;.+?<(?:a\b[^>]+\bhref=["\']/(?:(?:user|channel)s|model|pornstar)/|span\b[^>]+\bclass=["\']username)[^>]+>(.+?)<',
  271. webpage, 'uploader', fatal=False)
  272. view_count = self._extract_count(
  273. r'<span class="count">([\d,\.]+)</span> views', webpage, 'view')
  274. like_count = self._extract_count(
  275. r'<span class="votesUp">([\d,\.]+)</span>', webpage, 'like')
  276. dislike_count = self._extract_count(
  277. r'<span class="votesDown">([\d,\.]+)</span>', webpage, 'dislike')
  278. comment_count = self._extract_count(
  279. r'All Comments\s*<span>\(([\d,.]+)\)', webpage, 'comment')
  280. def extract_list(meta_key):
  281. div = self._search_regex(
  282. r'(?s)<div[^>]+\bclass=["\'].*?\b%sWrapper[^>]*>(.+?)</div>'
  283. % meta_key, webpage, meta_key, default=None)
  284. if div:
  285. return re.findall(r'<a[^>]+\bhref=[^>]+>([^<]+)', div)
  286. return {
  287. 'id': video_id,
  288. 'uploader': video_uploader,
  289. 'upload_date': upload_date,
  290. 'title': title,
  291. 'thumbnail': thumbnail,
  292. 'duration': duration,
  293. 'view_count': view_count,
  294. 'like_count': like_count,
  295. 'dislike_count': dislike_count,
  296. 'comment_count': comment_count,
  297. 'formats': formats,
  298. 'age_limit': 18,
  299. 'tags': extract_list('tags'),
  300. 'categories': extract_list('categories'),
  301. 'subtitles': subtitles,
  302. }
  303. class PornHubPlaylistBaseIE(PornHubBaseIE):
  304. def _extract_entries(self, webpage, host):
  305. # Only process container div with main playlist content skipping
  306. # drop-down menu that uses similar pattern for videos (see
  307. # https://github.com/ytdl-org/youtube-dl/issues/11594).
  308. container = self._search_regex(
  309. r'(?s)(<div[^>]+class=["\']container.+)', webpage,
  310. 'container', default=webpage)
  311. return [
  312. self.url_result(
  313. 'http://www.%s/%s' % (host, video_url),
  314. PornHubIE.ie_key(), video_title=title)
  315. for video_url, title in orderedSet(re.findall(
  316. r'href="/?(view_video\.php\?.*\bviewkey=[\da-z]+[^"]*)"[^>]*\s+title="([^"]+)"',
  317. container))
  318. ]
  319. def _real_extract(self, url):
  320. mobj = re.match(self._VALID_URL, url)
  321. host = mobj.group('host')
  322. playlist_id = mobj.group('id')
  323. webpage = self._download_webpage(url, playlist_id)
  324. entries = self._extract_entries(webpage, host)
  325. playlist = self._parse_json(
  326. self._search_regex(
  327. r'(?:playlistObject|PLAYLIST_VIEW)\s*=\s*({.+?});', webpage,
  328. 'playlist', default='{}'),
  329. playlist_id, fatal=False)
  330. title = playlist.get('title') or self._search_regex(
  331. r'>Videos\s+in\s+(.+?)\s+[Pp]laylist<', webpage, 'title', fatal=False)
  332. return self.playlist_result(
  333. entries, playlist_id, title, playlist.get('description'))
  334. class PornHubUserIE(PornHubPlaylistBaseIE):
  335. _VALID_URL = r'(?P<url>https?://(?:[^/]+\.)?pornhub\.(?:com|net)/(?:(?:user|channel)s|model|pornstar)/(?P<id>[^/?#&]+))(?:[?#&]|/(?!videos)|$)'
  336. _TESTS = [{
  337. 'url': 'https://www.pornhub.com/model/zoe_ph',
  338. 'playlist_mincount': 118,
  339. }, {
  340. 'url': 'https://www.pornhub.com/pornstar/liz-vicious',
  341. 'info_dict': {
  342. 'id': 'liz-vicious',
  343. },
  344. 'playlist_mincount': 118,
  345. }, {
  346. 'url': 'https://www.pornhub.com/users/russianveet69',
  347. 'only_matching': True,
  348. }, {
  349. 'url': 'https://www.pornhub.com/channels/povd',
  350. 'only_matching': True,
  351. }, {
  352. 'url': 'https://www.pornhub.com/model/zoe_ph?abc=1',
  353. 'only_matching': True,
  354. }]
  355. def _real_extract(self, url):
  356. mobj = re.match(self._VALID_URL, url)
  357. user_id = mobj.group('id')
  358. return self.url_result(
  359. '%s/videos' % mobj.group('url'), ie=PornHubPagedVideoListIE.ie_key(),
  360. video_id=user_id)
  361. class PornHubPagedPlaylistBaseIE(PornHubPlaylistBaseIE):
  362. @staticmethod
  363. def _has_more(webpage):
  364. return re.search(
  365. r'''(?x)
  366. <li[^>]+\bclass=["\']page_next|
  367. <link[^>]+\brel=["\']next|
  368. <button[^>]+\bid=["\']moreDataBtn
  369. ''', webpage) is not None
  370. def _real_extract(self, url):
  371. mobj = re.match(self._VALID_URL, url)
  372. host = mobj.group('host')
  373. item_id = mobj.group('id')
  374. page = int_or_none(self._search_regex(
  375. r'\bpage=(\d+)', url, 'page', default=None))
  376. entries = []
  377. for page_num in (page, ) if page is not None else itertools.count(1):
  378. try:
  379. webpage = self._download_webpage(
  380. url, item_id, 'Downloading page %d' % page_num,
  381. query={'page': page_num})
  382. except ExtractorError as e:
  383. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 404:
  384. break
  385. raise
  386. page_entries = self._extract_entries(webpage, host)
  387. if not page_entries:
  388. break
  389. entries.extend(page_entries)
  390. if not self._has_more(webpage):
  391. break
  392. return self.playlist_result(orderedSet(entries), item_id)
  393. class PornHubPagedVideoListIE(PornHubPagedPlaylistBaseIE):
  394. _VALID_URL = r'https?://(?:[^/]+\.)?(?P<host>pornhub\.(?:com|net))/(?P<id>(?:[^/]+/)*[^/?#&]+)'
  395. _TESTS = [{
  396. 'url': 'https://www.pornhub.com/model/zoe_ph/videos',
  397. 'only_matching': True,
  398. }, {
  399. 'url': 'http://www.pornhub.com/users/rushandlia/videos',
  400. 'only_matching': True,
  401. }, {
  402. 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos',
  403. 'info_dict': {
  404. 'id': 'pornstar/jenny-blighe/videos',
  405. },
  406. 'playlist_mincount': 149,
  407. }, {
  408. 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos?page=3',
  409. 'info_dict': {
  410. 'id': 'pornstar/jenny-blighe/videos',
  411. },
  412. 'playlist_mincount': 40,
  413. }, {
  414. # default sorting as Top Rated Videos
  415. 'url': 'https://www.pornhub.com/channels/povd/videos',
  416. 'info_dict': {
  417. 'id': 'channels/povd/videos',
  418. },
  419. 'playlist_mincount': 293,
  420. }, {
  421. # Top Rated Videos
  422. 'url': 'https://www.pornhub.com/channels/povd/videos?o=ra',
  423. 'only_matching': True,
  424. }, {
  425. # Most Recent Videos
  426. 'url': 'https://www.pornhub.com/channels/povd/videos?o=da',
  427. 'only_matching': True,
  428. }, {
  429. # Most Viewed Videos
  430. 'url': 'https://www.pornhub.com/channels/povd/videos?o=vi',
  431. 'only_matching': True,
  432. }, {
  433. 'url': 'http://www.pornhub.com/users/zoe_ph/videos/public',
  434. 'only_matching': True,
  435. }, {
  436. # Most Viewed Videos
  437. 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=mv',
  438. 'only_matching': True,
  439. }, {
  440. # Top Rated Videos
  441. 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=tr',
  442. 'only_matching': True,
  443. }, {
  444. # Longest Videos
  445. 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=lg',
  446. 'only_matching': True,
  447. }, {
  448. # Newest Videos
  449. 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=cm',
  450. 'only_matching': True,
  451. }, {
  452. 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos/paid',
  453. 'only_matching': True,
  454. }, {
  455. 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos/fanonly',
  456. 'only_matching': True,
  457. }, {
  458. 'url': 'https://www.pornhub.com/video',
  459. 'only_matching': True,
  460. }, {
  461. 'url': 'https://www.pornhub.com/video?page=3',
  462. 'only_matching': True,
  463. }, {
  464. 'url': 'https://www.pornhub.com/video/search?search=123',
  465. 'only_matching': True,
  466. }, {
  467. 'url': 'https://www.pornhub.com/categories/teen',
  468. 'only_matching': True,
  469. }, {
  470. 'url': 'https://www.pornhub.com/categories/teen?page=3',
  471. 'only_matching': True,
  472. }, {
  473. 'url': 'https://www.pornhub.com/hd',
  474. 'only_matching': True,
  475. }, {
  476. 'url': 'https://www.pornhub.com/hd?page=3',
  477. 'only_matching': True,
  478. }, {
  479. 'url': 'https://www.pornhub.com/described-video',
  480. 'only_matching': True,
  481. }, {
  482. 'url': 'https://www.pornhub.com/described-video?page=2',
  483. 'only_matching': True,
  484. }, {
  485. 'url': 'https://www.pornhub.com/video/incategories/60fps-1/hd-porn',
  486. 'only_matching': True,
  487. }, {
  488. 'url': 'https://www.pornhub.com/playlist/44121572',
  489. 'info_dict': {
  490. 'id': 'playlist/44121572',
  491. },
  492. 'playlist_mincount': 132,
  493. }, {
  494. 'url': 'https://www.pornhub.com/playlist/4667351',
  495. 'only_matching': True,
  496. }, {
  497. 'url': 'https://de.pornhub.com/playlist/4667351',
  498. 'only_matching': True,
  499. }]
  500. @classmethod
  501. def suitable(cls, url):
  502. return (False
  503. if PornHubIE.suitable(url) or PornHubUserIE.suitable(url) or PornHubUserVideosUploadIE.suitable(url)
  504. else super(PornHubPagedVideoListIE, cls).suitable(url))
  505. class PornHubUserVideosUploadIE(PornHubPagedPlaylistBaseIE):
  506. _VALID_URL = r'(?P<url>https?://(?:[^/]+\.)?(?P<host>pornhub\.(?:com|net))/(?:(?:user|channel)s|model|pornstar)/(?P<id>[^/]+)/videos/upload)'
  507. _TESTS = [{
  508. 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos/upload',
  509. 'info_dict': {
  510. 'id': 'jenny-blighe',
  511. },
  512. 'playlist_mincount': 129,
  513. }, {
  514. 'url': 'https://www.pornhub.com/model/zoe_ph/videos/upload',
  515. 'only_matching': True,
  516. }]