]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/xvideos.py
[cbs] extract smpte and vtt subtitles
[youtube-dl.git] / 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'''(?x)
18                     https?://
19                         (?:
20                             (?:www\.)?xvideos\.com/video|
21                             flashservice\.xvideos\.com/embedframe/|
22                             static-hw\.xvideos\.com/swf/xv-player\.swf\?.*?\bid_video=
23                         )
24                         (?P<id>[0-9]+)
25                     '''
26     _TESTS = [{
27         'url': 'http://www.xvideos.com/video4588838/biker_takes_his_girl',
28         'md5': '14cea69fcb84db54293b1e971466c2e1',
29         'info_dict': {
30             'id': '4588838',
31             'ext': 'mp4',
32             'title': 'Biker Takes his Girl',
33             'duration': 108,
34             'age_limit': 18,
35         }
36     }, {
37         'url': 'https://flashservice.xvideos.com/embedframe/4588838',
38         'only_matching': True,
39     }, {
40         'url': 'http://static-hw.xvideos.com/swf/xv-player.swf?id_video=4588838',
41         'only_matching': True,
42     }]
43
44     def _real_extract(self, url):
45         video_id = self._match_id(url)
46
47         webpage = self._download_webpage(
48             'https://www.xvideos.com/video%s/' % video_id, video_id)
49
50         mobj = re.search(r'<h1 class="inlineError">(.+?)</h1>', webpage)
51         if mobj:
52             raise ExtractorError('%s said: %s' % (self.IE_NAME, clean_html(mobj.group(1))), expected=True)
53
54         title = self._html_search_regex(
55             (r'<title>(?P<title>.+?)\s+-\s+XVID',
56              r'setVideoTitle\s*\(\s*(["\'])(?P<title>(?:(?!\1).)+)\1'),
57             webpage, 'title', default=None,
58             group='title') or self._og_search_title(webpage)
59
60         thumbnails = []
61         for preference, thumbnail in enumerate(('', '169')):
62             thumbnail_url = self._search_regex(
63                 r'setThumbUrl%s\(\s*(["\'])(?P<thumbnail>(?:(?!\1).)+)\1' % thumbnail,
64                 webpage, 'thumbnail', default=None, group='thumbnail')
65             if thumbnail_url:
66                 thumbnails.append({
67                     'url': thumbnail_url,
68                     'preference': preference,
69                 })
70
71         duration = int_or_none(self._og_search_property(
72             'duration', webpage, default=None)) or parse_duration(
73             self._search_regex(
74                 r'<span[^>]+class=["\']duration["\'][^>]*>.*?(\d[^<]+)',
75                 webpage, 'duration', fatal=False))
76
77         formats = []
78
79         video_url = compat_urllib_parse_unquote(self._search_regex(
80             r'flv_url=(.+?)&', webpage, 'video URL', default=''))
81         if video_url:
82             formats.append({
83                 'url': video_url,
84                 'format_id': 'flv',
85             })
86
87         for kind, _, format_url in re.findall(
88                 r'setVideo([^(]+)\((["\'])(http.+?)\2\)', webpage):
89             format_id = kind.lower()
90             if format_id == 'hls':
91                 formats.extend(self._extract_m3u8_formats(
92                     format_url, video_id, 'mp4',
93                     entry_protocol='m3u8_native', m3u8_id='hls', fatal=False))
94             elif format_id in ('urllow', 'urlhigh'):
95                 formats.append({
96                     'url': format_url,
97                     'format_id': '%s-%s' % (determine_ext(format_url, 'mp4'), format_id[3:]),
98                     'quality': -2 if format_id.endswith('low') else None,
99                 })
100
101         self._sort_formats(formats)
102
103         return {
104             'id': video_id,
105             'formats': formats,
106             'title': title,
107             'duration': duration,
108             'thumbnails': thumbnails,
109             'age_limit': 18,
110         }