]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/redtube.py
[cleanup] Mark some compat variables for removal (#2173)
[yt-dlp.git] / yt_dlp / extractor / redtube.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7 determine_ext,
8 ExtractorError,
9 int_or_none,
10 merge_dicts,
11 str_to_int,
12 unified_strdate,
13 url_or_none,
14 )
15
16
17 class RedTubeIE(InfoExtractor):
18 _VALID_URL = r'https?://(?:(?:\w+\.)?redtube\.com/|embed\.redtube\.com/\?.*?\bid=)(?P<id>[0-9]+)'
19 _TESTS = [{
20 'url': 'https://www.redtube.com/38864951',
21 'md5': '4fba70cbca3aefd25767ab4b523c9878',
22 'info_dict': {
23 'id': '38864951',
24 'ext': 'mp4',
25 'title': 'Public Sex on the Balcony in Freezing Paris! Amateur Couple LeoLulu',
26 'description': 'Watch video Public Sex on the Balcony in Freezing Paris! Amateur Couple LeoLulu on Redtube, home of free Blowjob porn videos and Blonde sex movies online. Video length: (10:46) - Uploaded by leolulu - Verified User - Starring Pornstar: Leolulu',
27 'upload_date': '20210111',
28 'timestamp': 1610343109,
29 'duration': 646,
30 'view_count': int,
31 'age_limit': 18,
32 'thumbnail': r're:https://\wi-ph\.rdtcdn\.com/videos/.+/.+\.jpg',
33 },
34 }, {
35 'url': 'http://embed.redtube.com/?bgcolor=000000&id=1443286',
36 'only_matching': True,
37 }, {
38 'url': 'http://it.redtube.com/66418',
39 'only_matching': True,
40 }]
41
42 @staticmethod
43 def _extract_urls(webpage):
44 return re.findall(
45 r'<iframe[^>]+?src=["\'](?P<url>(?:https?:)?//embed\.redtube\.com/\?.*?\bid=\d+)',
46 webpage)
47
48 def _real_extract(self, url):
49 video_id = self._match_id(url)
50 webpage = self._download_webpage(
51 'http://www.redtube.com/%s' % video_id, video_id)
52
53 ERRORS = (
54 (('video-deleted-info', '>This video has been removed'), 'has been removed'),
55 (('private_video_text', '>This video is private', '>Send a friend request to its owner to be able to view it'), 'is private'),
56 )
57
58 for patterns, message in ERRORS:
59 if any(p in webpage for p in patterns):
60 raise ExtractorError(
61 'Video %s %s' % (video_id, message), expected=True)
62
63 info = self._search_json_ld(webpage, video_id, default={})
64
65 if not info.get('title'):
66 info['title'] = self._html_search_regex(
67 (r'<h(\d)[^>]+class="(?:video_title_text|videoTitle|video_title)[^"]*">(?P<title>(?:(?!\1).)+)</h\1>',
68 r'(?:videoTitle|title)\s*:\s*(["\'])(?P<title>(?:(?!\1).)+)\1',),
69 webpage, 'title', group='title',
70 default=None) or self._og_search_title(webpage)
71
72 formats = []
73 sources = self._parse_json(
74 self._search_regex(
75 r'sources\s*:\s*({.+?})', webpage, 'source', default='{}'),
76 video_id, fatal=False)
77 if sources and isinstance(sources, dict):
78 for format_id, format_url in sources.items():
79 if format_url:
80 formats.append({
81 'url': format_url,
82 'format_id': format_id,
83 'height': int_or_none(format_id),
84 })
85 medias = self._parse_json(
86 self._search_regex(
87 r'mediaDefinition["\']?\s*:\s*(\[.+?}\s*\])', webpage,
88 'media definitions', default='{}'),
89 video_id, fatal=False)
90 for media in medias if isinstance(medias, list) else []:
91 format_url = url_or_none(media.get('videoUrl'))
92 if not format_url:
93 continue
94 format_id = media.get('format')
95 quality = media.get('quality')
96 if format_id == 'hls' or (format_id == 'mp4' and not quality):
97 more_media = self._download_json(format_url, video_id, fatal=False)
98 else:
99 more_media = [media]
100 for media in more_media if isinstance(more_media, list) else []:
101 format_url = url_or_none(media.get('videoUrl'))
102 if not format_url:
103 continue
104 format_id = media.get('format')
105 if format_id == 'hls' or determine_ext(format_url) == 'm3u8':
106 formats.extend(self._extract_m3u8_formats(
107 format_url, video_id, 'mp4',
108 entry_protocol='m3u8_native', m3u8_id=format_id or 'hls',
109 fatal=False))
110 continue
111 format_id = media.get('quality')
112 formats.append({
113 'url': format_url,
114 'ext': 'mp4',
115 'format_id': format_id,
116 'height': int_or_none(format_id),
117 })
118 if not formats:
119 video_url = self._html_search_regex(
120 r'<source src="(.+?)" type="video/mp4">', webpage, 'video URL')
121 formats.append({'url': video_url, 'ext': 'mp4'})
122 self._sort_formats(formats)
123
124 thumbnail = self._og_search_thumbnail(webpage)
125 upload_date = unified_strdate(self._search_regex(
126 r'<span[^>]+>(?:ADDED|Published on) ([^<]+)<',
127 webpage, 'upload date', default=None))
128 duration = int_or_none(self._og_search_property(
129 'video:duration', webpage, default=None) or self._search_regex(
130 r'videoDuration\s*:\s*(\d+)', webpage, 'duration', default=None))
131 view_count = str_to_int(self._search_regex(
132 (r'<div[^>]*>Views</div>\s*<div[^>]*>\s*([\d,.]+)',
133 r'<span[^>]*>VIEWS</span>\s*</td>\s*<td>\s*([\d,.]+)',
134 r'<span[^>]+\bclass=["\']video_view_count[^>]*>\s*([\d,.]+)'),
135 webpage, 'view count', default=None))
136
137 # No self-labeling, but they describe themselves as
138 # "Home of Videos Porno"
139 age_limit = 18
140
141 return merge_dicts(info, {
142 'id': video_id,
143 'ext': 'mp4',
144 'thumbnail': thumbnail,
145 'upload_date': upload_date,
146 'duration': duration,
147 'view_count': view_count,
148 'age_limit': age_limit,
149 'formats': formats,
150 })