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.

275 lines
9.5 KiB

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