Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

447 rindas
16KB

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