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