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