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