]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/youporn.py
Fix "invalid escape sequences" error on Python 3.6
[yt-dlp.git] / youtube_dl / extractor / youporn.py
CommitLineData
f24e9833
PH
1from __future__ import unicode_literals
2
0143dc02 3import re
0143dc02
PH
4
5from .common import InfoExtractor
1cc79574 6from ..utils import (
589c33da 7 int_or_none,
5c2266df 8 sanitized_Request,
589c33da 9 str_to_int,
0143dc02
PH
10 unescapeHTML,
11 unified_strdate,
12)
589c33da 13from ..aes import aes_decrypt_text
0143dc02 14
bfe9de85 15
0143dc02 16class YouPornIE(InfoExtractor):
589c33da 17 _VALID_URL = r'https?://(?:www\.)?youporn\.com/watch/(?P<id>\d+)/(?P<display_id>[^/?#&]+)'
4f13f8f7 18 _TESTS = [{
f24e9833 19 'url': 'http://www.youporn.com/watch/505835/sex-ed-is-it-safe-to-masturbate-daily/',
2c3322e3 20 'md5': '3744d24c50438cf5b6f6d59feb5055c2',
f24e9833
PH
21 'info_dict': {
22 'id': '505835',
589c33da 23 'display_id': 'sex-ed-is-it-safe-to-masturbate-daily',
f24e9833 24 'ext': 'mp4',
f24e9833 25 'title': 'Sex Ed: Is It Safe To Masturbate Daily?',
589c33da 26 'description': 'Love & Sex Answers: http://bit.ly/DanAndJenn -- Is It Unhealthy To Masturbate Daily?',
ec85ded8 27 'thumbnail': r're:^https?://.*\.jpg$',
e572a101 28 'uploader': 'Ask Dan And Jennifer',
589c33da
S
29 'upload_date': '20101221',
30 'average_rating': int,
31 'view_count': int,
755ff8d2 32 'comment_count': int,
589c33da
S
33 'categories': list,
34 'tags': list,
f24e9833 35 'age_limit': 18,
4f13f8f7
S
36 },
37 }, {
f6af0f88 38 # Unknown uploader
4f13f8f7
S
39 'url': 'http://www.youporn.com/watch/561726/big-tits-awesome-brunette-on-amazing-webcam-show/?from=related3&al=2&from_id=561726&pos=4',
40 'info_dict': {
41 'id': '561726',
42 'display_id': 'big-tits-awesome-brunette-on-amazing-webcam-show',
43 'ext': 'mp4',
44 'title': 'Big Tits Awesome Brunette On amazing webcam show',
45 'description': 'http://sweetlivegirls.com Big Tits Awesome Brunette On amazing webcam show.mp4',
ec85ded8 46 'thumbnail': r're:^https?://.*\.jpg$',
f6af0f88 47 'uploader': 'Unknown',
4f13f8f7
S
48 'upload_date': '20111125',
49 'average_rating': int,
50 'view_count': int,
755ff8d2 51 'comment_count': int,
4f13f8f7
S
52 'categories': list,
53 'tags': list,
54 'age_limit': 18,
55 },
56 'params': {
57 'skip_download': True,
58 },
59 }]
0143dc02 60
0143dc02
PH
61 def _real_extract(self, url):
62 mobj = re.match(self._VALID_URL, url)
589c33da
S
63 video_id = mobj.group('id')
64 display_id = mobj.group('display_id')
0143dc02 65
5c2266df 66 request = sanitized_Request(url)
589c33da
S
67 request.add_header('Cookie', 'age_verified=1')
68 webpage = self._download_webpage(request, display_id)
69
70 title = self._search_regex(
71 [r'(?:video_titles|videoTitle)\s*[:=]\s*(["\'])(?P<title>.+?)\1',
72 r'<h1[^>]+class=["\']heading\d?["\'][^>]*>([^<])<'],
73 webpage, 'title', group='title')
0143dc02 74
589c33da
S
75 links = []
76
77 sources = self._search_regex(
002c7552 78 r'(?s)sources\s*:\s*({.+?})', webpage, 'sources', default=None)
589c33da
S
79 if sources:
80 for _, link in re.findall(r'[^:]+\s*:\s*(["\'])(http.+?)\1', sources):
e572a101 81 links.append(link)
5f6a1245 82
589c33da
S
83 # Fallback #1
84 for _, link in re.findall(
85 r'(?:videoUrl|videoSrc|videoIpadUrl|html5PlayerSrc)\s*[:=]\s*(["\'])(http.+?)\1', webpage):
86 links.append(link)
87
88 # Fallback #2, this also contains extra low quality 180p format
89 for _, link in re.findall(r'<a[^>]+href=(["\'])(http.+?)\1[^>]+title=["\']Download [Vv]ideo', webpage):
90 links.append(link)
91
92 # Fallback #3, encrypted links
93 for _, encrypted_link in re.findall(
94 r'encryptedQuality\d{3,4}URL\s*=\s*(["\'])([\da-zA-Z+/=]+)\1', webpage):
95 links.append(aes_decrypt_text(encrypted_link, title, 32).decode('utf-8'))
96
0143dc02 97 formats = []
589c33da
S
98 for video_url in set(unescapeHTML(link) for link in links):
99 f = {
0143dc02 100 'url': video_url,
589c33da
S
101 }
102 # Video URL's path looks like this:
103 # /201012/17/505835/720p_1500k_505835/YouPorn%20-%20Sex%20Ed%20Is%20It%20Safe%20To%20Masturbate%20Daily.mp4
d627cec6 104 # /201012/17/505835/vl_240p_240k_505835/YouPorn%20-%20Sex%20Ed%20Is%20It%20Safe%20To%20Masturbate%20Daily.mp4
589c33da 105 # We will benefit from it by extracting some metadata
d627cec6 106 mobj = re.search(r'(?P<height>\d{3,4})[pP]_(?P<bitrate>\d+)[kK]_\d+/', video_url)
589c33da
S
107 if mobj:
108 height = int(mobj.group('height'))
109 bitrate = int(mobj.group('bitrate'))
110 f.update({
111 'format_id': '%dp-%dk' % (height, bitrate),
112 'height': height,
113 'tbr': bitrate,
114 })
115 formats.append(f)
bfe9de85
PH
116 self._sort_formats(formats)
117
b99d88c6 118 description = self._og_search_description(webpage, default=None)
589c33da
S
119 thumbnail = self._search_regex(
120 r'(?:imageurl\s*=|poster\s*:)\s*(["\'])(?P<thumbnail>.+?)\1',
121 webpage, 'thumbnail', fatal=False, group='thumbnail')
122
4f13f8f7 123 uploader = self._html_search_regex(
2c3322e3 124 r'(?s)<div[^>]+class=["\']submitByLink["\'][^>]*>(.+?)</div>',
589c33da
S
125 webpage, 'uploader', fatal=False)
126 upload_date = unified_strdate(self._html_search_regex(
2c3322e3 127 r'(?s)<div[^>]+class=["\']videoInfo(?:Date|Time)["\'][^>]*>(.+?)</div>',
589c33da
S
128 webpage, 'upload date', fatal=False))
129
130 age_limit = self._rta_search(webpage)
131
132 average_rating = int_or_none(self._search_regex(
2c3322e3 133 r'<div[^>]+class=["\']videoRatingPercentage["\'][^>]*>(\d+)%</div>',
589c33da
S
134 webpage, 'average rating', fatal=False))
135
136 view_count = str_to_int(self._search_regex(
2c3322e3
S
137 r'(?s)<div[^>]+class=(["\']).*?\bvideoInfoViews\b.*?\1[^>]*>.*?(?P<count>[\d,.]+)<',
138 webpage, 'view count', fatal=False, group='count'))
755ff8d2
S
139 comment_count = str_to_int(self._search_regex(
140 r'>All [Cc]omments? \(([\d,.]+)\)',
141 webpage, 'comment count', fatal=False))
589c33da 142
f6af0f88
S
143 def extract_tag_box(regex, title):
144 tag_box = self._search_regex(regex, webpage, title, default=None)
589c33da
S
145 if not tag_box:
146 return []
147 return re.findall(r'<a[^>]+href=[^>]+>([^<]+)', tag_box)
148
f6af0f88
S
149 categories = extract_tag_box(
150 r'(?s)Categories:.*?</[^>]+>(.+?)</div>', 'categories')
151 tags = extract_tag_box(
152 r'(?s)Tags:.*?</div>\s*<div[^>]+class=["\']tagBoxContent["\'][^>]*>(.+?)</div>',
153 'tags')
5f6a1245 154
7df28654 155 return {
156 'id': video_id,
589c33da
S
157 'display_id': display_id,
158 'title': title,
159 'description': description,
160 'thumbnail': thumbnail,
161 'uploader': uploader,
162 'upload_date': upload_date,
163 'average_rating': average_rating,
164 'view_count': view_count,
755ff8d2 165 'comment_count': comment_count,
589c33da
S
166 'categories': categories,
167 'tags': tags,
7df28654 168 'age_limit': age_limit,
169 'formats': formats,
170 }