xhamster.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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[27]\.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': 'http://xhamster.com/movies/1509445/femaleagent_shy_beauty_takes_the_bait.html',
  100. 'only_matching': True,
  101. }, {
  102. 'url': 'http://xhamster.com/movies/2221348/britney_spears_sexy_booty.html?hd',
  103. 'only_matching': True,
  104. }, {
  105. 'url': 'http://de.xhamster.com/videos/skinny-girl-fucks-herself-hard-in-the-forest-xhnBJZx',
  106. 'only_matching': True,
  107. }]
  108. def _real_extract(self, url):
  109. mobj = re.match(self._VALID_URL, url)
  110. video_id = mobj.group('id') or mobj.group('id_2')
  111. display_id = mobj.group('display_id') or mobj.group('display_id_2')
  112. desktop_url = re.sub(r'^(https?://(?:.+?\.)?)m\.', r'\1', url)
  113. webpage, urlh = self._download_webpage_handle(desktop_url, video_id)
  114. error = self._html_search_regex(
  115. r'<div[^>]+id=["\']videoClosed["\'][^>]*>(.+?)</div>',
  116. webpage, 'error', default=None)
  117. if error:
  118. raise ExtractorError(error, expected=True)
  119. age_limit = self._rta_search(webpage)
  120. def get_height(s):
  121. return int_or_none(self._search_regex(
  122. r'^(\d+)[pP]', s, 'height', default=None))
  123. initials = self._parse_json(
  124. self._search_regex(
  125. r'window\.initials\s*=\s*({.+?})\s*;', webpage, 'initials',
  126. default='{}'),
  127. video_id, fatal=False)
  128. if initials:
  129. video = initials['videoModel']
  130. title = video['title']
  131. formats = []
  132. for format_id, formats_dict in video['sources'].items():
  133. if not isinstance(formats_dict, dict):
  134. continue
  135. for quality, format_item in formats_dict.items():
  136. if format_id == 'download':
  137. # Download link takes some time to be generated,
  138. # skipping for now
  139. continue
  140. if not isinstance(format_item, dict):
  141. continue
  142. format_url = format_item.get('link')
  143. filesize = int_or_none(
  144. format_item.get('size'), invscale=1000000)
  145. else:
  146. format_url = format_item
  147. filesize = None
  148. format_url = url_or_none(format_url)
  149. if not format_url:
  150. continue
  151. formats.append({
  152. 'format_id': '%s-%s' % (format_id, quality),
  153. 'url': format_url,
  154. 'ext': determine_ext(format_url, 'mp4'),
  155. 'height': get_height(quality),
  156. 'filesize': filesize,
  157. 'http_headers': {
  158. 'Referer': urlh.geturl(),
  159. },
  160. })
  161. self._sort_formats(formats)
  162. categories_list = video.get('categories')
  163. if isinstance(categories_list, list):
  164. categories = []
  165. for c in categories_list:
  166. if not isinstance(c, dict):
  167. continue
  168. c_name = c.get('name')
  169. if isinstance(c_name, compat_str):
  170. categories.append(c_name)
  171. else:
  172. categories = None
  173. return {
  174. 'id': video_id,
  175. 'display_id': display_id,
  176. 'title': title,
  177. 'description': video.get('description'),
  178. 'timestamp': int_or_none(video.get('created')),
  179. 'uploader': try_get(
  180. video, lambda x: x['author']['name'], compat_str),
  181. 'thumbnail': video.get('thumbURL'),
  182. 'duration': int_or_none(video.get('duration')),
  183. 'view_count': int_or_none(video.get('views')),
  184. 'like_count': int_or_none(try_get(
  185. video, lambda x: x['rating']['likes'], int)),
  186. 'dislike_count': int_or_none(try_get(
  187. video, lambda x: x['rating']['dislikes'], int)),
  188. 'comment_count': int_or_none(video.get('views')),
  189. 'age_limit': age_limit,
  190. 'categories': categories,
  191. 'formats': formats,
  192. }
  193. # Old layout fallback
  194. title = self._html_search_regex(
  195. [r'<h1[^>]*>([^<]+)</h1>',
  196. r'<meta[^>]+itemprop=".*?caption.*?"[^>]+content="(.+?)"',
  197. r'<title[^>]*>(.+?)(?:,\s*[^,]*?\s*Porn\s*[^,]*?:\s*xHamster[^<]*| - xHamster\.com)</title>'],
  198. webpage, 'title')
  199. formats = []
  200. format_urls = set()
  201. sources = self._parse_json(
  202. self._search_regex(
  203. r'sources\s*:\s*({.+?})\s*,?\s*\n', webpage, 'sources',
  204. default='{}'),
  205. video_id, fatal=False)
  206. for format_id, format_url in sources.items():
  207. format_url = url_or_none(format_url)
  208. if not format_url:
  209. continue
  210. if format_url in format_urls:
  211. continue
  212. format_urls.add(format_url)
  213. formats.append({
  214. 'format_id': format_id,
  215. 'url': format_url,
  216. 'height': get_height(format_id),
  217. })
  218. video_url = self._search_regex(
  219. [r'''file\s*:\s*(?P<q>["'])(?P<mp4>.+?)(?P=q)''',
  220. r'''<a\s+href=(?P<q>["'])(?P<mp4>.+?)(?P=q)\s+class=["']mp4Thumb''',
  221. r'''<video[^>]+file=(?P<q>["'])(?P<mp4>.+?)(?P=q)[^>]*>'''],
  222. webpage, 'video url', group='mp4', default=None)
  223. if video_url and video_url not in format_urls:
  224. formats.append({
  225. 'url': video_url,
  226. })
  227. self._sort_formats(formats)
  228. # Only a few videos have an description
  229. mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
  230. description = mobj.group(1) if mobj else None
  231. upload_date = unified_strdate(self._search_regex(
  232. r'hint=["\'](\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}',
  233. webpage, 'upload date', fatal=False))
  234. uploader = self._html_search_regex(
  235. r'<span[^>]+itemprop=["\']author[^>]+><a[^>]+><span[^>]+>([^<]+)',
  236. webpage, 'uploader', default='anonymous')
  237. thumbnail = self._search_regex(
  238. [r'''["']thumbUrl["']\s*:\s*(?P<q>["'])(?P<thumbnail>.+?)(?P=q)''',
  239. r'''<video[^>]+"poster"=(?P<q>["'])(?P<thumbnail>.+?)(?P=q)[^>]*>'''],
  240. webpage, 'thumbnail', fatal=False, group='thumbnail')
  241. duration = parse_duration(self._search_regex(
  242. [r'<[^<]+\bitemprop=["\']duration["\'][^<]+\bcontent=["\'](.+?)["\']',
  243. r'Runtime:\s*</span>\s*([\d:]+)'], webpage,
  244. 'duration', fatal=False))
  245. view_count = int_or_none(self._search_regex(
  246. r'content=["\']User(?:View|Play)s:(\d+)',
  247. webpage, 'view count', fatal=False))
  248. mobj = re.search(r'hint=[\'"](?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes', webpage)
  249. (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
  250. mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
  251. comment_count = mobj.group('commentcount') if mobj else 0
  252. categories_html = self._search_regex(
  253. r'(?s)<table.+?(<span>Categories:.+?)</table>', webpage,
  254. 'categories', default=None)
  255. categories = [clean_html(category) for category in re.findall(
  256. r'<a[^>]+>(.+?)</a>', categories_html)] if categories_html else None
  257. return {
  258. 'id': video_id,
  259. 'display_id': display_id,
  260. 'title': title,
  261. 'description': description,
  262. 'upload_date': upload_date,
  263. 'uploader': uploader,
  264. 'thumbnail': thumbnail,
  265. 'duration': duration,
  266. 'view_count': view_count,
  267. 'like_count': int_or_none(like_count),
  268. 'dislike_count': int_or_none(dislike_count),
  269. 'comment_count': int_or_none(comment_count),
  270. 'age_limit': age_limit,
  271. 'categories': categories,
  272. 'formats': formats,
  273. }
  274. class XHamsterEmbedIE(InfoExtractor):
  275. _VALID_URL = r'https?://(?:.+?\.)?%s/xembed\.php\?video=(?P<id>\d+)' % XHamsterIE._DOMAINS
  276. _TEST = {
  277. 'url': 'http://xhamster.com/xembed.php?video=3328539',
  278. 'info_dict': {
  279. 'id': '3328539',
  280. 'ext': 'mp4',
  281. 'title': 'Pen Masturbation',
  282. 'timestamp': 1406581861,
  283. 'upload_date': '20140728',
  284. 'uploader': 'ManyakisArt',
  285. 'duration': 5,
  286. 'age_limit': 18,
  287. }
  288. }
  289. @staticmethod
  290. def _extract_urls(webpage):
  291. return [url for _, url in re.findall(
  292. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?xhamster\.com/xembed\.php\?video=\d+)\1',
  293. webpage)]
  294. def _real_extract(self, url):
  295. video_id = self._match_id(url)
  296. webpage = self._download_webpage(url, video_id)
  297. video_url = self._search_regex(
  298. r'href="(https?://xhamster\.com/(?:movies/{0}/[^"]*\.html|videos/[^/]*-{0})[^"]*)"'.format(video_id),
  299. webpage, 'xhamster url', default=None)
  300. if not video_url:
  301. vars = self._parse_json(
  302. self._search_regex(r'vars\s*:\s*({.+?})\s*,\s*\n', webpage, 'vars'),
  303. video_id)
  304. video_url = dict_get(vars, ('downloadLink', 'homepageLink', 'commentsLink', 'shareUrl'))
  305. return self.url_result(video_url, 'XHamster')
  306. class XHamsterUserIE(InfoExtractor):
  307. _VALID_URL = r'https?://(?:.+?\.)?%s/users/(?P<id>[^/?#&]+)' % XHamsterIE._DOMAINS
  308. _TESTS = [{
  309. # Paginated user profile
  310. 'url': 'https://xhamster.com/users/netvideogirls/videos',
  311. 'info_dict': {
  312. 'id': 'netvideogirls',
  313. },
  314. 'playlist_mincount': 267,
  315. }, {
  316. # Non-paginated user profile
  317. 'url': 'https://xhamster.com/users/firatkaan/videos',
  318. 'info_dict': {
  319. 'id': 'firatkaan',
  320. },
  321. 'playlist_mincount': 1,
  322. }]
  323. def _entries(self, user_id):
  324. next_page_url = 'https://xhamster.com/users/%s/videos/1' % user_id
  325. for pagenum in itertools.count(1):
  326. page = self._download_webpage(
  327. next_page_url, user_id, 'Downloading page %s' % pagenum)
  328. for video_tag in re.findall(
  329. r'(<a[^>]+class=["\'].*?\bvideo-thumb__image-container[^>]+>)',
  330. page):
  331. video = extract_attributes(video_tag)
  332. video_url = url_or_none(video.get('href'))
  333. if not video_url or not XHamsterIE.suitable(video_url):
  334. continue
  335. video_id = XHamsterIE._match_id(video_url)
  336. yield self.url_result(
  337. video_url, ie=XHamsterIE.ie_key(), video_id=video_id)
  338. mobj = re.search(r'<a[^>]+data-page=["\']next[^>]+>', page)
  339. if not mobj:
  340. break
  341. next_page = extract_attributes(mobj.group(0))
  342. next_page_url = url_or_none(next_page.get('href'))
  343. if not next_page_url:
  344. break
  345. def _real_extract(self, url):
  346. user_id = self._match_id(url)
  347. return self.playlist_result(self._entries(user_id), user_id)