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