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.

328 lines
11 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import functools
  4. import itertools
  5. import operator
  6. # import os
  7. import re
  8. from .common import InfoExtractor
  9. from ..compat import (
  10. compat_HTTPError,
  11. # compat_urllib_parse_unquote,
  12. # compat_urllib_parse_unquote_plus,
  13. # compat_urllib_parse_urlparse,
  14. )
  15. from ..utils import (
  16. ExtractorError,
  17. int_or_none,
  18. js_to_json,
  19. orderedSet,
  20. # sanitized_Request,
  21. remove_quotes,
  22. str_to_int,
  23. )
  24. # from ..aes import (
  25. # aes_decrypt_text
  26. # )
  27. class PornHubIE(InfoExtractor):
  28. IE_DESC = 'PornHub and Thumbzilla'
  29. _VALID_URL = r'''(?x)
  30. https?://
  31. (?:
  32. (?:[a-z]+\.)?pornhub\.com/(?:(?:view_video\.php|video/show)\?viewkey=|embed/)|
  33. (?:www\.)?thumbzilla\.com/video/
  34. )
  35. (?P<id>[\da-z]+)
  36. '''
  37. _TESTS = [{
  38. 'url': 'http://www.pornhub.com/view_video.php?viewkey=648719015',
  39. 'md5': '1e19b41231a02eba417839222ac9d58e',
  40. 'info_dict': {
  41. 'id': '648719015',
  42. 'ext': 'mp4',
  43. 'title': 'Seductive Indian beauty strips down and fingers her pink pussy',
  44. 'uploader': 'Babes',
  45. 'duration': 361,
  46. 'view_count': int,
  47. 'like_count': int,
  48. 'dislike_count': int,
  49. 'comment_count': int,
  50. 'age_limit': 18,
  51. 'tags': list,
  52. 'categories': list,
  53. },
  54. }, {
  55. # non-ASCII title
  56. 'url': 'http://www.pornhub.com/view_video.php?viewkey=1331683002',
  57. 'info_dict': {
  58. 'id': '1331683002',
  59. 'ext': 'mp4',
  60. 'title': '重庆婷婷女王足交',
  61. 'uploader': 'cj397186295',
  62. 'duration': 1753,
  63. 'view_count': int,
  64. 'like_count': int,
  65. 'dislike_count': int,
  66. 'comment_count': int,
  67. 'age_limit': 18,
  68. 'tags': list,
  69. 'categories': list,
  70. },
  71. 'params': {
  72. 'skip_download': True,
  73. },
  74. }, {
  75. 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph557bbb6676d2d',
  76. 'only_matching': True,
  77. }, {
  78. # removed at the request of cam4.com
  79. 'url': 'http://fr.pornhub.com/view_video.php?viewkey=ph55ca2f9760862',
  80. 'only_matching': True,
  81. }, {
  82. # removed at the request of the copyright owner
  83. 'url': 'http://www.pornhub.com/view_video.php?viewkey=788152859',
  84. 'only_matching': True,
  85. }, {
  86. # removed by uploader
  87. 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph572716d15a111',
  88. 'only_matching': True,
  89. }, {
  90. # private video
  91. 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph56fd731fce6b7',
  92. 'only_matching': True,
  93. }, {
  94. 'url': 'https://www.thumbzilla.com/video/ph56c6114abd99a/horny-girlfriend-sex',
  95. 'only_matching': True,
  96. }, {
  97. 'url': 'http://www.pornhub.com/video/show?viewkey=648719015',
  98. 'only_matching': True,
  99. }]
  100. @staticmethod
  101. def _extract_urls(webpage):
  102. return re.findall(
  103. r'<iframe[^>]+?src=["\'](?P<url>(?:https?:)?//(?:www\.)?pornhub\.com/embed/[\da-z]+)',
  104. webpage)
  105. def _extract_count(self, pattern, webpage, name):
  106. return str_to_int(self._search_regex(
  107. pattern, webpage, '%s count' % name, fatal=False))
  108. def _real_extract(self, url):
  109. video_id = self._match_id(url)
  110. self._set_cookie('pornhub.com', 'age_verified', '1')
  111. def dl_webpage(platform):
  112. self._set_cookie('pornhub.com', 'platform', platform)
  113. return self._download_webpage(
  114. 'http://www.pornhub.com/view_video.php?viewkey=%s' % video_id,
  115. video_id)
  116. webpage = dl_webpage('pc')
  117. error_msg = self._html_search_regex(
  118. r'(?s)<div[^>]+class=(["\'])(?:(?!\1).)*\b(?:removed|userMessageSection)\b(?:(?!\1).)*\1[^>]*>(?P<error>.+?)</div>',
  119. webpage, 'error message', default=None, group='error')
  120. if error_msg:
  121. error_msg = re.sub(r'\s+', ' ', error_msg)
  122. raise ExtractorError(
  123. 'PornHub said: %s' % error_msg,
  124. expected=True, video_id=video_id)
  125. tv_webpage = dl_webpage('tv')
  126. assignments = self._search_regex(
  127. r'(var.+?mediastring.+?)</script>', tv_webpage,
  128. 'encoded url').split(';')
  129. js_vars = {}
  130. def parse_js_value(inp):
  131. inp = re.sub(r'/\*(?:(?!\*/).)*?\*/', '', inp)
  132. if '+' in inp:
  133. inps = inp.split('+')
  134. return functools.reduce(
  135. operator.concat, map(parse_js_value, inps))
  136. inp = inp.strip()
  137. if inp in js_vars:
  138. return js_vars[inp]
  139. return remove_quotes(inp)
  140. for assn in assignments:
  141. assn = assn.strip()
  142. if not assn:
  143. continue
  144. assn = re.sub(r'var\s+', '', assn)
  145. vname, value = assn.split('=', 1)
  146. js_vars[vname] = parse_js_value(value)
  147. video_url = js_vars['mediastring']
  148. title = self._search_regex(
  149. r'<h1>([^>]+)</h1>', tv_webpage, 'title', default=None)
  150. # video_title from flashvars contains whitespace instead of non-ASCII (see
  151. # http://www.pornhub.com/view_video.php?viewkey=1331683002), not relying
  152. # on that anymore.
  153. title = title or self._html_search_meta(
  154. 'twitter:title', webpage, default=None) or self._search_regex(
  155. (r'<h1[^>]+class=["\']title["\'][^>]*>(?P<title>[^<]+)',
  156. r'<div[^>]+data-video-title=(["\'])(?P<title>.+?)\1',
  157. r'shareTitle\s*=\s*(["\'])(?P<title>.+?)\1'),
  158. webpage, 'title', group='title')
  159. flashvars = self._parse_json(
  160. self._search_regex(
  161. r'var\s+flashvars_\d+\s*=\s*({.+?});', webpage, 'flashvars', default='{}'),
  162. video_id)
  163. if flashvars:
  164. thumbnail = flashvars.get('image_url')
  165. duration = int_or_none(flashvars.get('video_duration'))
  166. else:
  167. title, thumbnail, duration = [None] * 3
  168. video_uploader = self._html_search_regex(
  169. r'(?s)From:&nbsp;.+?<(?:a\b[^>]+\bhref=["\']/(?:user|channel)s/|span\b[^>]+\bclass=["\']username)[^>]+>(.+?)<',
  170. webpage, 'uploader', fatal=False)
  171. view_count = self._extract_count(
  172. r'<span class="count">([\d,\.]+)</span> views', webpage, 'view')
  173. like_count = self._extract_count(
  174. r'<span class="votesUp">([\d,\.]+)</span>', webpage, 'like')
  175. dislike_count = self._extract_count(
  176. r'<span class="votesDown">([\d,\.]+)</span>', webpage, 'dislike')
  177. comment_count = self._extract_count(
  178. r'All Comments\s*<span>\(([\d,.]+)\)', webpage, 'comment')
  179. page_params = self._parse_json(self._search_regex(
  180. r'page_params\.zoneDetails\[([\'"])[^\'"]+\1\]\s*=\s*(?P<data>{[^}]+})',
  181. webpage, 'page parameters', group='data', default='{}'),
  182. video_id, transform_source=js_to_json, fatal=False)
  183. tags = categories = None
  184. if page_params:
  185. tags = page_params.get('tags', '').split(',')
  186. categories = page_params.get('categories', '').split(',')
  187. return {
  188. 'id': video_id,
  189. 'url': video_url,
  190. 'uploader': video_uploader,
  191. 'title': title,
  192. 'thumbnail': thumbnail,
  193. 'duration': duration,
  194. 'view_count': view_count,
  195. 'like_count': like_count,
  196. 'dislike_count': dislike_count,
  197. 'comment_count': comment_count,
  198. # 'formats': formats,
  199. 'age_limit': 18,
  200. 'tags': tags,
  201. 'categories': categories,
  202. }
  203. class PornHubPlaylistBaseIE(InfoExtractor):
  204. def _extract_entries(self, webpage):
  205. # Only process container div with main playlist content skipping
  206. # drop-down menu that uses similar pattern for videos (see
  207. # https://github.com/rg3/youtube-dl/issues/11594).
  208. container = self._search_regex(
  209. r'(?s)(<div[^>]+class=["\']container.+)', webpage,
  210. 'container', default=webpage)
  211. return [
  212. self.url_result(
  213. 'http://www.pornhub.com/%s' % video_url,
  214. PornHubIE.ie_key(), video_title=title)
  215. for video_url, title in orderedSet(re.findall(
  216. r'href="/?(view_video\.php\?.*\bviewkey=[\da-z]+[^"]*)"[^>]*\s+title="([^"]+)"',
  217. container))
  218. ]
  219. def _real_extract(self, url):
  220. playlist_id = self._match_id(url)
  221. webpage = self._download_webpage(url, playlist_id)
  222. entries = self._extract_entries(webpage)
  223. playlist = self._parse_json(
  224. self._search_regex(
  225. r'(?:playlistObject|PLAYLIST_VIEW)\s*=\s*({.+?});', webpage,
  226. 'playlist', default='{}'),
  227. playlist_id, fatal=False)
  228. title = playlist.get('title') or self._search_regex(
  229. r'>Videos\s+in\s+(.+?)\s+[Pp]laylist<', webpage, 'title', fatal=False)
  230. return self.playlist_result(
  231. entries, playlist_id, title, playlist.get('description'))
  232. class PornHubPlaylistIE(PornHubPlaylistBaseIE):
  233. _VALID_URL = r'https?://(?:www\.)?pornhub\.com/playlist/(?P<id>\d+)'
  234. _TESTS = [{
  235. 'url': 'http://www.pornhub.com/playlist/4667351',
  236. 'info_dict': {
  237. 'id': '4667351',
  238. 'title': 'Nataly Hot',
  239. },
  240. 'playlist_mincount': 2,
  241. }]
  242. class PornHubUserVideosIE(PornHubPlaylistBaseIE):
  243. _VALID_URL = r'https?://(?:www\.)?pornhub\.com/(?:user|channel)s/(?P<id>[^/]+)/videos'
  244. _TESTS = [{
  245. 'url': 'http://www.pornhub.com/users/zoe_ph/videos/public',
  246. 'info_dict': {
  247. 'id': 'zoe_ph',
  248. },
  249. 'playlist_mincount': 171,
  250. }, {
  251. 'url': 'http://www.pornhub.com/users/rushandlia/videos',
  252. 'only_matching': True,
  253. }, {
  254. # default sorting as Top Rated Videos
  255. 'url': 'https://www.pornhub.com/channels/povd/videos',
  256. 'info_dict': {
  257. 'id': 'povd',
  258. },
  259. 'playlist_mincount': 293,
  260. }, {
  261. # Top Rated Videos
  262. 'url': 'https://www.pornhub.com/channels/povd/videos?o=ra',
  263. 'only_matching': True,
  264. }, {
  265. # Most Recent Videos
  266. 'url': 'https://www.pornhub.com/channels/povd/videos?o=da',
  267. 'only_matching': True,
  268. }, {
  269. # Most Viewed Videos
  270. 'url': 'https://www.pornhub.com/channels/povd/videos?o=vi',
  271. 'only_matching': True,
  272. }]
  273. def _real_extract(self, url):
  274. user_id = self._match_id(url)
  275. entries = []
  276. for page_num in itertools.count(1):
  277. try:
  278. webpage = self._download_webpage(
  279. url, user_id, 'Downloading page %d' % page_num,
  280. query={'page': page_num})
  281. except ExtractorError as e:
  282. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 404:
  283. break
  284. raise
  285. page_entries = self._extract_entries(webpage)
  286. if not page_entries:
  287. break
  288. entries.extend(page_entries)
  289. return self.playlist_result(entries, user_id)