[youporn] Fix title extraction (closes #13456)
[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 )
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': '3744d24c50438cf5b6f6d59feb5055c2',
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': r're:^https?://.*\.jpg$',
28             'uploader': 'Ask Dan And Jennifer',
29             'upload_date': '20101221',
30             'average_rating': int,
31             'view_count': int,
32             'comment_count': int,
33             'categories': list,
34             'tags': list,
35             'age_limit': 18,
36         },
37     }, {
38         # Unknown uploader
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',
46             'thumbnail': r're:^https?://.*\.jpg$',
47             'uploader': 'Unknown',
48             'upload_date': '20111125',
49             'average_rating': int,
50             'view_count': int,
51             'comment_count': int,
52             'categories': list,
53             'tags': list,
54             'age_limit': 18,
55         },
56         'params': {
57             'skip_download': True,
58         },
59     }]
60
61     def _real_extract(self, url):
62         mobj = re.match(self._VALID_URL, url)
63         video_id = mobj.group('id')
64         display_id = mobj.group('display_id')
65
66         request = sanitized_Request(url)
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|title)\s*[:=]\s*(["\'])(?P<title>(?:(?!\1).)+)\1',
72              r'<h1[^>]+class=["\']heading\d?["\'][^>]*>(?P<title>[^<]+)<'],
73             webpage, 'title', group='title',
74             default=None) or self._og_search_title(
75             webpage, default=None) or self._html_search_meta(
76             'title', webpage, fatal=True)
77
78         links = []
79
80         sources = self._search_regex(
81             r'(?s)sources\s*:\s*({.+?})', webpage, 'sources', default=None)
82         if sources:
83             for _, link in re.findall(r'[^:]+\s*:\s*(["\'])(http.+?)\1', sources):
84                 links.append(link)
85
86         # Fallback #1
87         for _, link in re.findall(
88                 r'(?:videoUrl|videoSrc|videoIpadUrl|html5PlayerSrc)\s*[:=]\s*(["\'])(http.+?)\1', webpage):
89             links.append(link)
90
91         # Fallback #2, this also contains extra low quality 180p format
92         for _, link in re.findall(r'<a[^>]+href=(["\'])(http.+?)\1[^>]+title=["\']Download [Vv]ideo', webpage):
93             links.append(link)
94
95         # Fallback #3, encrypted links
96         for _, encrypted_link in re.findall(
97                 r'encryptedQuality\d{3,4}URL\s*=\s*(["\'])([\da-zA-Z+/=]+)\1', webpage):
98             links.append(aes_decrypt_text(encrypted_link, title, 32).decode('utf-8'))
99
100         formats = []
101         for video_url in set(unescapeHTML(link) for link in links):
102             f = {
103                 'url': video_url,
104             }
105             # Video URL's path looks like this:
106             #  /201012/17/505835/720p_1500k_505835/YouPorn%20-%20Sex%20Ed%20Is%20It%20Safe%20To%20Masturbate%20Daily.mp4
107             #  /201012/17/505835/vl_240p_240k_505835/YouPorn%20-%20Sex%20Ed%20Is%20It%20Safe%20To%20Masturbate%20Daily.mp4
108             # We will benefit from it by extracting some metadata
109             mobj = re.search(r'(?P<height>\d{3,4})[pP]_(?P<bitrate>\d+)[kK]_\d+/', video_url)
110             if mobj:
111                 height = int(mobj.group('height'))
112                 bitrate = int(mobj.group('bitrate'))
113                 f.update({
114                     'format_id': '%dp-%dk' % (height, bitrate),
115                     'height': height,
116                     'tbr': bitrate,
117                 })
118             formats.append(f)
119         self._sort_formats(formats)
120
121         description = self._og_search_description(webpage, default=None)
122         thumbnail = self._search_regex(
123             r'(?:imageurl\s*=|poster\s*:)\s*(["\'])(?P<thumbnail>.+?)\1',
124             webpage, 'thumbnail', fatal=False, group='thumbnail')
125
126         uploader = self._html_search_regex(
127             r'(?s)<div[^>]+class=["\']submitByLink["\'][^>]*>(.+?)</div>',
128             webpage, 'uploader', fatal=False)
129         upload_date = unified_strdate(self._html_search_regex(
130             r'(?s)<div[^>]+class=["\']videoInfo(?:Date|Time)["\'][^>]*>(.+?)</div>',
131             webpage, 'upload date', fatal=False))
132
133         age_limit = self._rta_search(webpage)
134
135         average_rating = int_or_none(self._search_regex(
136             r'<div[^>]+class=["\']videoRatingPercentage["\'][^>]*>(\d+)%</div>',
137             webpage, 'average rating', fatal=False))
138
139         view_count = str_to_int(self._search_regex(
140             r'(?s)<div[^>]+class=(["\']).*?\bvideoInfoViews\b.*?\1[^>]*>.*?(?P<count>[\d,.]+)<',
141             webpage, 'view count', fatal=False, group='count'))
142         comment_count = str_to_int(self._search_regex(
143             r'>All [Cc]omments? \(([\d,.]+)\)',
144             webpage, 'comment count', fatal=False))
145
146         def extract_tag_box(regex, title):
147             tag_box = self._search_regex(regex, webpage, title, default=None)
148             if not tag_box:
149                 return []
150             return re.findall(r'<a[^>]+href=[^>]+>([^<]+)', tag_box)
151
152         categories = extract_tag_box(
153             r'(?s)Categories:.*?</[^>]+>(.+?)</div>', 'categories')
154         tags = extract_tag_box(
155             r'(?s)Tags:.*?</div>\s*<div[^>]+class=["\']tagBoxContent["\'][^>]*>(.+?)</div>',
156             'tags')
157
158         return {
159             'id': video_id,
160             'display_id': display_id,
161             'title': title,
162             'description': description,
163             'thumbnail': thumbnail,
164             'uploader': uploader,
165             'upload_date': upload_date,
166             'average_rating': average_rating,
167             'view_count': view_count,
168             'comment_count': comment_count,
169             'categories': categories,
170             'tags': tags,
171             'age_limit': age_limit,
172             'formats': formats,
173         }