]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/xhamster.py
68a48034ead8036eb28f3815a6588ac4a2253fbb
[youtubedl] / youtube_dl / extractor / xhamster.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import compat_str
7 from ..utils import (
8 clean_html,
9 determine_ext,
10 dict_get,
11 ExtractorError,
12 int_or_none,
13 parse_duration,
14 try_get,
15 unified_strdate,
16 url_or_none,
17 )
18
19
20 class XHamsterIE(InfoExtractor):
21 _VALID_URL = r'''(?x)
22 https?://
23 (?:.+?\.)?xhamster\.com/
24 (?:
25 movies/(?P<id>\d+)/(?P<display_id>[^/]*)\.html|
26 videos/(?P<display_id_2>[^/]*)-(?P<id_2>\d+)
27 )
28 '''
29
30 _TESTS = [{
31 'url': 'http://xhamster.com/movies/1509445/femaleagent_shy_beauty_takes_the_bait.html',
32 'md5': '8281348b8d3c53d39fffb377d24eac4e',
33 'info_dict': {
34 'id': '1509445',
35 'display_id': 'femaleagent_shy_beauty_takes_the_bait',
36 'ext': 'mp4',
37 'title': 'FemaleAgent Shy beauty takes the bait',
38 'timestamp': 1350194821,
39 'upload_date': '20121014',
40 'uploader': 'Ruseful2011',
41 'duration': 893,
42 'age_limit': 18,
43 'categories': ['Fake Hub', 'Amateur', 'MILFs', 'POV', 'Beauti', 'Beauties', 'Beautiful', 'Boss', 'Office', 'Oral', 'Reality', 'Sexy', 'Taking'],
44 },
45 }, {
46 'url': 'http://xhamster.com/movies/2221348/britney_spears_sexy_booty.html?hd',
47 'info_dict': {
48 'id': '2221348',
49 'display_id': 'britney_spears_sexy_booty',
50 'ext': 'mp4',
51 'title': 'Britney Spears Sexy Booty',
52 'timestamp': 1379123460,
53 'upload_date': '20130914',
54 'uploader': 'jojo747400',
55 'duration': 200,
56 'age_limit': 18,
57 'categories': ['Britney Spears', 'Celebrities', 'HD Videos', 'Sexy', 'Sexy Booty'],
58 },
59 'params': {
60 'skip_download': True,
61 },
62 }, {
63 # empty seo
64 'url': 'http://xhamster.com/movies/5667973/.html',
65 'info_dict': {
66 'id': '5667973',
67 'ext': 'mp4',
68 'title': '....',
69 'timestamp': 1454948101,
70 'upload_date': '20160208',
71 'uploader': 'parejafree',
72 'duration': 72,
73 'age_limit': 18,
74 'categories': ['Amateur', 'Blowjobs'],
75 },
76 'params': {
77 'skip_download': True,
78 },
79 }, {
80 # mobile site
81 'url': 'https://m.xhamster.com/videos/cute-teen-jacqueline-solo-masturbation-8559111',
82 'only_matching': True,
83 }, {
84 'url': 'https://xhamster.com/movies/2272726/amber_slayed_by_the_knight.html',
85 'only_matching': True,
86 }, {
87 # This video is visible for marcoalfa123456's friends only
88 'url': 'https://it.xhamster.com/movies/7263980/la_mia_vicina.html',
89 'only_matching': True,
90 }, {
91 # new URL schema
92 'url': 'https://pt.xhamster.com/videos/euro-pedal-pumping-7937821',
93 'only_matching': True,
94 }]
95
96 def _real_extract(self, url):
97 mobj = re.match(self._VALID_URL, url)
98 video_id = mobj.group('id') or mobj.group('id_2')
99 display_id = mobj.group('display_id') or mobj.group('display_id_2')
100
101 desktop_url = re.sub(r'^(https?://(?:.+?\.)?)m\.', r'\1', url)
102 webpage = self._download_webpage(desktop_url, video_id)
103
104 error = self._html_search_regex(
105 r'<div[^>]+id=["\']videoClosed["\'][^>]*>(.+?)</div>',
106 webpage, 'error', default=None)
107 if error:
108 raise ExtractorError(error, expected=True)
109
110 age_limit = self._rta_search(webpage)
111
112 def get_height(s):
113 return int_or_none(self._search_regex(
114 r'^(\d+)[pP]', s, 'height', default=None))
115
116 initials = self._parse_json(
117 self._search_regex(
118 r'window\.initials\s*=\s*({.+?})\s*;\s*\n', webpage, 'initials',
119 default='{}'),
120 video_id, fatal=False)
121 if initials:
122 video = initials['videoModel']
123 title = video['title']
124 formats = []
125 for format_id, formats_dict in video['sources'].items():
126 if not isinstance(formats_dict, dict):
127 continue
128 for quality, format_item in formats_dict.items():
129 if format_id == 'download':
130 # Download link takes some time to be generated,
131 # skipping for now
132 continue
133 if not isinstance(format_item, dict):
134 continue
135 format_url = format_item.get('link')
136 filesize = int_or_none(
137 format_item.get('size'), invscale=1000000)
138 else:
139 format_url = format_item
140 filesize = None
141 format_url = url_or_none(format_url)
142 if not format_url:
143 continue
144 formats.append({
145 'format_id': '%s-%s' % (format_id, quality),
146 'url': format_url,
147 'ext': determine_ext(format_url, 'mp4'),
148 'height': get_height(quality),
149 'filesize': filesize,
150 })
151 self._sort_formats(formats)
152
153 categories_list = video.get('categories')
154 if isinstance(categories_list, list):
155 categories = []
156 for c in categories_list:
157 if not isinstance(c, dict):
158 continue
159 c_name = c.get('name')
160 if isinstance(c_name, compat_str):
161 categories.append(c_name)
162 else:
163 categories = None
164
165 return {
166 'id': video_id,
167 'display_id': display_id,
168 'title': title,
169 'description': video.get('description'),
170 'timestamp': int_or_none(video.get('created')),
171 'uploader': try_get(
172 video, lambda x: x['author']['name'], compat_str),
173 'thumbnail': video.get('thumbURL'),
174 'duration': int_or_none(video.get('duration')),
175 'view_count': int_or_none(video.get('views')),
176 'like_count': int_or_none(try_get(
177 video, lambda x: x['rating']['likes'], int)),
178 'dislike_count': int_or_none(try_get(
179 video, lambda x: x['rating']['dislikes'], int)),
180 'comment_count': int_or_none(video.get('views')),
181 'age_limit': age_limit,
182 'categories': categories,
183 'formats': formats,
184 }
185
186 # Old layout fallback
187
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
194 formats = []
195 format_urls = set()
196
197 sources = self._parse_json(
198 self._search_regex(
199 r'sources\s*:\s*({.+?})\s*,?\s*\n', webpage, 'sources',
200 default='{}'),
201 video_id, fatal=False)
202 for format_id, format_url in sources.items():
203 format_url = url_or_none(format_url)
204 if not format_url:
205 continue
206 if format_url in format_urls:
207 continue
208 format_urls.add(format_url)
209 formats.append({
210 'format_id': format_id,
211 'url': format_url,
212 'height': get_height(format_id),
213 })
214
215 video_url = self._search_regex(
216 [r'''file\s*:\s*(?P<q>["'])(?P<mp4>.+?)(?P=q)''',
217 r'''<a\s+href=(?P<q>["'])(?P<mp4>.+?)(?P=q)\s+class=["']mp4Thumb''',
218 r'''<video[^>]+file=(?P<q>["'])(?P<mp4>.+?)(?P=q)[^>]*>'''],
219 webpage, 'video url', group='mp4', default=None)
220 if video_url and video_url not in format_urls:
221 formats.append({
222 'url': video_url,
223 })
224
225 self._sort_formats(formats)
226
227 # Only a few videos have an description
228 mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
229 description = mobj.group(1) if mobj else None
230
231 upload_date = unified_strdate(self._search_regex(
232 r'hint=["\'](\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}',
233 webpage, 'upload date', fatal=False))
234
235 uploader = self._html_search_regex(
236 r'<span[^>]+itemprop=["\']author[^>]+><a[^>]+><span[^>]+>([^<]+)',
237 webpage, 'uploader', default='anonymous')
238
239 thumbnail = self._search_regex(
240 [r'''["']thumbUrl["']\s*:\s*(?P<q>["'])(?P<thumbnail>.+?)(?P=q)''',
241 r'''<video[^>]+"poster"=(?P<q>["'])(?P<thumbnail>.+?)(?P=q)[^>]*>'''],
242 webpage, 'thumbnail', fatal=False, group='thumbnail')
243
244 duration = parse_duration(self._search_regex(
245 [r'<[^<]+\bitemprop=["\']duration["\'][^<]+\bcontent=["\'](.+?)["\']',
246 r'Runtime:\s*</span>\s*([\d:]+)'], webpage,
247 'duration', fatal=False))
248
249 view_count = int_or_none(self._search_regex(
250 r'content=["\']User(?:View|Play)s:(\d+)',
251 webpage, 'view count', fatal=False))
252
253 mobj = re.search(r'hint=[\'"](?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes', webpage)
254 (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
255
256 mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
257 comment_count = mobj.group('commentcount') if mobj else 0
258
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
265 return {
266 'id': video_id,
267 'display_id': display_id,
268 'title': title,
269 'description': description,
270 'upload_date': upload_date,
271 'uploader': uploader,
272 'thumbnail': thumbnail,
273 'duration': duration,
274 'view_count': view_count,
275 'like_count': int_or_none(like_count),
276 'dislike_count': int_or_none(dislike_count),
277 'comment_count': int_or_none(comment_count),
278 'age_limit': age_limit,
279 'categories': categories,
280 'formats': formats,
281 }
282
283
284 class XHamsterEmbedIE(InfoExtractor):
285 _VALID_URL = r'https?://(?:.+?\.)?xhamster\.com/xembed\.php\?video=(?P<id>\d+)'
286 _TEST = {
287 'url': 'http://xhamster.com/xembed.php?video=3328539',
288 'info_dict': {
289 'id': '3328539',
290 'ext': 'mp4',
291 'title': 'Pen Masturbation',
292 'timestamp': 1406581861,
293 'upload_date': '20140728',
294 'uploader': 'ManyakisArt',
295 'duration': 5,
296 'age_limit': 18,
297 }
298 }
299
300 @staticmethod
301 def _extract_urls(webpage):
302 return [url for _, url in re.findall(
303 r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?xhamster\.com/xembed\.php\?video=\d+)\1',
304 webpage)]
305
306 def _real_extract(self, url):
307 video_id = self._match_id(url)
308
309 webpage = self._download_webpage(url, video_id)
310
311 video_url = self._search_regex(
312 r'href="(https?://xhamster\.com/(?:movies/{0}/[^"]*\.html|videos/[^/]*-{0})[^"]*)"'.format(video_id),
313 webpage, 'xhamster url', default=None)
314
315 if not video_url:
316 vars = self._parse_json(
317 self._search_regex(r'vars\s*:\s*({.+?})\s*,\s*\n', webpage, 'vars'),
318 video_id)
319 video_url = dict_get(vars, ('downloadLink', 'homepageLink', 'commentsLink', 'shareUrl'))
320
321 return self.url_result(video_url, 'XHamster')