]>
Commit | Line | Data |
---|---|---|
032b3df5 PH |
1 | from __future__ import unicode_literals |
2 | ||
e28ed498 S |
3 | import re |
4 | ||
9f5daf00 | 5 | from .common import InfoExtractor |
ac12e888 | 6 | from ..utils import ( |
cd13343a | 7 | determine_ext, |
ac12e888 S |
8 | ExtractorError, |
9 | int_or_none, | |
560d3b7d | 10 | merge_dicts, |
ac12e888 S |
11 | str_to_int, |
12 | unified_strdate, | |
3052a30d | 13 | url_or_none, |
ac12e888 | 14 | ) |
9f5daf00 PH |
15 | |
16 | ||
17 | class RedTubeIE(InfoExtractor): | |
5021ca6c S |
18 | _VALID_URL = r'https?://(?:(?:www\.)?redtube\.com/|embed\.redtube\.com/\?.*?\bid=)(?P<id>[0-9]+)' |
19 | _TESTS = [{ | |
032b3df5 | 20 | 'url': 'http://www.redtube.com/66418', |
18ebd1a8 | 21 | 'md5': 'fc08071233725f26b8f014dba9590005', |
032b3df5 | 22 | 'info_dict': { |
faf34948 PH |
23 | 'id': '66418', |
24 | 'ext': 'mp4', | |
838b9340 | 25 | 'title': 'Sucked on a toilet', |
18ebd1a8 | 26 | 'upload_date': '20110811', |
ac12e888 S |
27 | 'duration': 596, |
28 | 'view_count': int, | |
838b9340 | 29 | 'age_limit': 18, |
6f5ac90c | 30 | } |
5021ca6c S |
31 | }, { |
32 | 'url': 'http://embed.redtube.com/?bgcolor=000000&id=1443286', | |
33 | 'only_matching': True, | |
34 | }] | |
9f5daf00 | 35 | |
e28ed498 S |
36 | @staticmethod |
37 | def _extract_urls(webpage): | |
38 | return re.findall( | |
39 | r'<iframe[^>]+?src=["\'](?P<url>(?:https?:)?//embed\.redtube\.com/\?.*?\bid=\d+)', | |
40 | webpage) | |
41 | ||
cd214418 | 42 | def _real_extract(self, url): |
faf34948 | 43 | video_id = self._match_id(url) |
5021ca6c S |
44 | webpage = self._download_webpage( |
45 | 'http://www.redtube.com/%s' % video_id, video_id) | |
9f5daf00 | 46 | |
484637a9 S |
47 | ERRORS = ( |
48 | (('video-deleted-info', '>This video has been removed'), 'has been removed'), | |
49 | (('private_video_text', '>This video is private', '>Send a friend request to its owner to be able to view it'), 'is private'), | |
50 | ) | |
51 | ||
52 | for patterns, message in ERRORS: | |
53 | if any(p in webpage for p in patterns): | |
54 | raise ExtractorError( | |
55 | 'Video %s %s' % (video_id, message), expected=True) | |
2676caf3 | 56 | |
560d3b7d S |
57 | info = self._search_json_ld(webpage, video_id, default={}) |
58 | ||
59 | if not info.get('title'): | |
60 | info['title'] = self._html_search_regex( | |
bf097a50 | 61 | (r'<h(\d)[^>]+class="(?:video_title_text|videoTitle|video_title)[^"]*">(?P<title>(?:(?!\1).)+)</h\1>', |
560d3b7d S |
62 | r'(?:videoTitle|title)\s*:\s*(["\'])(?P<title>(?:(?!\1).)+)\1',), |
63 | webpage, 'title', group='title', | |
64 | default=None) or self._og_search_title(webpage) | |
ac12e888 S |
65 | |
66 | formats = [] | |
67 | sources = self._parse_json( | |
68 | self._search_regex( | |
69 | r'sources\s*:\s*({.+?})', webpage, 'source', default='{}'), | |
70 | video_id, fatal=False) | |
71 | if sources and isinstance(sources, dict): | |
72 | for format_id, format_url in sources.items(): | |
73 | if format_url: | |
74 | formats.append({ | |
75 | 'url': format_url, | |
76 | 'format_id': format_id, | |
77 | 'height': int_or_none(format_id), | |
78 | }) | |
880fa66f S |
79 | medias = self._parse_json( |
80 | self._search_regex( | |
cd13343a | 81 | r'mediaDefinition["\']?\s*:\s*(\[.+?}\s*\])', webpage, |
880fa66f S |
82 | 'media definitions', default='{}'), |
83 | video_id, fatal=False) | |
84 | if medias and isinstance(medias, list): | |
85 | for media in medias: | |
3052a30d S |
86 | format_url = url_or_none(media.get('videoUrl')) |
87 | if not format_url: | |
880fa66f | 88 | continue |
cd13343a S |
89 | if media.get('format') == 'hls' or determine_ext(format_url) == 'm3u8': |
90 | formats.extend(self._extract_m3u8_formats( | |
91 | format_url, video_id, 'mp4', | |
92 | entry_protocol='m3u8_native', m3u8_id='hls', | |
93 | fatal=False)) | |
94 | continue | |
880fa66f S |
95 | format_id = media.get('quality') |
96 | formats.append({ | |
97 | 'url': format_url, | |
98 | 'format_id': format_id, | |
99 | 'height': int_or_none(format_id), | |
100 | }) | |
101 | if not formats: | |
ac12e888 S |
102 | video_url = self._html_search_regex( |
103 | r'<source src="(.+?)" type="video/mp4">', webpage, 'video URL') | |
104 | formats.append({'url': video_url}) | |
105 | self._sort_formats(formats) | |
106 | ||
107 | thumbnail = self._og_search_thumbnail(webpage) | |
108 | upload_date = unified_strdate(self._search_regex( | |
560d3b7d S |
109 | r'<span[^>]+>(?:ADDED|Published on) ([^<]+)<', |
110 | webpage, 'upload date', default=None)) | |
18ebd1a8 W |
111 | duration = int_or_none(self._og_search_property( |
112 | 'video:duration', webpage, default=None) or self._search_regex( | |
113 | r'videoDuration\s*:\s*(\d+)', webpage, 'duration', default=None)) | |
ac12e888 | 114 | view_count = str_to_int(self._search_regex( |
1367c798 | 115 | (r'<div[^>]*>Views</div>\s*<div[^>]*>\s*([\d,.]+)', |
560d3b7d S |
116 | r'<span[^>]*>VIEWS</span>\s*</td>\s*<td>\s*([\d,.]+)', |
117 | r'<span[^>]+\bclass=["\']video_view_count[^>]*>\s*([\d,.]+)'), | |
118 | webpage, 'view count', default=None)) | |
ac12e888 | 119 | |
1310bf24 PH |
120 | # No self-labeling, but they describe themselves as |
121 | # "Home of Videos Porno" | |
122 | age_limit = 18 | |
123 | ||
560d3b7d | 124 | return merge_dicts(info, { |
032b3df5 | 125 | 'id': video_id, |
faf34948 | 126 | 'ext': 'mp4', |
ac12e888 S |
127 | 'thumbnail': thumbnail, |
128 | 'upload_date': upload_date, | |
129 | 'duration': duration, | |
130 | 'view_count': view_count, | |
1310bf24 | 131 | 'age_limit': age_limit, |
ac12e888 | 132 | 'formats': formats, |
560d3b7d | 133 | }) |