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