]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/redtube.py
[utils] Better traceback for `ExtractorError`
[yt-dlp.git] / yt_dlp / extractor / redtube.py
CommitLineData
032b3df5
PH
1from __future__ import unicode_literals
2
e28ed498
S
3import re
4
9f5daf00 5from .common import InfoExtractor
ac12e888 6from ..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
17class RedTubeIE(InfoExtractor):
1ca5f821 18 _VALID_URL = r'https?://(?:(?:\w+\.)?redtube\.com/|embed\.redtube\.com/\?.*?\bid=)(?P<id>[0-9]+)'
5021ca6c 19 _TESTS = [{
93864403 20 'url': 'https://www.redtube.com/38864951',
21 'md5': '4fba70cbca3aefd25767ab4b523c9878',
032b3df5 22 'info_dict': {
93864403 23 'id': '38864951',
faf34948 24 'ext': 'mp4',
93864403 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,
ac12e888 30 'view_count': int,
838b9340 31 'age_limit': 18,
93864403 32 'thumbnail': r're:https://\wi-ph\.rdtcdn\.com/videos/.+/.+\.jpg',
33 },
5021ca6c
S
34 }, {
35 'url': 'http://embed.redtube.com/?bgcolor=000000&id=1443286',
36 'only_matching': True,
1ca5f821 37 }, {
38 'url': 'http://it.redtube.com/66418',
39 'only_matching': True,
5021ca6c 40 }]
9f5daf00 41
e28ed498
S
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
cd214418 48 def _real_extract(self, url):
faf34948 49 video_id = self._match_id(url)
5021ca6c
S
50 webpage = self._download_webpage(
51 'http://www.redtube.com/%s' % video_id, video_id)
9f5daf00 52
484637a9
S
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)
2676caf3 62
560d3b7d
S
63 info = self._search_json_ld(webpage, video_id, default={})
64
65 if not info.get('title'):
66 info['title'] = self._html_search_regex(
bf097a50 67 (r'<h(\d)[^>]+class="(?:video_title_text|videoTitle|video_title)[^"]*">(?P<title>(?:(?!\1).)+)</h\1>',
560d3b7d
S
68 r'(?:videoTitle|title)\s*:\s*(["\'])(?P<title>(?:(?!\1).)+)\1',),
69 webpage, 'title', group='title',
70 default=None) or self._og_search_title(webpage)
ac12e888
S
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 })
880fa66f
S
85 medias = self._parse_json(
86 self._search_regex(
cd13343a 87 r'mediaDefinition["\']?\s*:\s*(\[.+?}\s*\])', webpage,
880fa66f
S
88 'media definitions', default='{}'),
89 video_id, fatal=False)
93864403 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 []:
3052a30d
S
101 format_url = url_or_none(media.get('videoUrl'))
102 if not format_url:
880fa66f 103 continue
93864403 104 format_id = media.get('format')
105 if format_id == 'hls' or determine_ext(format_url) == 'm3u8':
cd13343a
S
106 formats.extend(self._extract_m3u8_formats(
107 format_url, video_id, 'mp4',
93864403 108 entry_protocol='m3u8_native', m3u8_id=format_id or 'hls',
cd13343a
S
109 fatal=False))
110 continue
880fa66f
S
111 format_id = media.get('quality')
112 formats.append({
113 'url': format_url,
265a7a8e 114 'ext': 'mp4',
880fa66f
S
115 'format_id': format_id,
116 'height': int_or_none(format_id),
117 })
118 if not formats:
ac12e888
S
119 video_url = self._html_search_regex(
120 r'<source src="(.+?)" type="video/mp4">', webpage, 'video URL')
265a7a8e 121 formats.append({'url': video_url, 'ext': 'mp4'})
ac12e888
S
122 self._sort_formats(formats)
123
124 thumbnail = self._og_search_thumbnail(webpage)
125 upload_date = unified_strdate(self._search_regex(
560d3b7d
S
126 r'<span[^>]+>(?:ADDED|Published on) ([^<]+)<',
127 webpage, 'upload date', default=None))
18ebd1a8
W
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))
ac12e888 131 view_count = str_to_int(self._search_regex(
1367c798 132 (r'<div[^>]*>Views</div>\s*<div[^>]*>\s*([\d,.]+)',
560d3b7d
S
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))
ac12e888 136
1310bf24
PH
137 # No self-labeling, but they describe themselves as
138 # "Home of Videos Porno"
139 age_limit = 18
140
560d3b7d 141 return merge_dicts(info, {
032b3df5 142 'id': video_id,
faf34948 143 'ext': 'mp4',
ac12e888
S
144 'thumbnail': thumbnail,
145 'upload_date': upload_date,
146 'duration': duration,
147 'view_count': view_count,
1310bf24 148 'age_limit': age_limit,
ac12e888 149 'formats': formats,
560d3b7d 150 })