]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/xhamster.py
[ard.py] add playlist support
[yt-dlp.git] / youtube_dl / 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,
ccb079ee 14 int_or_none,
51378d35 15 parse_duration,
fea92aa6 16 try_get,
44731e30 17 unified_strdate,
3052a30d 18 url_or_none,
cb10cded
PH
19)
20
21
22class XHamsterIE(InfoExtractor):
8945b10f 23 _DOMAINS = r'(?:xhamster\.(?:com|one|desi)|xhms\.pro|xhamster[27]\.com)'
00e5c363
S
24 _VALID_URL = r'''(?x)
25 https?://
8945b10f 26 (?:.+?\.)?%s/
00e5c363
S
27 (?:
28 movies/(?P<id>\d+)/(?P<display_id>[^/]*)\.html|
29 videos/(?P<display_id_2>[^/]*)-(?P<id_2>\d+)
30 )
8945b10f 31 ''' % _DOMAINS
6b43132c 32 _TESTS = [{
8945b10f
S
33 'url': 'https://xhamster.com/videos/femaleagent-shy-beauty-takes-the-bait-1509445',
34 'md5': '98b4687efb1ffd331c4197854dc09e8f',
6b43132c
S
35 'info_dict': {
36 'id': '1509445',
8945b10f 37 'display_id': 'femaleagent-shy-beauty-takes-the-bait',
6b43132c
S
38 'ext': 'mp4',
39 'title': 'FemaleAgent Shy beauty takes the bait',
fea92aa6 40 'timestamp': 1350194821,
6b43132c
S
41 'upload_date': '20121014',
42 'uploader': 'Ruseful2011',
51378d35 43 'duration': 893,
6b43132c 44 'age_limit': 18,
ccb079ee 45 },
6b43132c 46 }, {
8945b10f 47 'url': 'https://xhamster.com/videos/britney-spears-sexy-booty-2221348?hd=',
6b43132c
S
48 'info_dict': {
49 'id': '2221348',
8945b10f 50 'display_id': 'britney-spears-sexy-booty',
6b43132c
S
51 'ext': 'mp4',
52 'title': 'Britney Spears Sexy Booty',
fea92aa6 53 'timestamp': 1379123460,
6b43132c
S
54 'upload_date': '20130914',
55 'uploader': 'jojo747400',
51378d35 56 'duration': 200,
6b43132c 57 'age_limit': 18,
5b9aefef 58 },
6b43132c
S
59 'params': {
60 'skip_download': True,
a4690b32 61 },
6b43132c 62 }, {
8945b10f 63 # empty seo, unavailable via new URL schema
6b43132c
S
64 'url': 'http://xhamster.com/movies/5667973/.html',
65 'info_dict': {
66 'id': '5667973',
67 'ext': 'mp4',
68 'title': '....',
fea92aa6 69 'timestamp': 1454948101,
6b43132c
S
70 'upload_date': '20160208',
71 'uploader': 'parejafree',
51378d35 72 'duration': 72,
6b43132c 73 'age_limit': 18,
5b9aefef 74 },
6b43132c
S
75 'params': {
76 'skip_download': True,
77 },
b271e335
W
78 }, {
79 # mobile site
80 'url': 'https://m.xhamster.com/videos/cute-teen-jacqueline-solo-masturbation-8559111',
81 'only_matching': True,
6b43132c
S
82 }, {
83 'url': 'https://xhamster.com/movies/2272726/amber_slayed_by_the_knight.html',
84 'only_matching': True,
103f8c8d
S
85 }, {
86 # This video is visible for marcoalfa123456's friends only
87 'url': 'https://it.xhamster.com/movies/7263980/la_mia_vicina.html',
88 'only_matching': True,
00e5c363
S
89 }, {
90 # new URL schema
91 'url': 'https://pt.xhamster.com/videos/euro-pedal-pumping-7937821',
92 'only_matching': True,
b43c5f47
S
93 }, {
94 'url': 'https://xhamster.one/videos/femaleagent-shy-beauty-takes-the-bait-1509445',
95 'only_matching': True,
8945b10f
S
96 }, {
97 'url': 'https://xhamster.desi/videos/femaleagent-shy-beauty-takes-the-bait-1509445',
98 'only_matching': True,
99 }, {
100 'url': 'https://xhamster2.com/videos/femaleagent-shy-beauty-takes-the-bait-1509445',
101 'only_matching': True,
102 }, {
103 'url': 'http://xhamster.com/movies/1509445/femaleagent_shy_beauty_takes_the_bait.html',
104 'only_matching': True,
105 }, {
106 'url': 'http://xhamster.com/movies/2221348/britney_spears_sexy_booty.html?hd',
107 'only_matching': True,
6b43132c 108 }]
cb10cded 109
5f6a1245 110 def _real_extract(self, url):
cb10cded 111 mobj = re.match(self._VALID_URL, url)
00e5c363
S
112 video_id = mobj.group('id') or mobj.group('id_2')
113 display_id = mobj.group('display_id') or mobj.group('display_id_2')
114
b271e335 115 desktop_url = re.sub(r'^(https?://(?:.+?\.)?)m\.', r'\1', url)
b274e48d 116 webpage, urlh = self._download_webpage_handle(desktop_url, video_id)
cb10cded 117
103f8c8d
S
118 error = self._html_search_regex(
119 r'<div[^>]+id=["\']videoClosed["\'][^>]*>(.+?)</div>',
120 webpage, 'error', default=None)
121 if error:
122 raise ExtractorError(error, expected=True)
123
fea92aa6
S
124 age_limit = self._rta_search(webpage)
125
126 def get_height(s):
127 return int_or_none(self._search_regex(
128 r'^(\d+)[pP]', s, 'height', default=None))
129
130 initials = self._parse_json(
131 self._search_regex(
132 r'window\.initials\s*=\s*({.+?})\s*;\s*\n', 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
3052a30d
S
155 format_url = url_or_none(format_url)
156 if not format_url:
fea92aa6
S
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,
b274e48d
S
164 'http_headers': {
165 'Referer': urlh.geturl(),
166 },
fea92aa6
S
167 })
168 self._sort_formats(formats)
169
170 categories_list = video.get('categories')
171 if isinstance(categories_list, list):
172 categories = []
173 for c in categories_list:
174 if not isinstance(c, dict):
175 continue
176 c_name = c.get('name')
177 if isinstance(c_name, compat_str):
178 categories.append(c_name)
179 else:
180 categories = None
181
182 return {
183 'id': video_id,
184 'display_id': display_id,
185 'title': title,
186 'description': video.get('description'),
187 'timestamp': int_or_none(video.get('created')),
188 'uploader': try_get(
189 video, lambda x: x['author']['name'], compat_str),
190 'thumbnail': video.get('thumbURL'),
191 'duration': int_or_none(video.get('duration')),
192 'view_count': int_or_none(video.get('views')),
193 'like_count': int_or_none(try_get(
194 video, lambda x: x['rating']['likes'], int)),
195 'dislike_count': int_or_none(try_get(
196 video, lambda x: x['rating']['dislikes'], int)),
197 'comment_count': int_or_none(video.get('views')),
198 'age_limit': age_limit,
199 'categories': categories,
200 'formats': formats,
201 }
202
203 # Old layout fallback
204
4395ca2e 205 title = self._html_search_regex(
1a6d9284
S
206 [r'<h1[^>]*>([^<]+)</h1>',
207 r'<meta[^>]+itemprop=".*?caption.*?"[^>]+content="(.+?)"',
208 r'<title[^>]*>(.+?)(?:,\s*[^,]*?\s*Porn\s*[^,]*?:\s*xHamster[^<]*| - xHamster\.com)</title>'],
209 webpage, 'title')
cb10cded 210
d852c6bc
S
211 formats = []
212 format_urls = set()
213
214 sources = self._parse_json(
215 self._search_regex(
216 r'sources\s*:\s*({.+?})\s*,?\s*\n', webpage, 'sources',
217 default='{}'),
218 video_id, fatal=False)
219 for format_id, format_url in sources.items():
3052a30d
S
220 format_url = url_or_none(format_url)
221 if not format_url:
d852c6bc
S
222 continue
223 if format_url in format_urls:
224 continue
225 format_urls.add(format_url)
226 formats.append({
227 'format_id': format_id,
228 'url': format_url,
fea92aa6 229 'height': get_height(format_id),
d852c6bc
S
230 })
231
232 video_url = self._search_regex(
233 [r'''file\s*:\s*(?P<q>["'])(?P<mp4>.+?)(?P=q)''',
234 r'''<a\s+href=(?P<q>["'])(?P<mp4>.+?)(?P=q)\s+class=["']mp4Thumb''',
235 r'''<video[^>]+file=(?P<q>["'])(?P<mp4>.+?)(?P=q)[^>]*>'''],
236 webpage, 'video url', group='mp4', default=None)
237 if video_url and video_url not in format_urls:
238 formats.append({
239 'url': video_url,
240 })
241
242 self._sort_formats(formats)
243
4353cf51 244 # Only a few videos have an description
22ff1c4a 245 mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
ccb079ee 246 description = mobj.group(1) if mobj else None
cb10cded 247
4763b624
S
248 upload_date = unified_strdate(self._search_regex(
249 r'hint=["\'](\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}',
250 webpage, 'upload date', fatal=False))
cb10cded 251
3e485224 252 uploader = self._html_search_regex(
a846173d 253 r'<span[^>]+itemprop=["\']author[^>]+><a[^>]+><span[^>]+>([^<]+)',
3e485224 254 webpage, 'uploader', default='anonymous')
cb10cded 255
251a44b7 256 thumbnail = self._search_regex(
b271e335
W
257 [r'''["']thumbUrl["']\s*:\s*(?P<q>["'])(?P<thumbnail>.+?)(?P=q)''',
258 r'''<video[^>]+"poster"=(?P<q>["'])(?P<thumbnail>.+?)(?P=q)[^>]*>'''],
c73cdd80 259 webpage, 'thumbnail', fatal=False, group='thumbnail')
ccb079ee 260
51378d35 261 duration = parse_duration(self._search_regex(
d852c6bc
S
262 [r'<[^<]+\bitemprop=["\']duration["\'][^<]+\bcontent=["\'](.+?)["\']',
263 r'Runtime:\s*</span>\s*([\d:]+)'], webpage,
51378d35 264 'duration', fatal=False))
ccb079ee 265
6a16fd4a
S
266 view_count = int_or_none(self._search_regex(
267 r'content=["\']User(?:View|Play)s:(\d+)',
268 webpage, 'view count', fatal=False))
ccb079ee 269
a846173d 270 mobj = re.search(r'hint=[\'"](?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes', webpage)
ccb079ee
S
271 (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
272
273 mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
274 comment_count = mobj.group('commentcount') if mobj else 0
cb10cded 275
85552042
S
276 categories_html = self._search_regex(
277 r'(?s)<table.+?(<span>Categories:.+?)</table>', webpage,
278 'categories', default=None)
279 categories = [clean_html(category) for category in re.findall(
280 r'<a[^>]+>(.+?)</a>', categories_html)] if categories_html else None
281
5d0c9754 282 return {
283 'id': video_id,
d852c6bc 284 'display_id': display_id,
ccb079ee
S
285 'title': title,
286 'description': description,
287 'upload_date': upload_date,
3e485224 288 'uploader': uploader,
ccb079ee
S
289 'thumbnail': thumbnail,
290 'duration': duration,
291 'view_count': view_count,
292 'like_count': int_or_none(like_count),
293 'dislike_count': int_or_none(dislike_count),
294 'comment_count': int_or_none(comment_count),
9d92015d 295 'age_limit': age_limit,
85552042 296 'categories': categories,
ccb079ee 297 'formats': formats,
5d0c9754 298 }
0bbba43e
S
299
300
301class XHamsterEmbedIE(InfoExtractor):
8945b10f 302 _VALID_URL = r'https?://(?:.+?\.)?%s/xembed\.php\?video=(?P<id>\d+)' % XHamsterIE._DOMAINS
0bbba43e
S
303 _TEST = {
304 'url': 'http://xhamster.com/xembed.php?video=3328539',
305 'info_dict': {
306 'id': '3328539',
307 'ext': 'mp4',
308 'title': 'Pen Masturbation',
b271e335 309 'timestamp': 1406581861,
0bbba43e 310 'upload_date': '20140728',
b271e335 311 'uploader': 'ManyakisArt',
0bbba43e
S
312 'duration': 5,
313 'age_limit': 18,
314 }
315 }
316
2bb5b6d0
S
317 @staticmethod
318 def _extract_urls(webpage):
319 return [url for _, url in re.findall(
320 r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?xhamster\.com/xembed\.php\?video=\d+)\1',
321 webpage)]
322
0bbba43e
S
323 def _real_extract(self, url):
324 video_id = self._match_id(url)
325
326 webpage = self._download_webpage(url, video_id)
327
328 video_url = self._search_regex(
db962528 329 r'href="(https?://xhamster\.com/(?:movies/{0}/[^"]*\.html|videos/[^/]*-{0})[^"]*)"'.format(video_id),
065c4b27
S
330 webpage, 'xhamster url', default=None)
331
332 if not video_url:
333 vars = self._parse_json(
334 self._search_regex(r'vars\s*:\s*({.+?})\s*,\s*\n', webpage, 'vars'),
335 video_id)
336 video_url = dict_get(vars, ('downloadLink', 'homepageLink', 'commentsLink', 'shareUrl'))
0bbba43e 337
25701d5a 338 return self.url_result(video_url, 'XHamster')
df228355
S
339
340
341class XHamsterUserIE(InfoExtractor):
342 _VALID_URL = r'https?://(?:.+?\.)?%s/users/(?P<id>[^/?#&]+)' % XHamsterIE._DOMAINS
343 _TESTS = [{
344 # Paginated user profile
345 'url': 'https://xhamster.com/users/netvideogirls/videos',
346 'info_dict': {
347 'id': 'netvideogirls',
348 },
349 'playlist_mincount': 267,
350 }, {
351 # Non-paginated user profile
352 'url': 'https://xhamster.com/users/firatkaan/videos',
353 'info_dict': {
354 'id': 'firatkaan',
355 },
356 'playlist_mincount': 1,
357 }]
358
359 def _entries(self, user_id):
360 next_page_url = 'https://xhamster.com/users/%s/videos/1' % user_id
361 for pagenum in itertools.count(1):
362 page = self._download_webpage(
363 next_page_url, user_id, 'Downloading page %s' % pagenum)
364 for video_tag in re.findall(
365 r'(<a[^>]+class=["\'].*?\bvideo-thumb__image-container[^>]+>)',
366 page):
367 video = extract_attributes(video_tag)
368 video_url = url_or_none(video.get('href'))
369 if not video_url or not XHamsterIE.suitable(video_url):
370 continue
371 video_id = XHamsterIE._match_id(video_url)
372 yield self.url_result(
373 video_url, ie=XHamsterIE.ie_key(), video_id=video_id)
374 mobj = re.search(r'<a[^>]+data-page=["\']next[^>]+>', page)
375 if not mobj:
376 break
377 next_page = extract_attributes(mobj.group(0))
378 next_page_url = url_or_none(next_page.get('href'))
379 if not next_page_url:
380 break
381
382 def _real_extract(self, url):
383 user_id = self._match_id(url)
384 return self.playlist_result(self._entries(user_id), user_id)