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.

451 lines
16 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. 'http://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 PornHubPlaylistIE(PornHubPlaylistBaseIE):
  335. _VALID_URL = r'https?://(?:[^/]+\.)?(?P<host>pornhub\.(?:com|net))/playlist/(?P<id>\d+)'
  336. _TESTS = [{
  337. 'url': 'http://www.pornhub.com/playlist/4667351',
  338. 'info_dict': {
  339. 'id': '4667351',
  340. 'title': 'Nataly Hot',
  341. },
  342. 'playlist_mincount': 2,
  343. }, {
  344. 'url': 'https://de.pornhub.com/playlist/4667351',
  345. 'only_matching': True,
  346. }]
  347. class PornHubUserVideosIE(PornHubPlaylistBaseIE):
  348. _VALID_URL = r'https?://(?:[^/]+\.)?(?P<host>pornhub\.(?:com|net))/(?:(?:user|channel)s|model|pornstar)/(?P<id>[^/]+)/videos'
  349. _TESTS = [{
  350. 'url': 'http://www.pornhub.com/users/zoe_ph/videos/public',
  351. 'info_dict': {
  352. 'id': 'zoe_ph',
  353. },
  354. 'playlist_mincount': 171,
  355. }, {
  356. 'url': 'http://www.pornhub.com/users/rushandlia/videos',
  357. 'only_matching': True,
  358. }, {
  359. # default sorting as Top Rated Videos
  360. 'url': 'https://www.pornhub.com/channels/povd/videos',
  361. 'info_dict': {
  362. 'id': 'povd',
  363. },
  364. 'playlist_mincount': 293,
  365. }, {
  366. # Top Rated Videos
  367. 'url': 'https://www.pornhub.com/channels/povd/videos?o=ra',
  368. 'only_matching': True,
  369. }, {
  370. # Most Recent Videos
  371. 'url': 'https://www.pornhub.com/channels/povd/videos?o=da',
  372. 'only_matching': True,
  373. }, {
  374. # Most Viewed Videos
  375. 'url': 'https://www.pornhub.com/channels/povd/videos?o=vi',
  376. 'only_matching': True,
  377. }, {
  378. 'url': 'http://www.pornhub.com/users/zoe_ph/videos/public',
  379. 'only_matching': True,
  380. }, {
  381. 'url': 'https://www.pornhub.com/model/jayndrea/videos/upload',
  382. 'only_matching': True,
  383. }, {
  384. 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos/upload',
  385. 'only_matching': True,
  386. }]
  387. def _real_extract(self, url):
  388. mobj = re.match(self._VALID_URL, url)
  389. host = mobj.group('host')
  390. user_id = mobj.group('id')
  391. entries = []
  392. for page_num in itertools.count(1):
  393. try:
  394. webpage = self._download_webpage(
  395. url, user_id, 'Downloading page %d' % page_num,
  396. query={'page': page_num})
  397. except ExtractorError as e:
  398. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 404:
  399. break
  400. raise
  401. page_entries = self._extract_entries(webpage, host)
  402. if not page_entries:
  403. break
  404. entries.extend(page_entries)
  405. return self.playlist_result(entries, user_id)