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