Merge pull request #4615 from dwemthy/https_xhamster
[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     """Information Extractor for xHamster"""
17     _VALID_URL = r'(?P<proto>https?)://(?:.+?\.)?xhamster\.com/movies/(?P<id>[0-9]+)/(?P<seo>.+?)\.html(?:\?.*)?'
18     _TESTS = [
19         {
20             'url': 'http://xhamster.com/movies/1509445/femaleagent_shy_beauty_takes_the_bait.html',
21             'info_dict': {
22                 'id': '1509445',
23                 'ext': 'mp4',
24                 'title': 'FemaleAgent Shy beauty takes the bait',
25                 'upload_date': '20121014',
26                 'uploader_id': 'Ruseful2011',
27                 'duration': 893,
28                 'age_limit': 18,
29             }
30         },
31         {
32             'url': 'http://xhamster.com/movies/2221348/britney_spears_sexy_booty.html?hd',
33             'info_dict': {
34                 'id': '2221348',
35                 'ext': 'mp4',
36                 'title': 'Britney Spears  Sexy Booty',
37                 'upload_date': '20130914',
38                 'uploader_id': 'jojo747400',
39                 'duration': 200,
40                 'age_limit': 18,
41             }
42         },
43         {
44             'url': 'https://xhamster.com/movies/2272726/amber_slayed_by_the_knight.html',
45             'info_dict': {
46                 'id': '2272726',
47                 'ext': 'mp4',
48                 'title': 'Amber slayed by the Knight',
49                 'upload_date': '20131009',
50                 'uploader_id': 'amberblank',
51                 'duration': 149,
52                 'age_limit': 18,
53             }
54         },
55         {
56             "url": "https://xhamster.com/movies/1444747/hotkinkyjo_amp_asian_girl_elbow_deep_anal_fisting_and_footing.html",
57             'info_dict': {
58                 "id": "1444747",
59                 'ext': 'mp4',
60                 "title": "HOTKINKYJO & ASIAN GIRL ELBOW DEEP ANAL FISTING AND FOOTING",
61                 "upload_date": "20120922",
62                 "uploader_id": "alex1981",
63                 "duration": 80,
64                 "age_limit": 18,
65             }
66         }
67     ]
68
69     def _real_extract(self, url):
70         def extract_video_url(webpage):
71             mp4 = re.search(r'<video\s+.*?file="([^"]+)".*?>', webpage)
72             if mp4 is None:
73                 raise ExtractorError('Unable to extract media URL')
74             else:
75                 return mp4.group(1)
76
77         def is_hd(webpage):
78             return '<div class=\'icon iconHD\'' in webpage
79
80         mobj = re.match(self._VALID_URL, url)
81
82         video_id = mobj.group('id')
83         seo = mobj.group('seo')
84         proto = mobj.group('proto')
85         mrss_url = '%s://xhamster.com/movies/%s/%s.html' % (proto, video_id, seo)
86         webpage = self._download_webpage(mrss_url, video_id)
87
88         title = self._html_search_regex(r'<title>(?P<title>.+?) - xHamster\.com</title>', webpage, 'title')
89
90         # Only a few videos have an description
91         mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
92         description = mobj.group(1) if mobj else None
93
94         upload_date = self._html_search_regex(r'hint=\'(\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}\'',
95                                               webpage, 'upload date', fatal=False)
96         if upload_date:
97             upload_date = unified_strdate(upload_date)
98
99         uploader_id = self._html_search_regex(r'<a href=\'/user/[^>]+>(?P<uploader_id>[^<]+)',
100                                               webpage, 'uploader id', default='anonymous')
101
102         thumbnail = self._html_search_regex(r'<video\s+.*?poster="([^"]+)".*?>', webpage, 'thumbnail', fatal=False)
103
104         duration = parse_duration(self._html_search_regex(r'<span>Runtime:</span> (\d+:\d+)</div>',
105                                                           webpage, 'duration', fatal=False))
106
107         view_count = self._html_search_regex(r'<span>Views:</span> ([^<]+)</div>', webpage, 'view count', fatal=False)
108         if view_count:
109             view_count = str_to_int(view_count)
110
111         mobj = re.search(r"hint='(?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes'", webpage)
112         (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
113
114         mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
115         comment_count = mobj.group('commentcount') if mobj else 0
116
117         age_limit = self._rta_search(webpage)
118
119         hd = is_hd(webpage)
120
121         video_url = extract_video_url(webpage)
122         formats = [{
123             'url': video_url,
124             'format_id': 'hd' if hd else 'sd',
125             'preference': 1,
126         }]
127
128         if not hd:
129             mrss_url = self._search_regex(r'<link rel="canonical" href="([^"]+)', webpage, 'mrss_url')
130             webpage = self._download_webpage(mrss_url + '?hd', video_id, note='Downloading HD webpage')
131             if is_hd(webpage):
132                 video_url = extract_video_url(webpage)
133                 formats.append({
134                     'url': video_url,
135                     'format_id': 'hd',
136                     'preference': 2,
137                 })
138
139         self._sort_formats(formats)
140
141         return {
142             'id': video_id,
143             'title': title,
144             'description': description,
145             'upload_date': upload_date,
146             'uploader_id': uploader_id,
147             'thumbnail': thumbnail,
148             'duration': duration,
149             'view_count': view_count,
150             'like_count': int_or_none(like_count),
151             'dislike_count': int_or_none(dislike_count),
152             'comment_count': int_or_none(comment_count),
153             'age_limit': age_limit,
154             'formats': formats,
155         }