]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/xhamster.py
[tv5mondeplus] Fix extractor (#739)
[yt-dlp.git] / yt_dlp / extractor / xhamster.py
CommitLineData
35409e11
PH
1from __future__ import unicode_literals
2
df228355 3import itertools
cb10cded
PH
4import re
5
6from .common import InfoExtractor
d852c6bc 7from ..compat import compat_str
cb10cded 8from ..utils import (
85552042 9 clean_html,
fea92aa6 10 determine_ext,
065c4b27 11 dict_get,
df228355 12 extract_attributes,
103f8c8d 13 ExtractorError,
cc2db878 14 float_or_none,
ccb079ee 15 int_or_none,
51378d35 16 parse_duration,
cc2db878 17 str_or_none,
fea92aa6 18 try_get,
44731e30 19 unified_strdate,
3052a30d 20 url_or_none,
cc2db878 21 urljoin,
cb10cded
PH
22)
23
24
25class XHamsterIE(InfoExtractor):
10709fc7 26 _DOMAINS = r'(?:xhamster\.(?:com|one|desi)|xhms\.pro|xhamster\d+\.com)'
00e5c363
S
27 _VALID_URL = r'''(?x)
28 https?://
8945b10f 29 (?:.+?\.)?%s/
00e5c363 30 (?:
6cb30ea5
J
31 movies/(?P<id>[\dA-Za-z]+)/(?P<display_id>[^/]*)\.html|
32 videos/(?P<display_id_2>[^/]*)-(?P<id_2>[\dA-Za-z]+)
00e5c363 33 )
8945b10f 34 ''' % _DOMAINS
6b43132c 35 _TESTS = [{
8945b10f
S
36 'url': 'https://xhamster.com/videos/femaleagent-shy-beauty-takes-the-bait-1509445',
37 'md5': '98b4687efb1ffd331c4197854dc09e8f',
6b43132c
S
38 'info_dict': {
39 'id': '1509445',
8945b10f 40 'display_id': 'femaleagent-shy-beauty-takes-the-bait',
6b43132c
S
41 'ext': 'mp4',
42 'title': 'FemaleAgent Shy beauty takes the bait',
fea92aa6 43 'timestamp': 1350194821,
6b43132c
S
44 'upload_date': '20121014',
45 'uploader': 'Ruseful2011',
51378d35 46 'duration': 893,
6b43132c 47 'age_limit': 18,
ccb079ee 48 },
6b43132c 49 }, {
8945b10f 50 'url': 'https://xhamster.com/videos/britney-spears-sexy-booty-2221348?hd=',
6b43132c
S
51 'info_dict': {
52 'id': '2221348',
8945b10f 53 'display_id': 'britney-spears-sexy-booty',
6b43132c
S
54 'ext': 'mp4',
55 'title': 'Britney Spears Sexy Booty',
fea92aa6 56 'timestamp': 1379123460,
6b43132c
S
57 'upload_date': '20130914',
58 'uploader': 'jojo747400',
51378d35 59 'duration': 200,
6b43132c 60 'age_limit': 18,
5b9aefef 61 },
6b43132c
S
62 'params': {
63 'skip_download': True,
a4690b32 64 },
6b43132c 65 }, {
8945b10f 66 # empty seo, unavailable via new URL schema
6b43132c
S
67 'url': 'http://xhamster.com/movies/5667973/.html',
68 'info_dict': {
69 'id': '5667973',
70 'ext': 'mp4',
71 'title': '....',
fea92aa6 72 'timestamp': 1454948101,
6b43132c
S
73 'upload_date': '20160208',
74 'uploader': 'parejafree',
51378d35 75 'duration': 72,
6b43132c 76 'age_limit': 18,
5b9aefef 77 },
6b43132c
S
78 'params': {
79 'skip_download': True,
80 },
b271e335
W
81 }, {
82 # mobile site
83 'url': 'https://m.xhamster.com/videos/cute-teen-jacqueline-solo-masturbation-8559111',
84 'only_matching': True,
6b43132c
S
85 }, {
86 'url': 'https://xhamster.com/movies/2272726/amber_slayed_by_the_knight.html',
87 'only_matching': True,
103f8c8d
S
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,
00e5c363
S
92 }, {
93 # new URL schema
94 'url': 'https://pt.xhamster.com/videos/euro-pedal-pumping-7937821',
95 'only_matching': True,
b43c5f47
S
96 }, {
97 'url': 'https://xhamster.one/videos/femaleagent-shy-beauty-takes-the-bait-1509445',
98 'only_matching': True,
8945b10f
S
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,
10709fc7
S
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,
8945b10f
S
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,
6cb30ea5
J
117 }, {
118 'url': 'http://de.xhamster.com/videos/skinny-girl-fucks-herself-hard-in-the-forest-xhnBJZx',
119 'only_matching': True,
6b43132c 120 }]
cb10cded 121
5f6a1245 122 def _real_extract(self, url):
cb10cded 123 mobj = re.match(self._VALID_URL, url)
00e5c363
S
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
b271e335 127 desktop_url = re.sub(r'^(https?://(?:.+?\.)?)m\.', r'\1', url)
b274e48d 128 webpage, urlh = self._download_webpage_handle(desktop_url, video_id)
cb10cded 129
103f8c8d
S
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
fea92aa6
S
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(
39e7107d
U
144 (r'window\.initials\s*=\s*({.+?})\s*;\s*</script>',
145 r'window\.initials\s*=\s*({.+?})\s*;'), webpage, 'initials',
fea92aa6
S
146 default='{}'),
147 video_id, fatal=False)
148 if initials:
149 video = initials['videoModel']
150 title = video['title']
151 formats = []
cc2db878 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():
fea92aa6
S
156 if not isinstance(formats_dict, dict):
157 continue
cc2db878 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'))
fea92aa6
S
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
cc2db878 168 format_url = format_item
3052a30d 169 format_url = url_or_none(format_url)
cc2db878 170 if not format_url or format_url in format_urls:
fea92aa6 171 continue
cc2db878 172 format_urls.add(format_url)
fea92aa6
S
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),
cc2db878 178 'filesize': format_sizes.get(quality),
b274e48d
S
179 'http_headers': {
180 'Referer': urlh.geturl(),
181 },
fea92aa6 182 })
cc2db878 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 })
54f37eea 234 self._sort_formats(formats)
fea92aa6
S
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
4395ca2e 271 title = self._html_search_regex(
1a6d9284
S
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')
cb10cded 276
d852c6bc
S
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():
3052a30d
S
286 format_url = url_or_none(format_url)
287 if not format_url:
d852c6bc
S
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,
fea92aa6 295 'height': get_height(format_id),
d852c6bc
S
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
4353cf51 310 # Only a few videos have an description
22ff1c4a 311 mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
ccb079ee 312 description = mobj.group(1) if mobj else None
cb10cded 313
4763b624
S
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))
cb10cded 317
3e485224 318 uploader = self._html_search_regex(
a846173d 319 r'<span[^>]+itemprop=["\']author[^>]+><a[^>]+><span[^>]+>([^<]+)',
3e485224 320 webpage, 'uploader', default='anonymous')
cb10cded 321
251a44b7 322 thumbnail = self._search_regex(
b271e335
W
323 [r'''["']thumbUrl["']\s*:\s*(?P<q>["'])(?P<thumbnail>.+?)(?P=q)''',
324 r'''<video[^>]+"poster"=(?P<q>["'])(?P<thumbnail>.+?)(?P=q)[^>]*>'''],
c73cdd80 325 webpage, 'thumbnail', fatal=False, group='thumbnail')
ccb079ee 326
51378d35 327 duration = parse_duration(self._search_regex(
d852c6bc
S
328 [r'<[^<]+\bitemprop=["\']duration["\'][^<]+\bcontent=["\'](.+?)["\']',
329 r'Runtime:\s*</span>\s*([\d:]+)'], webpage,
51378d35 330 'duration', fatal=False))
ccb079ee 331
6a16fd4a
S
332 view_count = int_or_none(self._search_regex(
333 r'content=["\']User(?:View|Play)s:(\d+)',
334 webpage, 'view count', fatal=False))
ccb079ee 335
a846173d 336 mobj = re.search(r'hint=[\'"](?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes', webpage)
ccb079ee
S
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
cb10cded 341
85552042
S
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
5d0c9754 348 return {
349 'id': video_id,
d852c6bc 350 'display_id': display_id,
ccb079ee
S
351 'title': title,
352 'description': description,
353 'upload_date': upload_date,
3e485224 354 'uploader': uploader,
ccb079ee
S
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),
9d92015d 361 'age_limit': age_limit,
85552042 362 'categories': categories,
ccb079ee 363 'formats': formats,
5d0c9754 364 }
0bbba43e
S
365
366
367class XHamsterEmbedIE(InfoExtractor):
8945b10f 368 _VALID_URL = r'https?://(?:.+?\.)?%s/xembed\.php\?video=(?P<id>\d+)' % XHamsterIE._DOMAINS
0bbba43e
S
369 _TEST = {
370 'url': 'http://xhamster.com/xembed.php?video=3328539',
371 'info_dict': {
372 'id': '3328539',
373 'ext': 'mp4',
374 'title': 'Pen Masturbation',
b271e335 375 'timestamp': 1406581861,
0bbba43e 376 'upload_date': '20140728',
b271e335 377 'uploader': 'ManyakisArt',
0bbba43e
S
378 'duration': 5,
379 'age_limit': 18,
380 }
381 }
382
2bb5b6d0
S
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
0bbba43e
S
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(
db962528 395 r'href="(https?://xhamster\.com/(?:movies/{0}/[^"]*\.html|videos/[^/]*-{0})[^"]*)"'.format(video_id),
065c4b27
S
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'))
0bbba43e 403
25701d5a 404 return self.url_result(video_url, 'XHamster')
df228355
S
405
406
407class 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)