Merge branch 'master' into openload-phantomjs-method
[youtube-dl] / youtube_dl / extractor / xvideos.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import compat_urllib_parse_unquote
7 from ..utils import (
8     clean_html,
9     determine_ext,
10     ExtractorError,
11     int_or_none,
12     parse_duration,
13 )
14
15
16 class XVideosIE(InfoExtractor):
17     _VALID_URL = r'https?://(?:www\.)?xvideos\.com/video(?P<id>[0-9]+)(?:.*)'
18     _TEST = {
19         'url': 'http://www.xvideos.com/video4588838/biker_takes_his_girl',
20         'md5': '14cea69fcb84db54293b1e971466c2e1',
21         'info_dict': {
22             'id': '4588838',
23             'ext': 'mp4',
24             'title': 'Biker Takes his Girl',
25             'duration': 108,
26             'age_limit': 18,
27         }
28     }
29
30     def _real_extract(self, url):
31         video_id = self._match_id(url)
32         webpage = self._download_webpage(url, video_id)
33
34         mobj = re.search(r'<h1 class="inlineError">(.+?)</h1>', webpage)
35         if mobj:
36             raise ExtractorError('%s said: %s' % (self.IE_NAME, clean_html(mobj.group(1))), expected=True)
37
38         video_title = self._html_search_regex(
39             r'<title>(.*?)\s+-\s+XVID', webpage, 'title')
40         video_thumbnail = self._search_regex(
41             r'url_bigthumb=(.+?)&amp', webpage, 'thumbnail', fatal=False)
42         video_duration = int_or_none(self._og_search_property(
43             'duration', webpage, default=None)) or parse_duration(
44             self._search_regex(
45                 r'<span[^>]+class=["\']duration["\'][^>]*>.*?(\d[^<]+)',
46                 webpage, 'duration', fatal=False))
47
48         formats = []
49
50         video_url = compat_urllib_parse_unquote(self._search_regex(
51             r'flv_url=(.+?)&', webpage, 'video URL', default=''))
52         if video_url:
53             formats.append({
54                 'url': video_url,
55                 'format_id': 'flv',
56             })
57
58         for kind, _, format_url in re.findall(
59                 r'setVideo([^(]+)\((["\'])(http.+?)\2\)', webpage):
60             format_id = kind.lower()
61             if format_id == 'hls':
62                 formats.extend(self._extract_m3u8_formats(
63                     format_url, video_id, 'mp4',
64                     entry_protocol='m3u8_native', m3u8_id='hls', fatal=False))
65             elif format_id in ('urllow', 'urlhigh'):
66                 formats.append({
67                     'url': format_url,
68                     'format_id': '%s-%s' % (determine_ext(format_url, 'mp4'), format_id[3:]),
69                     'quality': -2 if format_id.endswith('low') else None,
70                 })
71
72         self._sort_formats(formats)
73
74         return {
75             'id': video_id,
76             'formats': formats,
77             'title': video_title,
78             'duration': video_duration,
79             'thumbnail': video_thumbnail,
80             'age_limit': 18,
81         }