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