xhamster.py 14 KB

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