[xhamster] Add support for hd video
[youtube-dl] / youtube_dl / extractor / xhamster.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5     compat_urllib_parse,
6     unescapeHTML,
7     determine_ext,
8     ExtractorError,
9 )
10
11
12 class XHamsterIE(InfoExtractor):
13     """Information Extractor for xHamster"""
14     _VALID_URL = r'(?:http://)?(?:www\.)?xhamster\.com/movies/(?P<id>[0-9]+)/(?P<seo>.+?)\.html(?:\?.*)?'
15     _TESTS = [{
16         u'url': u'http://xhamster.com/movies/1509445/femaleagent_shy_beauty_takes_the_bait.html',
17         u'file': u'1509445.flv',
18         u'md5': u'9f48e0e8d58e3076bb236ff412ab62fa',
19         u'info_dict': {
20             u"upload_date": u"20121014", 
21             u"uploader_id": u"Ruseful2011", 
22             u"title": u"FemaleAgent Shy beauty takes the bait",
23             u"age_limit": 18,
24         }
25     },
26     {
27         u'url': u'http://xhamster.com/movies/2221348/britney_spears_sexy_booty.html?hd',
28         u'file': u'2221348.flv',
29         u'md5': u'e767b9475de189320f691f49c679c4c7',
30         u'info_dict': {
31             u"upload_date": u"20130914",
32             u"uploader_id": u"jojo747400",
33             u"title": u"Britney Spears  Sexy Booty",
34             u"age_limit": 18,
35         }
36     }]
37
38     def _real_extract(self,url):
39         def extract_video_url(webpage):
40             mobj = re.search(r'\'srv\': \'(?P<server>[^\']*)\',\s*\'file\': \'(?P<file>[^\']+)\',', webpage)
41             if mobj is None:
42                 raise ExtractorError(u'Unable to extract media URL')
43             if len(mobj.group('server')) == 0:
44                 return compat_urllib_parse.unquote(mobj.group('file'))
45             else:
46                 return mobj.group('server')+'/key='+mobj.group('file')
47
48         def extract_mp4_video_url(webpage):
49             mp4 = re.search(r'<a href=\"(.+?)\" class=\"mp4Play\"',webpage)
50             if mp4 is None:
51                 return None
52             else:
53                 return mp4.group(1)
54
55         def is_hd(webpage):
56             return webpage.find('<div class=\'icon iconHD\'') != -1
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         video_title = self._html_search_regex(r'<title>(?P<title>.+?) - xHamster\.com</title>',
66             webpage, u'title')
67
68         # Only a few videos have an description
69         mobj = re.search('<span>Description: </span>(?P<description>[^<]+)', webpage)
70         if mobj:
71             video_description = unescapeHTML(mobj.group('description'))
72         else:
73             video_description = None
74
75         mobj = re.search(r'hint=\'(?P<upload_date_Y>[0-9]{4})-(?P<upload_date_m>[0-9]{2})-(?P<upload_date_d>[0-9]{2}) [0-9]{2}:[0-9]{2}:[0-9]{2} [A-Z]{3,4}\'', webpage)
76         if mobj:
77             video_upload_date = mobj.group('upload_date_Y')+mobj.group('upload_date_m')+mobj.group('upload_date_d')
78         else:
79             video_upload_date = None
80             self._downloader.report_warning(u'Unable to extract upload date')
81
82         video_uploader_id = self._html_search_regex(r'<a href=\'/user/[^>]+>(?P<uploader_id>[^<]+)',
83             webpage, u'uploader id', default=u'anonymous')
84
85         video_thumbnail = self._search_regex(r'\'image\':\'(?P<thumbnail>[^\']+)\'',
86             webpage, u'thumbnail', fatal=False)
87
88         age_limit = self._rta_search(webpage)
89
90         hd = is_hd(webpage)
91
92         video_url = extract_video_url(webpage)
93         formats = [{
94             'url': video_url,
95             'ext': determine_ext(video_url),
96             'format': 'hd' if hd else 'sd',
97             'format_id': 'hd' if hd else 'sd',
98         }]
99
100         video_mp4_url = extract_mp4_video_url(webpage)
101         if (not video_mp4_url is None) and (formats[0]['ext'] != 'mp4'):
102             formats.append( {
103             'url': video_mp4_url,
104             'ext': 'mp4',
105             'format': 'hd' if hd else 'sd',
106             'format_id': 'hd' if hd else 'sd',
107         })
108
109         if not hd:
110             webpage = self._download_webpage(mrss_url+'?hd', video_id)
111             if is_hd(webpage):
112                 video_url = extract_video_url(webpage)
113                 formats.append({
114                     'url': video_url,
115                     'ext': determine_ext(video_url),
116                     'format': 'hd',
117                     'format_id': 'hd',
118                 })
119
120         return {
121             'id': video_id,
122             'title': video_title,
123             'formats': formats,
124             'description': video_description,
125             'upload_date': video_upload_date,
126             'uploader_id': video_uploader_id,
127             'thumbnail': video_thumbnail,
128             'age_limit': age_limit,
129         }