youtube-dl/youtube_dl/extractor/youporn.py

113 lines
4.7 KiB
Python
Raw Normal View History

2014-03-20 23:19:54 +00:00
from __future__ import unicode_literals
2013-06-23 20:12:14 +00:00
import json
import re
import sys
import datetime
2013-06-23 20:12:14 +00:00
from .common import InfoExtractor
from ..compat import (
2013-06-23 20:12:14 +00:00
compat_urllib_parse_urlparse,
compat_urllib_request,
)
from ..utils import (
2013-06-23 20:12:14 +00:00
ExtractorError,
unescapeHTML,
unified_strdate,
)
from ..aes import (
aes_decrypt_text
)
2013-06-23 20:12:14 +00:00
2013-06-23 20:12:14 +00:00
class YouPornIE(InfoExtractor):
2014-03-20 23:19:54 +00:00
_VALID_URL = r'^(?P<proto>https?://)(?:www\.)?(?P<url>youporn\.com/watch/(?P<videoid>[0-9]+)/(?P<title>[^/]+))'
2013-06-27 18:46:46 +00:00
_TEST = {
2014-03-20 23:19:54 +00:00
'url': 'http://www.youporn.com/watch/505835/sex-ed-is-it-safe-to-masturbate-daily/',
'info_dict': {
'id': '505835',
'ext': 'mp4',
'title': 'Sex Ed: Is It Safe To Masturbate Daily?',
'description': 'Watch Sex Ed: Is It Safe To Masturbate Daily? at YouPorn.com - YouPorn is the biggest free porn tube site on the net!',
'uploader': 'Ask Dan And Jennifer',
'thumbnail': 'http://cdn5.image.youporn.phncdn.com/201012/17/505835/640x480/8/sex-ed-is-it-safe-to-masturbate-daily-8.jpg',
'date': '20101221',
2014-03-20 23:19:54 +00:00
'age_limit': 18,
2013-06-27 18:46:46 +00:00
}
}
2013-06-23 20:12:14 +00:00
def _real_extract(self, url):
mobj = re.match(self._VALID_URL, url)
video_id = mobj.group('videoid')
2014-03-20 23:19:54 +00:00
url = mobj.group('proto') + 'www.' + mobj.group('url')
2013-06-23 20:12:14 +00:00
req = compat_urllib_request.Request(url)
req.add_header('Cookie', 'age_verified=1')
webpage = self._download_webpage(req, video_id)
age_limit = self._rta_search(webpage)
2013-06-23 20:12:14 +00:00
self.report_extraction(video_id)
video_title = self._html_search_regex(r'page_params.video_title = \'(.+?)\';', webpage, 'video URL', fatal=False)
video_description = self._html_search_meta('description', webpage, 'video DESC', fatal=False)
video_thumbnail = self._html_search_regex(r'page_params.imageurl\t=\t"(.+?)";', webpage, 'video THUMB', fatal=False)
video_uploader = self._html_search_regex(r"<div class=\'videoInfoBy\'>By:</div>\n<a href=\"[^>]+\">(.+?)</a>", webpage, 'video UPLOADER', fatal=False)
video_date = self._html_search_regex(r"<div class='videoInfoTime'>\n<i class='icon-clock'></i> (.+?)\n</div>", webpage, 'video DATE', fatal=False)
video_date = datetime.datetime.strptime(video_date, '%B %d, %Y').strftime('%Y%m%d')
2013-06-23 20:12:14 +00:00
# Get all of the links from the page
DOWNLOAD_LIST_RE = r'(?s)sources: {\n(?P<download_list>.*?)}'
download_list_html = self._search_regex(DOWNLOAD_LIST_RE, webpage, 'download list').strip()
LINK_RE = r': \'(.+?)\','
2013-06-23 20:12:14 +00:00
links = re.findall(LINK_RE, download_list_html)
# Get all encrypted links
encrypted_links = re.findall(r'page_params.encryptedQuality[0-9]{3,4}URL\s=\s\'([a-zA-Z0-9+/]+={0,2})\';', webpage)
for encrypted_link in encrypted_links:
link = aes_decrypt_text(encrypted_link, video_title, 32).decode('utf-8')
# it's unclear if encryted links still differ from normal ones, so only include in links array if it's unique
if link not in links:
links.append(link)
2014-11-23 19:41:03 +00:00
2013-06-23 20:12:14 +00:00
formats = []
for link in links:
# A link looks like this:
# http://cdn2b.public.youporn.phncdn.com/201012/17/505835/720p_1500k_505835/YouPorn%20-%20Sex%20Ed%20Is%20It%20Safe%20To%20Masturbate%20Daily.mp4?rs=200&ri=2500&s=1445599900&e=1445773500&h=5345d19ce9944ec52eb167abf24af248
2013-06-23 20:12:14 +00:00
# A path looks like this:
# 201012/17/505835/720p_1500k_505835/YouPorn%20-%20Sex%20Ed%20Is%20It%20Safe%20To%20Masturbate%20Daily.mp4
video_url = unescapeHTML(link)
path = compat_urllib_parse_urlparse(video_url).path
format_parts = path.split('/')[4].split('_')[:2]
dn = compat_urllib_parse_urlparse(video_url).netloc.partition('.')[0]
resolution = format_parts[0]
height = int(resolution[:-len('p')])
bitrate = int(format_parts[1][:-len('k')])
2014-03-20 23:19:54 +00:00
format = '-'.join(format_parts) + '-' + dn
2013-06-23 20:12:14 +00:00
formats.append({
'url': video_url,
'format': format,
'format_id': format,
'height': height,
'tbr': bitrate,
'resolution': resolution,
2013-06-23 20:12:14 +00:00
})
self._sort_formats(formats)
if not formats:
2014-11-26 12:06:02 +00:00
raise ExtractorError('ERROR: no known formats available for video')
2014-11-23 19:41:03 +00:00
return {
'id': video_id,
'title': video_title,
'description': video_description,
'thumbnail': video_thumbnail,
'uploader': video_uploader,
'date': video_date,
'age_limit': age_limit,
'formats': formats,
}