pornhub.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. def _real_extract(self, url):
  363. mobj = re.match(self._VALID_URL, url)
  364. host = mobj.group('host')
  365. item_id = mobj.group('id')
  366. page = int_or_none(self._search_regex(
  367. r'\bpage=(\d+)', url, 'page', default=None))
  368. page_url = self._make_page_url(url)
  369. entries = []
  370. for page_num in (page, ) if page is not None else itertools.count(1):
  371. try:
  372. webpage = self._download_webpage(
  373. page_url, item_id, 'Downloading page %d' % page_num,
  374. query={'page': page_num})
  375. except ExtractorError as e:
  376. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 404:
  377. break
  378. raise
  379. page_entries = self._extract_entries(webpage, host)
  380. if not page_entries:
  381. break
  382. entries.extend(page_entries)
  383. if not self._has_more(webpage):
  384. break
  385. return self.playlist_result(orderedSet(entries), item_id)
  386. class PornHubPagedVideoListIE(PornHubPagedPlaylistBaseIE):
  387. _VALID_URL = r'https?://(?:[^/]+\.)?(?P<host>pornhub\.(?:com|net))/(?P<id>(?:[^/]+/)*[^/?#&]+)'
  388. _TESTS = [{
  389. 'url': 'https://www.pornhub.com/model/zoe_ph/videos',
  390. 'only_matching': True,
  391. }, {
  392. 'url': 'http://www.pornhub.com/users/rushandlia/videos',
  393. 'only_matching': True,
  394. }, {
  395. 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos',
  396. 'info_dict': {
  397. 'id': 'pornstar/jenny-blighe/videos',
  398. },
  399. 'playlist_mincount': 149,
  400. }, {
  401. 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos?page=3',
  402. 'info_dict': {
  403. 'id': 'pornstar/jenny-blighe/videos',
  404. },
  405. 'playlist_mincount': 40,
  406. }, {
  407. # default sorting as Top Rated Videos
  408. 'url': 'https://www.pornhub.com/channels/povd/videos',
  409. 'info_dict': {
  410. 'id': 'channels/povd/videos',
  411. },
  412. 'playlist_mincount': 293,
  413. }, {
  414. # Top Rated Videos
  415. 'url': 'https://www.pornhub.com/channels/povd/videos?o=ra',
  416. 'only_matching': True,
  417. }, {
  418. # Most Recent Videos
  419. 'url': 'https://www.pornhub.com/channels/povd/videos?o=da',
  420. 'only_matching': True,
  421. }, {
  422. # Most Viewed Videos
  423. 'url': 'https://www.pornhub.com/channels/povd/videos?o=vi',
  424. 'only_matching': True,
  425. }, {
  426. 'url': 'http://www.pornhub.com/users/zoe_ph/videos/public',
  427. 'only_matching': True,
  428. }, {
  429. # Most Viewed Videos
  430. 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=mv',
  431. 'only_matching': True,
  432. }, {
  433. # Top Rated Videos
  434. 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=tr',
  435. 'only_matching': True,
  436. }, {
  437. # Longest Videos
  438. 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=lg',
  439. 'only_matching': True,
  440. }, {
  441. # Newest Videos
  442. 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=cm',
  443. 'only_matching': True,
  444. }, {
  445. 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos/paid',
  446. 'only_matching': True,
  447. }, {
  448. 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos/fanonly',
  449. 'only_matching': True,
  450. }, {
  451. 'url': 'https://www.pornhub.com/video',
  452. 'only_matching': True,
  453. }, {
  454. 'url': 'https://www.pornhub.com/video?page=3',
  455. 'only_matching': True,
  456. }, {
  457. 'url': 'https://www.pornhub.com/video/search?search=123',
  458. 'only_matching': True,
  459. }, {
  460. 'url': 'https://www.pornhub.com/categories/teen',
  461. 'only_matching': True,
  462. }, {
  463. 'url': 'https://www.pornhub.com/categories/teen?page=3',
  464. 'only_matching': True,
  465. }, {
  466. 'url': 'https://www.pornhub.com/hd',
  467. 'only_matching': True,
  468. }, {
  469. 'url': 'https://www.pornhub.com/hd?page=3',
  470. 'only_matching': True,
  471. }, {
  472. 'url': 'https://www.pornhub.com/described-video',
  473. 'only_matching': True,
  474. }, {
  475. 'url': 'https://www.pornhub.com/described-video?page=2',
  476. 'only_matching': True,
  477. }, {
  478. 'url': 'https://www.pornhub.com/video/incategories/60fps-1/hd-porn',
  479. 'only_matching': True,
  480. }, {
  481. 'url': 'https://www.pornhub.com/playlist/44121572',
  482. 'info_dict': {
  483. 'id': 'playlist/44121572',
  484. },
  485. 'playlist_mincount': 132,
  486. }, {
  487. 'url': 'https://www.pornhub.com/playlist/4667351',
  488. 'only_matching': True,
  489. }, {
  490. 'url': 'https://de.pornhub.com/playlist/4667351',
  491. 'only_matching': True,
  492. }]
  493. @classmethod
  494. def suitable(cls, url):
  495. return (False
  496. if PornHubIE.suitable(url) or PornHubUserIE.suitable(url) or PornHubUserVideosUploadIE.suitable(url)
  497. else super(PornHubPagedVideoListIE, cls).suitable(url))
  498. def _make_page_url(self, url):
  499. return url
  500. @staticmethod
  501. def _has_more(webpage):
  502. return re.search(
  503. r'''(?x)
  504. <li[^>]+\bclass=["\']page_next|
  505. <link[^>]+\brel=["\']next|
  506. <button[^>]+\bid=["\']moreDataBtn
  507. ''', webpage) is not None
  508. class PornHubUserVideosUploadIE(PornHubPagedPlaylistBaseIE):
  509. _VALID_URL = r'(?P<url>https?://(?:[^/]+\.)?(?P<host>pornhub\.(?:com|net))/(?:(?:user|channel)s|model|pornstar)/(?P<id>[^/]+)/videos/upload)'
  510. _TESTS = [{
  511. 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos/upload',
  512. 'info_dict': {
  513. 'id': 'jenny-blighe',
  514. },
  515. 'playlist_mincount': 129,
  516. }, {
  517. 'url': 'https://www.pornhub.com/model/zoe_ph/videos/upload',
  518. 'only_matching': True,
  519. }]
  520. def _make_page_url(self, url):
  521. mobj = re.match(self._VALID_URL, url)
  522. return '%s/ajax' % mobj.group('url')
  523. @staticmethod
  524. def _has_more(webpage):
  525. return True