[youporn] Improve uploader extraction
[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 ..compat import compat_urllib_request
7 from ..utils import (
8     int_or_none,
9     str_to_int,
10     unescapeHTML,
11     unified_strdate,
12 )
13 from ..aes import aes_decrypt_text
14
15
16 class YouPornIE(InfoExtractor):
17     _VALID_URL = r'https?://(?:www\.)?youporn\.com/watch/(?P<id>\d+)/(?P<display_id>[^/?#&]+)'
18     _TESTS = [{
19         'url': 'http://www.youporn.com/watch/505835/sex-ed-is-it-safe-to-masturbate-daily/',
20         'md5': '71ec5fcfddacf80f495efa8b6a8d9a89',
21         'info_dict': {
22             'id': '505835',
23             'display_id': 'sex-ed-is-it-safe-to-masturbate-daily',
24             'ext': 'mp4',
25             'title': 'Sex Ed: Is It Safe To Masturbate Daily?',
26             'description': 'Love & Sex Answers: http://bit.ly/DanAndJenn -- Is It Unhealthy To Masturbate Daily?',
27             'thumbnail': 're:^https?://.*\.jpg$',
28             'uploader': 'Ask Dan And Jennifer',
29             'upload_date': '20101221',
30             'average_rating': int,
31             'view_count': int,
32             'categories': list,
33             'tags': list,
34             'age_limit': 18,
35         },
36     }, {
37         # Anonymous User uploader
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',
45             'thumbnail': 're:^https?://.*\.jpg$',
46             'uploader': 'Anonymous User',
47             'upload_date': '20111125',
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         },
57     }]
58
59     def _real_extract(self, url):
60         mobj = re.match(self._VALID_URL, url)
61         video_id = mobj.group('id')
62         display_id = mobj.group('display_id')
63
64         request = compat_urllib_request.Request(url)
65         request.add_header('Cookie', 'age_verified=1')
66         webpage = self._download_webpage(request, display_id)
67
68         title = self._search_regex(
69             [r'(?:video_titles|videoTitle)\s*[:=]\s*(["\'])(?P<title>.+?)\1',
70              r'<h1[^>]+class=["\']heading\d?["\'][^>]*>([^<])<'],
71             webpage, 'title', group='title')
72
73         links = []
74
75         sources = self._search_regex(
76             r'sources\s*:\s*({.+?})', webpage, 'sources', default=None)
77         if sources:
78             for _, link in re.findall(r'[^:]+\s*:\s*(["\'])(http.+?)\1', sources):
79                 links.append(link)
80
81         # Fallback #1
82         for _, link in re.findall(
83                 r'(?:videoUrl|videoSrc|videoIpadUrl|html5PlayerSrc)\s*[:=]\s*(["\'])(http.+?)\1', webpage):
84             links.append(link)
85
86         # Fallback #2, this also contains extra low quality 180p format
87         for _, link in re.findall(r'<a[^>]+href=(["\'])(http.+?)\1[^>]+title=["\']Download [Vv]ideo', webpage):
88             links.append(link)
89
90         # Fallback #3, encrypted links
91         for _, encrypted_link in re.findall(
92                 r'encryptedQuality\d{3,4}URL\s*=\s*(["\'])([\da-zA-Z+/=]+)\1', webpage):
93             links.append(aes_decrypt_text(encrypted_link, title, 32).decode('utf-8'))
94
95         formats = []
96         for video_url in set(unescapeHTML(link) for link in links):
97             f = {
98                 'url': video_url,
99             }
100             # Video URL's path looks like this:
101             #  /201012/17/505835/720p_1500k_505835/YouPorn%20-%20Sex%20Ed%20Is%20It%20Safe%20To%20Masturbate%20Daily.mp4
102             # We will benefit from it by extracting some metadata
103             mobj = re.search(r'/(?P<height>\d{3,4})[pP]_(?P<bitrate>\d+)[kK]_\d+/', video_url)
104             if mobj:
105                 height = int(mobj.group('height'))
106                 bitrate = int(mobj.group('bitrate'))
107                 f.update({
108                     'format_id': '%dp-%dk' % (height, bitrate),
109                     'height': height,
110                     'tbr': bitrate,
111                 })
112             formats.append(f)
113         self._sort_formats(formats)
114
115         description = self._html_search_regex(
116             r'(?s)<div[^>]+class=["\']video-description["\'][^>]*>(.+?)</div>',
117             webpage, 'description', default=None)
118         thumbnail = self._search_regex(
119             r'(?:imageurl\s*=|poster\s*:)\s*(["\'])(?P<thumbnail>.+?)\1',
120             webpage, 'thumbnail', fatal=False, group='thumbnail')
121
122         uploader = self._html_search_regex(
123             r'(?s)<div[^>]+class=["\']videoInfoBy["\'][^>]*>\s*By:\s*</div>(.+?)</(?:a|div)>',
124             webpage, 'uploader', fatal=False)
125         upload_date = unified_strdate(self._html_search_regex(
126             r'(?s)<div[^>]+class=["\']videoInfoTime["\'][^>]*>(.+?)</div>',
127             webpage, 'upload date', fatal=False))
128
129         age_limit = self._rta_search(webpage)
130
131         average_rating = int_or_none(self._search_regex(
132             r'<div[^>]+class=["\']videoInfoRating["\'][^>]*>\s*<div[^>]+class=["\']videoRatingPercentage["\'][^>]*>(\d+)%</div>',
133             webpage, 'average rating', fatal=False))
134
135         view_count = str_to_int(self._search_regex(
136             r'(?s)<div[^>]+class=["\']videoInfoViews["\'][^>]*>.*?([\d,.]+)\s*</div>',
137             webpage, 'view count', fatal=False))
138
139         def extract_tag_box(title):
140             tag_box = self._search_regex(
141                 (r'<div[^>]+class=["\']tagBoxTitle["\'][^>]*>\s*%s\b.*?</div>\s*'
142                  '<div[^>]+class=["\']tagBoxContent["\']>(.+?)</div>') % re.escape(title),
143                 webpage, '%s tag box' % title, default=None)
144             if not tag_box:
145                 return []
146             return re.findall(r'<a[^>]+href=[^>]+>([^<]+)', tag_box)
147
148         categories = extract_tag_box('Category')
149         tags = extract_tag_box('Tags')
150
151         return {
152             'id': video_id,
153             'display_id': display_id,
154             'title': title,
155             'description': description,
156             'thumbnail': thumbnail,
157             'uploader': uploader,
158             'upload_date': upload_date,
159             'average_rating': average_rating,
160             'view_count': view_count,
161             'categories': categories,
162             'tags': tags,
163             'age_limit': age_limit,
164             'formats': formats,
165         }