Merge branch 'akamai_pv' of https://github.com/remitamine/youtube-dl into remitamine...
[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': '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(
67             [r'<h1[^>]*>([^<]+)</h1>',
68              r'<meta[^>]+itemprop=".*?caption.*?"[^>]+content="(.+?)"',
69              r'<title[^>]*>(.+?)(?:,\s*[^,]*?\s*Porn\s*[^,]*?:\s*xHamster[^<]*| - xHamster\.com)</title>'],
70             webpage, 'title')
71
72         # Only a few videos have an description
73         mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
74         description = mobj.group(1) if mobj else None
75
76         upload_date = unified_strdate(self._search_regex(
77             r'hint=["\'](\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}',
78             webpage, 'upload date', fatal=False))
79
80         uploader = self._html_search_regex(
81             r'<span[^>]+itemprop=["\']author[^>]+><a[^>]+href=["\'].+?xhamster\.com/user/[^>]+>(?P<uploader>.+?)</a>',
82             webpage, 'uploader', default='anonymous')
83
84         thumbnail = self._search_regex(
85             [r'''thumb\s*:\s*(?P<q>["'])(?P<thumbnail>.+?)(?P=q)''',
86              r'''<video[^>]+poster=(?P<q>["'])(?P<thumbnail>.+?)(?P=q)[^>]*>'''],
87             webpage, 'thumbnail', fatal=False, group='thumbnail')
88
89         duration = float_or_none(self._search_regex(
90             r'(["\'])duration\1\s*:\s*(["\'])(?P<duration>.+?)\2',
91             webpage, 'duration', fatal=False, group='duration'))
92
93         view_count = int_or_none(self._search_regex(
94             r'content=["\']User(?:View|Play)s:(\d+)',
95             webpage, 'view count', fatal=False))
96
97         mobj = re.search(r"hint='(?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes'", webpage)
98         (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
99
100         mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
101         comment_count = mobj.group('commentcount') if mobj else 0
102
103         age_limit = self._rta_search(webpage)
104
105         hd = is_hd(webpage)
106
107         format_id = 'hd' if hd else 'sd'
108
109         video_url = extract_video_url(webpage, format_id)
110         formats = [{
111             'url': video_url,
112             'format_id': 'hd' if hd else 'sd',
113             'preference': 1,
114         }]
115
116         if not hd:
117             mrss_url = self._search_regex(r'<link rel="canonical" href="([^"]+)', webpage, 'mrss_url')
118             webpage = self._download_webpage(mrss_url + '?hd', video_id, note='Downloading HD webpage')
119             if is_hd(webpage):
120                 video_url = extract_video_url(webpage, 'hd')
121                 formats.append({
122                     'url': video_url,
123                     'format_id': 'hd',
124                     'preference': 2,
125                 })
126
127         self._sort_formats(formats)
128
129         return {
130             'id': video_id,
131             'title': title,
132             'description': description,
133             'upload_date': upload_date,
134             'uploader': uploader,
135             'thumbnail': thumbnail,
136             'duration': duration,
137             'view_count': view_count,
138             'like_count': int_or_none(like_count),
139             'dislike_count': int_or_none(dislike_count),
140             'comment_count': int_or_none(comment_count),
141             'age_limit': age_limit,
142             'formats': formats,
143         }
144
145
146 class XHamsterEmbedIE(InfoExtractor):
147     _VALID_URL = r'https?://(?:www\.)?xhamster\.com/xembed\.php\?video=(?P<id>\d+)'
148     _TEST = {
149         'url': 'http://xhamster.com/xembed.php?video=3328539',
150         'info_dict': {
151             'id': '3328539',
152             'ext': 'mp4',
153             'title': 'Pen Masturbation',
154             'upload_date': '20140728',
155             'uploader_id': 'anonymous',
156             'duration': 5,
157             'age_limit': 18,
158         }
159     }
160
161     @staticmethod
162     def _extract_urls(webpage):
163         return [url for _, url in re.findall(
164             r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?xhamster\.com/xembed\.php\?video=\d+)\1',
165             webpage)]
166
167     def _real_extract(self, url):
168         video_id = self._match_id(url)
169
170         webpage = self._download_webpage(url, video_id)
171
172         video_url = self._search_regex(
173             r'href="(https?://xhamster\.com/movies/%s/[^"]+\.html[^"]*)"' % video_id,
174             webpage, 'xhamster url', default=None)
175
176         if not video_url:
177             vars = self._parse_json(
178                 self._search_regex(r'vars\s*:\s*({.+?})\s*,\s*\n', webpage, 'vars'),
179                 video_id)
180             video_url = dict_get(vars, ('downloadLink', 'homepageLink', 'commentsLink', 'shareUrl'))
181
182         return self.url_result(video_url, 'XHamster')