]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/redtube.py
Pre-process when using `--flat-playlist`
[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 = [{
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,
1ca5f821 34 }, {
35 'url': 'http://it.redtube.com/66418',
36 'only_matching': True,
5021ca6c 37 }]
9f5daf00 38
e28ed498
S
39 @staticmethod
40 def _extract_urls(webpage):
41 return re.findall(
42 r'<iframe[^>]+?src=["\'](?P<url>(?:https?:)?//embed\.redtube\.com/\?.*?\bid=\d+)',
43 webpage)
44
cd214418 45 def _real_extract(self, url):
faf34948 46 video_id = self._match_id(url)
5021ca6c
S
47 webpage = self._download_webpage(
48 'http://www.redtube.com/%s' % video_id, video_id)
9f5daf00 49
484637a9
S
50 ERRORS = (
51 (('video-deleted-info', '>This video has been removed'), 'has been removed'),
52 (('private_video_text', '>This video is private', '>Send a friend request to its owner to be able to view it'), 'is private'),
53 )
54
55 for patterns, message in ERRORS:
56 if any(p in webpage for p in patterns):
57 raise ExtractorError(
58 'Video %s %s' % (video_id, message), expected=True)
2676caf3 59
560d3b7d
S
60 info = self._search_json_ld(webpage, video_id, default={})
61
62 if not info.get('title'):
63 info['title'] = self._html_search_regex(
bf097a50 64 (r'<h(\d)[^>]+class="(?:video_title_text|videoTitle|video_title)[^"]*">(?P<title>(?:(?!\1).)+)</h\1>',
560d3b7d
S
65 r'(?:videoTitle|title)\s*:\s*(["\'])(?P<title>(?:(?!\1).)+)\1',),
66 webpage, 'title', group='title',
67 default=None) or self._og_search_title(webpage)
ac12e888
S
68
69 formats = []
70 sources = self._parse_json(
71 self._search_regex(
72 r'sources\s*:\s*({.+?})', webpage, 'source', default='{}'),
73 video_id, fatal=False)
74 if sources and isinstance(sources, dict):
75 for format_id, format_url in sources.items():
76 if format_url:
77 formats.append({
78 'url': format_url,
79 'format_id': format_id,
80 'height': int_or_none(format_id),
81 })
880fa66f
S
82 medias = self._parse_json(
83 self._search_regex(
cd13343a 84 r'mediaDefinition["\']?\s*:\s*(\[.+?}\s*\])', webpage,
880fa66f
S
85 'media definitions', default='{}'),
86 video_id, fatal=False)
87 if medias and isinstance(medias, list):
88 for media in medias:
3052a30d
S
89 format_url = url_or_none(media.get('videoUrl'))
90 if not format_url:
880fa66f 91 continue
cd13343a
S
92 if media.get('format') == 'hls' or determine_ext(format_url) == 'm3u8':
93 formats.extend(self._extract_m3u8_formats(
94 format_url, video_id, 'mp4',
95 entry_protocol='m3u8_native', m3u8_id='hls',
96 fatal=False))
97 continue
880fa66f
S
98 format_id = media.get('quality')
99 formats.append({
100 'url': format_url,
265a7a8e 101 'ext': 'mp4',
880fa66f
S
102 'format_id': format_id,
103 'height': int_or_none(format_id),
104 })
105 if not formats:
ac12e888
S
106 video_url = self._html_search_regex(
107 r'<source src="(.+?)" type="video/mp4">', webpage, 'video URL')
265a7a8e 108 formats.append({'url': video_url, 'ext': 'mp4'})
ac12e888
S
109 self._sort_formats(formats)
110
111 thumbnail = self._og_search_thumbnail(webpage)
112 upload_date = unified_strdate(self._search_regex(
560d3b7d
S
113 r'<span[^>]+>(?:ADDED|Published on) ([^<]+)<',
114 webpage, 'upload date', default=None))
18ebd1a8
W
115 duration = int_or_none(self._og_search_property(
116 'video:duration', webpage, default=None) or self._search_regex(
117 r'videoDuration\s*:\s*(\d+)', webpage, 'duration', default=None))
ac12e888 118 view_count = str_to_int(self._search_regex(
1367c798 119 (r'<div[^>]*>Views</div>\s*<div[^>]*>\s*([\d,.]+)',
560d3b7d
S
120 r'<span[^>]*>VIEWS</span>\s*</td>\s*<td>\s*([\d,.]+)',
121 r'<span[^>]+\bclass=["\']video_view_count[^>]*>\s*([\d,.]+)'),
122 webpage, 'view count', default=None))
ac12e888 123
1310bf24
PH
124 # No self-labeling, but they describe themselves as
125 # "Home of Videos Porno"
126 age_limit = 18
127
560d3b7d 128 return merge_dicts(info, {
032b3df5 129 'id': video_id,
faf34948 130 'ext': 'mp4',
ac12e888
S
131 'thumbnail': thumbnail,
132 'upload_date': upload_date,
133 'duration': duration,
134 'view_count': view_count,
1310bf24 135 'age_limit': age_limit,
ac12e888 136 'formats': formats,
560d3b7d 137 })