[BehindKink] Minor fixes
[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'http://(?:.+?\.)?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             'md5': '8281348b8d3c53d39fffb377d24eac4e',
22             'info_dict': {
23                 'id': '1509445',
24                 'ext': 'mp4',
25                 'title': 'FemaleAgent Shy beauty takes the bait',
26                 'upload_date': '20121014',
27                 'uploader_id': 'Ruseful2011',
28                 'duration': 893,
29                 'age_limit': 18,
30             }
31         },
32         {
33             'url': 'http://xhamster.com/movies/2221348/britney_spears_sexy_booty.html?hd',
34             'md5': '4cbd8d56708ecb4fb4124c23e4acb81a',
35             'info_dict': {
36                 'id': '2221348',
37                 'ext': 'mp4',
38                 'title': 'Britney Spears  Sexy Booty',
39                 'upload_date': '20130914',
40                 'uploader_id': 'jojo747400',
41                 'duration': 200,
42                 'age_limit': 18,
43             }
44         }
45     ]
46
47     def _real_extract(self,url):
48         def extract_video_url(webpage):
49             mp4 = re.search(r'<video\s+.*?file="([^"]+)".*?>', webpage)
50             if mp4 is None:
51                 raise ExtractorError('Unable to extract media URL')
52             else:
53                 return mp4.group(1)
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         mrss_url = 'http://xhamster.com/movies/%s/%s.html' % (video_id, seo)
63         webpage = self._download_webpage(mrss_url, video_id)
64
65         title = self._html_search_regex(r'<title>(?P<title>.+?) - xHamster\.com</title>', webpage, 'title')
66
67         # Only a few videos have an description
68         mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
69         description = mobj.group(1) if mobj else None
70
71         upload_date = self._html_search_regex(r'hint=\'(\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}\'',
72             webpage, 'upload date', fatal=False)
73         if upload_date:
74             upload_date = unified_strdate(upload_date)
75
76         uploader_id = self._html_search_regex(r'<a href=\'/user/[^>]+>(?P<uploader_id>[^<]+)',
77             webpage, 'uploader id', default='anonymous')
78
79         thumbnail = self._html_search_regex(r'<video\s+.*?poster="([^"]+)".*?>', webpage, 'thumbnail', fatal=False)
80
81         duration = parse_duration(self._html_search_regex(r'<span>Runtime:</span> (\d+:\d+)</div>',
82             webpage, 'duration', fatal=False))
83
84         view_count = self._html_search_regex(r'<span>Views:</span> ([^<]+)</div>', webpage, 'view count', fatal=False)
85         if view_count:
86             view_count = str_to_int(view_count)
87
88         mobj = re.search(r"hint='(?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes'", webpage)
89         (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
90
91         mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
92         comment_count = mobj.group('commentcount') if mobj else 0
93
94         age_limit = self._rta_search(webpage)
95
96         hd = is_hd(webpage)
97
98         video_url = extract_video_url(webpage)
99         formats = [{
100             'url': video_url,
101             'format_id': 'hd' if hd else 'sd',
102             'preference': 1,
103         }]
104
105         if not hd:
106             mrss_url = self._search_regex(r'<link rel="canonical" href="([^"]+)', webpage, 'mrss_url')
107             webpage = self._download_webpage(mrss_url + '?hd', video_id, note='Downloading HD webpage')
108             if is_hd(webpage):
109                 video_url = extract_video_url(webpage)
110                 formats.append({
111                     'url': video_url,
112                     'format_id': 'hd',
113                     'preference': 2,
114                 })
115
116         self._sort_formats(formats)
117
118         return {
119             'id': video_id,
120             'title': title,
121             'description': description,
122             'upload_date': upload_date,
123             'uploader_id': uploader_id,
124             'thumbnail': thumbnail,
125             'duration': duration,
126             'view_count': view_count,
127             'like_count': int_or_none(like_count),
128             'dislike_count': int_or_none(dislike_count),
129             'comment_count': int_or_none(comment_count),
130             'age_limit': age_limit,
131             'formats': formats,
132         }