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