xhamster.py 15 KB

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