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