]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/xhamster.py
Merge remote-tracking branch 'yan12125/download-dash-segments' (#5886)
[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_id': '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_id': '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):
50             mp4 = re.search(r'<video\s+.*?file="([^"]+)".*?>', webpage)
51             if mp4 is None:
52                 raise ExtractorError('Unable to extract media URL')
53             else:
54                 return mp4.group(1)
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_id = self._html_search_regex(r'<a href=\'/user/[^>]+>(?P<uploader_id>[^<]+)',
79                                               webpage, 'uploader id', default='anonymous')
80
81         thumbnail = self._html_search_regex(r'<video\s+.*?poster="([^"]+)".*?>', webpage, 'thumbnail', fatal=False)
82
83         duration = parse_duration(self._html_search_regex(r'<span>Runtime:</span> (\d+:\d+)</div>',
84                                                           webpage, 'duration', fatal=False))
85
86         view_count = self._html_search_regex(r'<span>Views:</span> ([^<]+)</div>', webpage, 'view count', fatal=False)
87         if view_count:
88             view_count = str_to_int(view_count)
89
90         mobj = re.search(r"hint='(?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes'", webpage)
91         (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
92
93         mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
94         comment_count = mobj.group('commentcount') if mobj else 0
95
96         age_limit = self._rta_search(webpage)
97
98         hd = is_hd(webpage)
99
100         video_url = extract_video_url(webpage)
101         formats = [{
102             'url': video_url,
103             'format_id': 'hd' if hd else 'sd',
104             'preference': 1,
105         }]
106
107         if not hd:
108             mrss_url = self._search_regex(r'<link rel="canonical" href="([^"]+)', webpage, 'mrss_url')
109             webpage = self._download_webpage(mrss_url + '?hd', video_id, note='Downloading HD webpage')
110             if is_hd(webpage):
111                 video_url = extract_video_url(webpage)
112                 formats.append({
113                     'url': video_url,
114                     'format_id': 'hd',
115                     'preference': 2,
116                 })
117
118         self._sort_formats(formats)
119
120         return {
121             'id': video_id,
122             'title': title,
123             'description': description,
124             'upload_date': upload_date,
125             'uploader_id': uploader_id,
126             'thumbnail': thumbnail,
127             'duration': duration,
128             'view_count': view_count,
129             'like_count': int_or_none(like_count),
130             'dislike_count': int_or_none(dislike_count),
131             'comment_count': int_or_none(comment_count),
132             'age_limit': age_limit,
133             'formats': formats,
134         }
135
136
137 class XHamsterEmbedIE(InfoExtractor):
138     _VALID_URL = r'https?://(?:www\.)?xhamster\.com/xembed\.php\?video=(?P<id>\d+)'
139     _TEST = {
140         'url': 'http://xhamster.com/xembed.php?video=3328539',
141         'info_dict': {
142             'id': '3328539',
143             'ext': 'mp4',
144             'title': 'Pen Masturbation',
145             'upload_date': '20140728',
146             'uploader_id': 'anonymous',
147             'duration': 5,
148             'age_limit': 18,
149         }
150     }
151
152     @staticmethod
153     def _extract_urls(webpage):
154         return [url for _, url in re.findall(
155             r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?xhamster\.com/xembed\.php\?video=\d+)\1',
156             webpage)]
157
158     def _real_extract(self, url):
159         video_id = self._match_id(url)
160
161         webpage = self._download_webpage(url, video_id)
162
163         video_url = self._search_regex(
164             r'href="(https?://xhamster\.com/movies/%s/[^"]+\.html[^"]*)"' % video_id,
165             webpage, 'xhamster url')
166
167         return self.url_result(video_url, 'XHamster')