[addanime] improve
[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]+)/.*\.html'
15     _TEST = {
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         }
24     }
25
26     def _real_extract(self,url):
27         mobj = re.match(self._VALID_URL, url)
28
29         video_id = mobj.group('id')
30         mrss_url = 'http://xhamster.com/movies/%s/.html' % video_id
31         webpage = self._download_webpage(mrss_url, video_id)
32
33         mobj = re.search(r'\'srv\': \'(?P<server>[^\']*)\',\s*\'file\': \'(?P<file>[^\']+)\',', webpage)
34         if mobj is None:
35             raise ExtractorError(u'Unable to extract media URL')
36         if len(mobj.group('server')) == 0:
37             video_url = compat_urllib_parse.unquote(mobj.group('file'))
38         else:
39             video_url = mobj.group('server')+'/key='+mobj.group('file')
40
41         video_title = self._html_search_regex(r'<title>(?P<title>.+?) - xHamster\.com</title>',
42             webpage, u'title')
43
44         # Only a few videos have an description
45         mobj = re.search('<span>Description: </span>(?P<description>[^<]+)', webpage)
46         if mobj:
47             video_description = unescapeHTML(mobj.group('description'))
48         else:
49             video_description = None
50
51         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)
52         if mobj:
53             video_upload_date = mobj.group('upload_date_Y')+mobj.group('upload_date_m')+mobj.group('upload_date_d')
54         else:
55             video_upload_date = None
56             self._downloader.report_warning(u'Unable to extract upload date')
57
58         video_uploader_id = self._html_search_regex(r'<a href=\'/user/[^>]+>(?P<uploader_id>[^<]+)',
59             webpage, u'uploader id', default=u'anonymous')
60
61         video_thumbnail = self._search_regex(r'\'image\':\'(?P<thumbnail>[^\']+)\'',
62             webpage, u'thumbnail', fatal=False)
63
64         return [{
65             'id':       video_id,
66             'url':      video_url,
67             'ext':      determine_ext(video_url),
68             'title':    video_title,
69             'description': video_description,
70             'upload_date': video_upload_date,
71             'uploader_id': video_uploader_id,
72             'thumbnail': video_thumbnail
73         }]