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