]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/youporn.py
[extractors] Use new framework for existing embeds (#4307)
[yt-dlp.git] / yt_dlp / extractor / youporn.py
CommitLineData
0143dc02 1import re
0143dc02
PH
2
3from .common import InfoExtractor
1cc79574 4from ..utils import (
46358f64 5 extract_attributes,
589c33da
S
6 int_or_none,
7 str_to_int,
0143dc02 8 unified_strdate,
3052a30d 9 url_or_none,
0143dc02 10)
0143dc02 11
bfe9de85 12
0143dc02 13class YouPornIE(InfoExtractor):
52c4c515 14 _VALID_URL = r'https?://(?:www\.)?youporn\.com/(?:watch|embed)/(?P<id>\d+)(?:/(?P<display_id>[^/?#&]+))?'
bfd973ec 15 _EMBED_REGEX = [r'<iframe[^>]+\bsrc=["\'](?P<url>(?:https?:)?//(?:www\.)?youporn\.com/embed/\d+)']
4f13f8f7 16 _TESTS = [{
f24e9833 17 'url': 'http://www.youporn.com/watch/505835/sex-ed-is-it-safe-to-masturbate-daily/',
2c3322e3 18 'md5': '3744d24c50438cf5b6f6d59feb5055c2',
f24e9833
PH
19 'info_dict': {
20 'id': '505835',
589c33da 21 'display_id': 'sex-ed-is-it-safe-to-masturbate-daily',
f24e9833 22 'ext': 'mp4',
f24e9833 23 'title': 'Sex Ed: Is It Safe To Masturbate Daily?',
589c33da 24 'description': 'Love & Sex Answers: http://bit.ly/DanAndJenn -- Is It Unhealthy To Masturbate Daily?',
ec85ded8 25 'thumbnail': r're:^https?://.*\.jpg$',
7c60c33e 26 'duration': 210,
e572a101 27 'uploader': 'Ask Dan And Jennifer',
18166bb8 28 'upload_date': '20101217',
589c33da
S
29 'average_rating': int,
30 'view_count': int,
31 'categories': list,
32 'tags': list,
f24e9833 33 'age_limit': 18,
4f13f8f7 34 },
46358f64 35 'skip': 'This video has been disabled',
4f13f8f7 36 }, {
f6af0f88 37 # Unknown uploader
4f13f8f7
S
38 'url': 'http://www.youporn.com/watch/561726/big-tits-awesome-brunette-on-amazing-webcam-show/?from=related3&al=2&from_id=561726&pos=4',
39 'info_dict': {
40 'id': '561726',
41 'display_id': 'big-tits-awesome-brunette-on-amazing-webcam-show',
42 'ext': 'mp4',
43 'title': 'Big Tits Awesome Brunette On amazing webcam show',
44 'description': 'http://sweetlivegirls.com Big Tits Awesome Brunette On amazing webcam show.mp4',
ec85ded8 45 'thumbnail': r're:^https?://.*\.jpg$',
f6af0f88 46 'uploader': 'Unknown',
18166bb8 47 'upload_date': '20110418',
4f13f8f7
S
48 'average_rating': int,
49 'view_count': int,
50 'categories': list,
51 'tags': list,
52 'age_limit': 18,
53 },
54 'params': {
55 'skip_download': True,
56 },
7c60c33e 57 'skip': '404',
52c4c515
S
58 }, {
59 'url': 'https://www.youporn.com/embed/505835/sex-ed-is-it-safe-to-masturbate-daily/',
60 'only_matching': True,
61 }, {
62 'url': 'http://www.youporn.com/watch/505835',
63 'only_matching': True,
30a074c2 64 }, {
65 'url': 'https://www.youporn.com/watch/13922959/femdom-principal/',
66 'only_matching': True,
4f13f8f7 67 }]
0143dc02 68
0143dc02 69 def _real_extract(self, url):
5ad28e7f 70 mobj = self._match_valid_url(url)
589c33da 71 video_id = mobj.group('id')
52c4c515 72 display_id = mobj.group('display_id') or video_id
0143dc02 73
46358f64 74 definitions = self._download_json(
75 'https://www.youporn.com/api/video/media_definitions/%s/' % video_id,
76 display_id)
589c33da 77
0143dc02 78 formats = []
46358f64 79 for definition in definitions:
80 if not isinstance(definition, dict):
81 continue
82 video_url = url_or_none(definition.get('videoUrl'))
83 if not video_url:
84 continue
589c33da 85 f = {
0143dc02 86 'url': video_url,
46358f64 87 'filesize': int_or_none(definition.get('videoSize')),
589c33da 88 }
46358f64 89 height = int_or_none(definition.get('quality'))
589c33da
S
90 # Video URL's path looks like this:
91 # /201012/17/505835/720p_1500k_505835/YouPorn%20-%20Sex%20Ed%20Is%20It%20Safe%20To%20Masturbate%20Daily.mp4
d627cec6 92 # /201012/17/505835/vl_240p_240k_505835/YouPorn%20-%20Sex%20Ed%20Is%20It%20Safe%20To%20Masturbate%20Daily.mp4
30a074c2 93 # /videos/201703/11/109285532/1080P_4000K_109285532.mp4
589c33da 94 # We will benefit from it by extracting some metadata
30a074c2 95 mobj = re.search(r'(?P<height>\d{3,4})[pP]_(?P<bitrate>\d+)[kK]_\d+', video_url)
589c33da 96 if mobj:
46358f64 97 if not height:
98 height = int(mobj.group('height'))
589c33da
S
99 bitrate = int(mobj.group('bitrate'))
100 f.update({
101 'format_id': '%dp-%dk' % (height, bitrate),
589c33da
S
102 'tbr': bitrate,
103 })
46358f64 104 f['height'] = height
589c33da 105 formats.append(f)
bfe9de85
PH
106 self._sort_formats(formats)
107
46358f64 108 webpage = self._download_webpage(
109 'http://www.youporn.com/watch/%s' % video_id, display_id,
110 headers={'Cookie': 'age_verified=1'})
111
112 title = self._html_search_regex(
113 r'(?s)<div[^>]+class=["\']watchVideoTitle[^>]+>(.+?)</div>',
114 webpage, 'title', default=None) or self._og_search_title(
115 webpage, default=None) or self._html_search_meta(
116 'title', webpage, fatal=True)
117
6089ff40
S
118 description = self._html_search_regex(
119 r'(?s)<div[^>]+\bid=["\']description["\'][^>]*>(.+?)</div>',
120 webpage, 'description',
121 default=None) or self._og_search_description(
122 webpage, default=None)
589c33da
S
123 thumbnail = self._search_regex(
124 r'(?:imageurl\s*=|poster\s*:)\s*(["\'])(?P<thumbnail>.+?)\1',
125 webpage, 'thumbnail', fatal=False, group='thumbnail')
7c60c33e 126 duration = int_or_none(self._html_search_meta(
127 'video:duration', webpage, 'duration', fatal=False))
589c33da 128
4f13f8f7 129 uploader = self._html_search_regex(
2c3322e3 130 r'(?s)<div[^>]+class=["\']submitByLink["\'][^>]*>(.+?)</div>',
589c33da
S
131 webpage, 'uploader', fatal=False)
132 upload_date = unified_strdate(self._html_search_regex(
6d1b3489 133 (r'UPLOADED:\s*<span>([^<]+)',
8bdd16b4 134 r'Date\s+[Aa]dded:\s*<span>([^<]+)',
6d1b3489 135 r'''(?s)<div[^>]+class=["']videoInfo(?:Date|Time)\b[^>]*>(.+?)</div>''',
136 r'(?s)<label\b[^>]*>Uploaded[^<]*</label>\s*<span\b[^>]*>(.+?)</span>'),
589c33da
S
137 webpage, 'upload date', fatal=False))
138
139 age_limit = self._rta_search(webpage)
140
46358f64 141 view_count = None
142 views = self._search_regex(
143 r'(<div[^>]+\bclass=["\']js_videoInfoViews["\']>)', webpage,
144 'views', default=None)
145 if views:
146 view_count = str_to_int(extract_attributes(views).get('data-value'))
755ff8d2
S
147 comment_count = str_to_int(self._search_regex(
148 r'>All [Cc]omments? \(([\d,.]+)\)',
8bdd16b4 149 webpage, 'comment count', default=None))
589c33da 150
f6af0f88
S
151 def extract_tag_box(regex, title):
152 tag_box = self._search_regex(regex, webpage, title, default=None)
589c33da
S
153 if not tag_box:
154 return []
155 return re.findall(r'<a[^>]+href=[^>]+>([^<]+)', tag_box)
156
f6af0f88
S
157 categories = extract_tag_box(
158 r'(?s)Categories:.*?</[^>]+>(.+?)</div>', 'categories')
159 tags = extract_tag_box(
160 r'(?s)Tags:.*?</div>\s*<div[^>]+class=["\']tagBoxContent["\'][^>]*>(.+?)</div>',
161 'tags')
5f6a1245 162
7df28654 163 return {
164 'id': video_id,
589c33da
S
165 'display_id': display_id,
166 'title': title,
167 'description': description,
168 'thumbnail': thumbnail,
7c60c33e 169 'duration': duration,
589c33da
S
170 'uploader': uploader,
171 'upload_date': upload_date,
589c33da 172 'view_count': view_count,
755ff8d2 173 'comment_count': comment_count,
589c33da
S
174 'categories': categories,
175 'tags': tags,
7df28654 176 'age_limit': age_limit,
177 'formats': formats,
178 }