[youporn] Fix extraction
[youtube-dl] / youtube_dl / extractor / youporn.py
1 from __future__ import unicode_literals
2
3
4 import json
5 import re
6 import sys
7 import datetime
8
9 from .common import InfoExtractor
10 from ..compat import (
11     compat_urllib_parse_urlparse,
12     compat_urllib_request,
13 )
14 from ..utils import (
15     ExtractorError,
16     unescapeHTML,
17     unified_strdate,
18 )
19 from ..aes import (
20     aes_decrypt_text
21 )
22
23
24 class YouPornIE(InfoExtractor):
25     _VALID_URL = r'^(?P<proto>https?://)(?:www\.)?(?P<url>youporn\.com/watch/(?P<videoid>[0-9]+)/(?P<title>[^/]+))'
26     _TEST = {
27         'url': 'http://www.youporn.com/watch/505835/sex-ed-is-it-safe-to-masturbate-daily/',
28         'info_dict': {
29             'id': '505835',
30             'ext': 'mp4',
31             'title': 'Sex Ed: Is It Safe To Masturbate Daily?',
32             'description': 'Watch Sex Ed: Is It Safe To Masturbate Daily? at YouPorn.com - YouPorn is the biggest free porn tube site on the net!',
33             'uploader': 'Ask Dan And Jennifer',
34             'thumbnail': 'http://cdn5.image.youporn.phncdn.com/201012/17/505835/640x480/8/sex-ed-is-it-safe-to-masturbate-daily-8.jpg',
35             'date': '20101221',
36             'age_limit': 18,
37         }
38     }
39
40     def _real_extract(self, url):
41         mobj = re.match(self._VALID_URL, url)
42         video_id = mobj.group('videoid')
43         url = mobj.group('proto') + 'www.' + mobj.group('url')
44
45         req = compat_urllib_request.Request(url)
46         req.add_header('Cookie', 'age_verified=1')
47         webpage = self._download_webpage(req, video_id)
48         age_limit = self._rta_search(webpage)
49
50         self.report_extraction(video_id)
51         video_title = self._html_search_regex(r'page_params.video_title = \'(.+?)\';', webpage, 'video URL', fatal=False)
52         video_description = self._html_search_meta('description', webpage, 'video DESC', fatal=False)
53         video_thumbnail = self._html_search_regex(r'page_params.imageurl\t=\t"(.+?)";', webpage, 'video THUMB', fatal=False)
54         video_uploader = self._html_search_regex(r"<div class=\'videoInfoBy\'>By:</div>\n<a href=\"[^>]+\">(.+?)</a>", webpage, 'video UPLOADER', fatal=False)
55         video_date = self._html_search_regex(r"<div class='videoInfoTime'>\n<i class='icon-clock'></i> (.+?)\n</div>", webpage, 'video DATE', fatal=False)
56         video_date = datetime.datetime.strptime(video_date, '%B %d, %Y').strftime('%Y%m%d')
57
58         # Get all of the links from the page
59         DOWNLOAD_LIST_RE = r'(?s)sources: {\n(?P<download_list>.*?)}'
60         download_list_html = self._search_regex(DOWNLOAD_LIST_RE, webpage, 'download list').strip()
61         LINK_RE = r': \'(.+?)\','
62         links = re.findall(LINK_RE, download_list_html)
63
64         # Get all encrypted links
65         encrypted_links = re.findall(r'page_params.encryptedQuality[0-9]{3,4}URL\s=\s\'([a-zA-Z0-9+/]+={0,2})\';', webpage)
66         for encrypted_link in encrypted_links:
67             link = aes_decrypt_text(encrypted_link, video_title, 32).decode('utf-8')
68             # it's unclear if encryted links still differ from normal ones, so only include in links array if it's unique
69             if link not in links:
70                 links.append(link)
71
72         formats = []
73         for link in links:
74             # A link looks like this:
75             # 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
76             # A path looks like this:
77             # 201012/17/505835/720p_1500k_505835/YouPorn%20-%20Sex%20Ed%20Is%20It%20Safe%20To%20Masturbate%20Daily.mp4
78             video_url = unescapeHTML(link)
79             path = compat_urllib_parse_urlparse(video_url).path
80             format_parts = path.split('/')[4].split('_')[:2]
81
82             dn = compat_urllib_parse_urlparse(video_url).netloc.partition('.')[0]
83
84             resolution = format_parts[0]
85             height = int(resolution[:-len('p')])
86             bitrate = int(format_parts[1][:-len('k')])
87             format = '-'.join(format_parts) + '-' + dn
88
89             formats.append({
90                 'url': video_url,
91                 'format': format,
92                 'format_id': format,
93                 'height': height,
94                 'tbr': bitrate,
95                 'resolution': resolution,
96             })
97
98         self._sort_formats(formats)
99
100         if not formats:
101             raise ExtractorError('ERROR: no known formats available for video')
102
103         return {
104             'id': video_id,
105             'title': video_title,
106             'description': video_description,
107             'thumbnail': video_thumbnail,
108             'uploader': video_uploader,
109             'date': video_date,
110             'age_limit': age_limit,
111             'formats': formats,
112         }