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.

394 line
15KB

  1. from __future__ import unicode_literals
  2. import itertools
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. clean_html,
  8. determine_ext,
  9. dict_get,
  10. extract_attributes,
  11. ExtractorError,
  12. int_or_none,
  13. parse_duration,
  14. try_get,
  15. unified_strdate,
  16. url_or_none,
  17. )
  18. class XHamsterIE(InfoExtractor):
  19. _DOMAINS = r'(?:xhamster\.(?:com|one|desi)|xhms\.pro|xhamster\d+\.com)'
  20. _VALID_URL = r'''(?x)
  21. https?://
  22. (?:.+?\.)?%s/
  23. (?:
  24. movies/(?P<id>[\dA-Za-z]+)/(?P<display_id>[^/]*)\.html|
  25. videos/(?P<display_id_2>[^/]*)-(?P<id_2>[\dA-Za-z]+)
  26. )
  27. ''' % _DOMAINS
  28. _TESTS = [{
  29. 'url': 'https://xhamster.com/videos/femaleagent-shy-beauty-takes-the-bait-1509445',
  30. 'md5': '98b4687efb1ffd331c4197854dc09e8f',
  31. 'info_dict': {
  32. 'id': '1509445',
  33. 'display_id': 'femaleagent-shy-beauty-takes-the-bait',
  34. 'ext': 'mp4',
  35. 'title': 'FemaleAgent Shy beauty takes the bait',
  36. 'timestamp': 1350194821,
  37. 'upload_date': '20121014',
  38. 'uploader': 'Ruseful2011',
  39. 'duration': 893,
  40. 'age_limit': 18,
  41. },
  42. }, {
  43. 'url': 'https://xhamster.com/videos/britney-spears-sexy-booty-2221348?hd=',
  44. 'info_dict': {
  45. 'id': '2221348',
  46. 'display_id': 'britney-spears-sexy-booty',
  47. 'ext': 'mp4',
  48. 'title': 'Britney Spears Sexy Booty',
  49. 'timestamp': 1379123460,
  50. 'upload_date': '20130914',
  51. 'uploader': 'jojo747400',
  52. 'duration': 200,
  53. 'age_limit': 18,
  54. },
  55. 'params': {
  56. 'skip_download': True,
  57. },
  58. }, {
  59. # empty seo, unavailable via new URL schema
  60. 'url': 'http://xhamster.com/movies/5667973/.html',
  61. 'info_dict': {
  62. 'id': '5667973',
  63. 'ext': 'mp4',
  64. 'title': '....',
  65. 'timestamp': 1454948101,
  66. 'upload_date': '20160208',
  67. 'uploader': 'parejafree',
  68. 'duration': 72,
  69. 'age_limit': 18,
  70. },
  71. 'params': {
  72. 'skip_download': True,
  73. },
  74. }, {
  75. # mobile site
  76. 'url': 'https://m.xhamster.com/videos/cute-teen-jacqueline-solo-masturbation-8559111',
  77. 'only_matching': True,
  78. }, {
  79. 'url': 'https://xhamster.com/movies/2272726/amber_slayed_by_the_knight.html',
  80. 'only_matching': True,
  81. }, {
  82. # This video is visible for marcoalfa123456's friends only
  83. 'url': 'https://it.xhamster.com/movies/7263980/la_mia_vicina.html',
  84. 'only_matching': True,
  85. }, {
  86. # new URL schema
  87. 'url': 'https://pt.xhamster.com/videos/euro-pedal-pumping-7937821',
  88. 'only_matching': True,
  89. }, {
  90. 'url': 'https://xhamster.one/videos/femaleagent-shy-beauty-takes-the-bait-1509445',
  91. 'only_matching': True,
  92. }, {
  93. 'url': 'https://xhamster.desi/videos/femaleagent-shy-beauty-takes-the-bait-1509445',
  94. 'only_matching': True,
  95. }, {
  96. 'url': 'https://xhamster2.com/videos/femaleagent-shy-beauty-takes-the-bait-1509445',
  97. 'only_matching': True,
  98. }, {
  99. 'url': 'https://xhamster11.com/videos/femaleagent-shy-beauty-takes-the-bait-1509445',
  100. 'only_matching': True,
  101. }, {
  102. 'url': 'https://xhamster26.com/videos/femaleagent-shy-beauty-takes-the-bait-1509445',
  103. 'only_matching': True,
  104. }, {
  105. 'url': 'http://xhamster.com/movies/1509445/femaleagent_shy_beauty_takes_the_bait.html',
  106. 'only_matching': True,
  107. }, {
  108. 'url': 'http://xhamster.com/movies/2221348/britney_spears_sexy_booty.html?hd',
  109. 'only_matching': True,
  110. }, {
  111. 'url': 'http://de.xhamster.com/videos/skinny-girl-fucks-herself-hard-in-the-forest-xhnBJZx',
  112. 'only_matching': True,
  113. }]
  114. def _real_extract(self, url):
  115. mobj = re.match(self._VALID_URL, url)
  116. video_id = mobj.group('id') or mobj.group('id_2')
  117. display_id = mobj.group('display_id') or mobj.group('display_id_2')
  118. desktop_url = re.sub(r'^(https?://(?:.+?\.)?)m\.', r'\1', url)
  119. webpage, urlh = self._download_webpage_handle(desktop_url, video_id)
  120. error = self._html_search_regex(
  121. r'<div[^>]+id=["\']videoClosed["\'][^>]*>(.+?)</div>',
  122. webpage, 'error', default=None)
  123. if error:
  124. raise ExtractorError(error, expected=True)
  125. age_limit = self._rta_search(webpage)
  126. def get_height(s):
  127. return int_or_none(self._search_regex(
  128. r'^(\d+)[pP]', s, 'height', default=None))
  129. initials = self._parse_json(
  130. self._search_regex(
  131. r'window\.initials\s*=\s*({.+?})\s*;', webpage, 'initials',
  132. default='{}'),
  133. video_id, fatal=False)
  134. if initials:
  135. video = initials['videoModel']
  136. title = video['title']
  137. formats = []
  138. for format_id, formats_dict in video['sources'].items():
  139. if not isinstance(formats_dict, dict):
  140. continue
  141. for quality, format_item in formats_dict.items():
  142. if format_id == 'download':
  143. # Download link takes some time to be generated,
  144. # skipping for now
  145. continue
  146. if not isinstance(format_item, dict):
  147. continue
  148. format_url = format_item.get('link')
  149. filesize = int_or_none(
  150. format_item.get('size'), invscale=1000000)
  151. else:
  152. format_url = format_item
  153. filesize = None
  154. format_url = url_or_none(format_url)
  155. if not format_url:
  156. continue
  157. formats.append({
  158. 'format_id': '%s-%s' % (format_id, quality),
  159. 'url': format_url,
  160. 'ext': determine_ext(format_url, 'mp4'),
  161. 'height': get_height(quality),
  162. 'filesize': filesize,
  163. 'http_headers': {
  164. 'Referer': urlh.geturl(),
  165. },
  166. })
  167. self._sort_formats(formats)
  168. categories_list = video.get('categories')
  169. if isinstance(categories_list, list):
  170. categories = []
  171. for c in categories_list:
  172. if not isinstance(c, dict):
  173. continue
  174. c_name = c.get('name')
  175. if isinstance(c_name, compat_str):
  176. categories.append(c_name)
  177. else:
  178. categories = None
  179. return {
  180. 'id': video_id,
  181. 'display_id': display_id,
  182. 'title': title,
  183. 'description': video.get('description'),
  184. 'timestamp': int_or_none(video.get('created')),
  185. 'uploader': try_get(
  186. video, lambda x: x['author']['name'], compat_str),
  187. 'thumbnail': video.get('thumbURL'),
  188. 'duration': int_or_none(video.get('duration')),
  189. 'view_count': int_or_none(video.get('views')),
  190. 'like_count': int_or_none(try_get(
  191. video, lambda x: x['rating']['likes'], int)),
  192. 'dislike_count': int_or_none(try_get(
  193. video, lambda x: x['rating']['dislikes'], int)),
  194. 'comment_count': int_or_none(video.get('views')),
  195. 'age_limit': age_limit,
  196. 'categories': categories,
  197. 'formats': formats,
  198. }
  199. # Old layout fallback
  200. title = self._html_search_regex(
  201. [r'<h1[^>]*>([^<]+)</h1>',
  202. r'<meta[^>]+itemprop=".*?caption.*?"[^>]+content="(.+?)"',
  203. r'<title[^>]*>(.+?)(?:,\s*[^,]*?\s*Porn\s*[^,]*?:\s*xHamster[^<]*| - xHamster\.com)</title>'],
  204. webpage, 'title')
  205. formats = []
  206. format_urls = set()
  207. sources = self._parse_json(
  208. self._search_regex(
  209. r'sources\s*:\s*({.+?})\s*,?\s*\n', webpage, 'sources',
  210. default='{}'),
  211. video_id, fatal=False)
  212. for format_id, format_url in sources.items():
  213. format_url = url_or_none(format_url)
  214. if not format_url:
  215. continue
  216. if format_url in format_urls:
  217. continue
  218. format_urls.add(format_url)
  219. formats.append({
  220. 'format_id': format_id,
  221. 'url': format_url,
  222. 'height': get_height(format_id),
  223. })
  224. video_url = self._search_regex(
  225. [r'''file\s*:\s*(?P<q>["'])(?P<mp4>.+?)(?P=q)''',
  226. r'''<a\s+href=(?P<q>["'])(?P<mp4>.+?)(?P=q)\s+class=["']mp4Thumb''',
  227. r'''<video[^>]+file=(?P<q>["'])(?P<mp4>.+?)(?P=q)[^>]*>'''],
  228. webpage, 'video url', group='mp4', default=None)
  229. if video_url and video_url not in format_urls:
  230. formats.append({
  231. 'url': video_url,
  232. })
  233. self._sort_formats(formats)
  234. # Only a few videos have an description
  235. mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
  236. description = mobj.group(1) if mobj else None
  237. upload_date = unified_strdate(self._search_regex(
  238. r'hint=["\'](\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}',
  239. webpage, 'upload date', fatal=False))
  240. uploader = self._html_search_regex(
  241. r'<span[^>]+itemprop=["\']author[^>]+><a[^>]+><span[^>]+>([^<]+)',
  242. webpage, 'uploader', default='anonymous')
  243. thumbnail = self._search_regex(
  244. [r'''["']thumbUrl["']\s*:\s*(?P<q>["'])(?P<thumbnail>.+?)(?P=q)''',
  245. r'''<video[^>]+"poster"=(?P<q>["'])(?P<thumbnail>.+?)(?P=q)[^>]*>'''],
  246. webpage, 'thumbnail', fatal=False, group='thumbnail')
  247. duration = parse_duration(self._search_regex(
  248. [r'<[^<]+\bitemprop=["\']duration["\'][^<]+\bcontent=["\'](.+?)["\']',
  249. r'Runtime:\s*</span>\s*([\d:]+)'], webpage,
  250. 'duration', fatal=False))
  251. view_count = int_or_none(self._search_regex(
  252. r'content=["\']User(?:View|Play)s:(\d+)',
  253. webpage, 'view count', fatal=False))
  254. mobj = re.search(r'hint=[\'"](?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes', webpage)
  255. (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
  256. mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
  257. comment_count = mobj.group('commentcount') if mobj else 0
  258. categories_html = self._search_regex(
  259. r'(?s)<table.+?(<span>Categories:.+?)</table>', webpage,
  260. 'categories', default=None)
  261. categories = [clean_html(category) for category in re.findall(
  262. r'<a[^>]+>(.+?)</a>', categories_html)] if categories_html else None
  263. return {
  264. 'id': video_id,
  265. 'display_id': display_id,
  266. 'title': title,
  267. 'description': description,
  268. 'upload_date': upload_date,
  269. 'uploader': uploader,
  270. 'thumbnail': thumbnail,
  271. 'duration': duration,
  272. 'view_count': view_count,
  273. 'like_count': int_or_none(like_count),
  274. 'dislike_count': int_or_none(dislike_count),
  275. 'comment_count': int_or_none(comment_count),
  276. 'age_limit': age_limit,
  277. 'categories': categories,
  278. 'formats': formats,
  279. }
  280. class XHamsterEmbedIE(InfoExtractor):
  281. _VALID_URL = r'https?://(?:.+?\.)?%s/xembed\.php\?video=(?P<id>\d+)' % XHamsterIE._DOMAINS
  282. _TEST = {
  283. 'url': 'http://xhamster.com/xembed.php?video=3328539',
  284. 'info_dict': {
  285. 'id': '3328539',
  286. 'ext': 'mp4',
  287. 'title': 'Pen Masturbation',
  288. 'timestamp': 1406581861,
  289. 'upload_date': '20140728',
  290. 'uploader': 'ManyakisArt',
  291. 'duration': 5,
  292. 'age_limit': 18,
  293. }
  294. }
  295. @staticmethod
  296. def _extract_urls(webpage):
  297. return [url for _, url in re.findall(
  298. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?xhamster\.com/xembed\.php\?video=\d+)\1',
  299. webpage)]
  300. def _real_extract(self, url):
  301. video_id = self._match_id(url)
  302. webpage = self._download_webpage(url, video_id)
  303. video_url = self._search_regex(
  304. r'href="(https?://xhamster\.com/(?:movies/{0}/[^"]*\.html|videos/[^/]*-{0})[^"]*)"'.format(video_id),
  305. webpage, 'xhamster url', default=None)
  306. if not video_url:
  307. vars = self._parse_json(
  308. self._search_regex(r'vars\s*:\s*({.+?})\s*,\s*\n', webpage, 'vars'),
  309. video_id)
  310. video_url = dict_get(vars, ('downloadLink', 'homepageLink', 'commentsLink', 'shareUrl'))
  311. return self.url_result(video_url, 'XHamster')
  312. class XHamsterUserIE(InfoExtractor):
  313. _VALID_URL = r'https?://(?:.+?\.)?%s/users/(?P<id>[^/?#&]+)' % XHamsterIE._DOMAINS
  314. _TESTS = [{
  315. # Paginated user profile
  316. 'url': 'https://xhamster.com/users/netvideogirls/videos',
  317. 'info_dict': {
  318. 'id': 'netvideogirls',
  319. },
  320. 'playlist_mincount': 267,
  321. }, {
  322. # Non-paginated user profile
  323. 'url': 'https://xhamster.com/users/firatkaan/videos',
  324. 'info_dict': {
  325. 'id': 'firatkaan',
  326. },
  327. 'playlist_mincount': 1,
  328. }]
  329. def _entries(self, user_id):
  330. next_page_url = 'https://xhamster.com/users/%s/videos/1' % user_id
  331. for pagenum in itertools.count(1):
  332. page = self._download_webpage(
  333. next_page_url, user_id, 'Downloading page %s' % pagenum)
  334. for video_tag in re.findall(
  335. r'(<a[^>]+class=["\'].*?\bvideo-thumb__image-container[^>]+>)',
  336. page):
  337. video = extract_attributes(video_tag)
  338. video_url = url_or_none(video.get('href'))
  339. if not video_url or not XHamsterIE.suitable(video_url):
  340. continue
  341. video_id = XHamsterIE._match_id(video_url)
  342. yield self.url_result(
  343. video_url, ie=XHamsterIE.ie_key(), video_id=video_id)
  344. mobj = re.search(r'<a[^>]+data-page=["\']next[^>]+>', page)
  345. if not mobj:
  346. break
  347. next_page = extract_attributes(mobj.group(0))
  348. next_page_url = url_or_none(next_page.get('href'))
  349. if not next_page_url:
  350. break
  351. def _real_extract(self, url):
  352. user_id = self._match_id(url)
  353. return self.playlist_result(self._entries(user_id), user_id)