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