]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/xhamster.py
7b67037140653cacdc5740f7c560280335de0a0d
[youtubedl] / youtube_dl / extractor / xhamster.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7 dict_get,
8 ExtractorError,
9 int_or_none,
10 parse_duration,
11 unified_strdate,
12 )
13
14
15 class XHamsterIE(InfoExtractor):
16 _VALID_URL = r'(?P<proto>https?)://(?:.+?\.)?xhamster\.com/movies/(?P<id>[0-9]+)/(?P<seo>.*?)\.html(?:\?.*)?'
17 _TESTS = [{
18 'url': 'http://xhamster.com/movies/1509445/femaleagent_shy_beauty_takes_the_bait.html',
19 'md5': '8281348b8d3c53d39fffb377d24eac4e',
20 'info_dict': {
21 'id': '1509445',
22 'ext': 'mp4',
23 'title': 'FemaleAgent Shy beauty takes the bait',
24 'upload_date': '20121014',
25 'uploader': 'Ruseful2011',
26 'duration': 893,
27 'age_limit': 18,
28 },
29 }, {
30 'url': 'http://xhamster.com/movies/2221348/britney_spears_sexy_booty.html?hd',
31 'info_dict': {
32 'id': '2221348',
33 'ext': 'mp4',
34 'title': 'Britney Spears Sexy Booty',
35 'upload_date': '20130914',
36 'uploader': 'jojo747400',
37 'duration': 200,
38 'age_limit': 18,
39 },
40 'params': {
41 'skip_download': True,
42 },
43 }, {
44 # empty seo
45 'url': 'http://xhamster.com/movies/5667973/.html',
46 'info_dict': {
47 'id': '5667973',
48 'ext': 'mp4',
49 'title': '....',
50 'upload_date': '20160208',
51 'uploader': 'parejafree',
52 'duration': 72,
53 'age_limit': 18,
54 },
55 'params': {
56 'skip_download': True,
57 },
58 }, {
59 'url': 'https://xhamster.com/movies/2272726/amber_slayed_by_the_knight.html',
60 'only_matching': True,
61 }, {
62 # This video is visible for marcoalfa123456's friends only
63 'url': 'https://it.xhamster.com/movies/7263980/la_mia_vicina.html',
64 'only_matching': True,
65 }]
66
67 def _real_extract(self, url):
68 def extract_video_url(webpage, name):
69 return self._search_regex(
70 [r'''file\s*:\s*(?P<q>["'])(?P<mp4>.+?)(?P=q)''',
71 r'''<a\s+href=(?P<q>["'])(?P<mp4>.+?)(?P=q)\s+class=["']mp4Thumb''',
72 r'''<video[^>]+file=(?P<q>["'])(?P<mp4>.+?)(?P=q)[^>]*>'''],
73 webpage, name, group='mp4')
74
75 def is_hd(webpage):
76 return '<div class=\'icon iconHD\'' in webpage
77
78 mobj = re.match(self._VALID_URL, url)
79
80 video_id = mobj.group('id')
81 seo = mobj.group('seo')
82 proto = mobj.group('proto')
83 mrss_url = '%s://xhamster.com/movies/%s/%s.html' % (proto, video_id, seo)
84 webpage = self._download_webpage(mrss_url, video_id)
85
86 error = self._html_search_regex(
87 r'<div[^>]+id=["\']videoClosed["\'][^>]*>(.+?)</div>',
88 webpage, 'error', default=None)
89 if error:
90 raise ExtractorError(error, expected=True)
91
92 title = self._html_search_regex(
93 [r'<h1[^>]*>([^<]+)</h1>',
94 r'<meta[^>]+itemprop=".*?caption.*?"[^>]+content="(.+?)"',
95 r'<title[^>]*>(.+?)(?:,\s*[^,]*?\s*Porn\s*[^,]*?:\s*xHamster[^<]*| - xHamster\.com)</title>'],
96 webpage, 'title')
97
98 # Only a few videos have an description
99 mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
100 description = mobj.group(1) if mobj else None
101
102 upload_date = unified_strdate(self._search_regex(
103 r'hint=["\'](\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}',
104 webpage, 'upload date', fatal=False))
105
106 uploader = self._html_search_regex(
107 r'<span[^>]+itemprop=["\']author[^>]+><a[^>]+href=["\'].+?xhamster\.com/user/[^>]+>(?P<uploader>.+?)</a>',
108 webpage, 'uploader', default='anonymous')
109
110 thumbnail = self._search_regex(
111 [r'''thumb\s*:\s*(?P<q>["'])(?P<thumbnail>.+?)(?P=q)''',
112 r'''<video[^>]+poster=(?P<q>["'])(?P<thumbnail>.+?)(?P=q)[^>]*>'''],
113 webpage, 'thumbnail', fatal=False, group='thumbnail')
114
115 duration = parse_duration(self._search_regex(
116 r'Runtime:\s*</span>\s*([\d:]+)', webpage,
117 'duration', fatal=False))
118
119 view_count = int_or_none(self._search_regex(
120 r'content=["\']User(?:View|Play)s:(\d+)',
121 webpage, 'view count', fatal=False))
122
123 mobj = re.search(r"hint='(?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes'", webpage)
124 (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
125
126 mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
127 comment_count = mobj.group('commentcount') if mobj else 0
128
129 age_limit = self._rta_search(webpage)
130
131 hd = is_hd(webpage)
132
133 format_id = 'hd' if hd else 'sd'
134
135 video_url = extract_video_url(webpage, format_id)
136 formats = [{
137 'url': video_url,
138 'format_id': 'hd' if hd else 'sd',
139 'preference': 1,
140 }]
141
142 if not hd:
143 mrss_url = self._search_regex(r'<link rel="canonical" href="([^"]+)', webpage, 'mrss_url')
144 webpage = self._download_webpage(mrss_url + '?hd', video_id, note='Downloading HD webpage')
145 if is_hd(webpage):
146 video_url = extract_video_url(webpage, 'hd')
147 formats.append({
148 'url': video_url,
149 'format_id': 'hd',
150 'preference': 2,
151 })
152
153 self._sort_formats(formats)
154
155 return {
156 'id': video_id,
157 'title': title,
158 'description': description,
159 'upload_date': upload_date,
160 'uploader': uploader,
161 'thumbnail': thumbnail,
162 'duration': duration,
163 'view_count': view_count,
164 'like_count': int_or_none(like_count),
165 'dislike_count': int_or_none(dislike_count),
166 'comment_count': int_or_none(comment_count),
167 'age_limit': age_limit,
168 'formats': formats,
169 }
170
171
172 class XHamsterEmbedIE(InfoExtractor):
173 _VALID_URL = r'https?://(?:www\.)?xhamster\.com/xembed\.php\?video=(?P<id>\d+)'
174 _TEST = {
175 'url': 'http://xhamster.com/xembed.php?video=3328539',
176 'info_dict': {
177 'id': '3328539',
178 'ext': 'mp4',
179 'title': 'Pen Masturbation',
180 'upload_date': '20140728',
181 'uploader_id': 'anonymous',
182 'duration': 5,
183 'age_limit': 18,
184 }
185 }
186
187 @staticmethod
188 def _extract_urls(webpage):
189 return [url for _, url in re.findall(
190 r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?xhamster\.com/xembed\.php\?video=\d+)\1',
191 webpage)]
192
193 def _real_extract(self, url):
194 video_id = self._match_id(url)
195
196 webpage = self._download_webpage(url, video_id)
197
198 video_url = self._search_regex(
199 r'href="(https?://xhamster\.com/movies/%s/[^"]*\.html[^"]*)"' % video_id,
200 webpage, 'xhamster url', default=None)
201
202 if not video_url:
203 vars = self._parse_json(
204 self._search_regex(r'vars\s*:\s*({.+?})\s*,\s*\n', webpage, 'vars'),
205 video_id)
206 video_url = dict_get(vars, ('downloadLink', 'homepageLink', 'commentsLink', 'shareUrl'))
207
208 return self.url_result(video_url, 'XHamster')