[youtube] fix extraction for embed restricted live streams(fixes #16433)
[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_str
7 from ..utils import (
8     int_or_none,
9     sanitized_Request,
10     str_to_int,
11     unescapeHTML,
12     unified_strdate,
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._search_regex(
72             [r'(?:video_titles|videoTitle)\s*[:=]\s*(["\'])(?P<title>(?:(?!\1).)+)\1',
73              r'<h1[^>]+class=["\']heading\d?["\'][^>]*>(?P<title>[^<]+)<'],
74             webpage, 'title', group='title',
75             default=None) or self._og_search_title(
76             webpage, default=None) or self._html_search_meta(
77             'title', webpage, fatal=True)
78
79         links = []
80
81         # Main source
82         definitions = self._parse_json(
83             self._search_regex(
84                 r'mediaDefinition\s*=\s*(\[.+?\]);', webpage,
85                 'media definitions', default='[]'),
86             video_id, fatal=False)
87         if definitions:
88             for definition in definitions:
89                 if not isinstance(definition, dict):
90                     continue
91                 video_url = definition.get('videoUrl')
92                 if isinstance(video_url, compat_str) and video_url:
93                     links.append(video_url)
94
95         # Fallback #1, this also contains extra low quality 180p format
96         for _, link in re.findall(r'<a[^>]+href=(["\'])(http.+?)\1[^>]+title=["\']Download [Vv]ideo', webpage):
97             links.append(link)
98
99         # Fallback #2 (unavailable as at 22.06.2017)
100         sources = self._search_regex(
101             r'(?s)sources\s*:\s*({.+?})', webpage, 'sources', default=None)
102         if sources:
103             for _, link in re.findall(r'[^:]+\s*:\s*(["\'])(http.+?)\1', sources):
104                 links.append(link)
105
106         # Fallback #3 (unavailable as at 22.06.2017)
107         for _, link in re.findall(
108                 r'(?:videoSrc|videoIpadUrl|html5PlayerSrc)\s*[:=]\s*(["\'])(http.+?)\1', webpage):
109             links.append(link)
110
111         # Fallback #4, encrypted links (unavailable as at 22.06.2017)
112         for _, encrypted_link in re.findall(
113                 r'encryptedQuality\d{3,4}URL\s*=\s*(["\'])([\da-zA-Z+/=]+)\1', webpage):
114             links.append(aes_decrypt_text(encrypted_link, title, 32).decode('utf-8'))
115
116         formats = []
117         for video_url in set(unescapeHTML(link) for link in links):
118             f = {
119                 'url': video_url,
120             }
121             # Video URL's path looks like this:
122             #  /201012/17/505835/720p_1500k_505835/YouPorn%20-%20Sex%20Ed%20Is%20It%20Safe%20To%20Masturbate%20Daily.mp4
123             #  /201012/17/505835/vl_240p_240k_505835/YouPorn%20-%20Sex%20Ed%20Is%20It%20Safe%20To%20Masturbate%20Daily.mp4
124             # We will benefit from it by extracting some metadata
125             mobj = re.search(r'(?P<height>\d{3,4})[pP]_(?P<bitrate>\d+)[kK]_\d+/', video_url)
126             if mobj:
127                 height = int(mobj.group('height'))
128                 bitrate = int(mobj.group('bitrate'))
129                 f.update({
130                     'format_id': '%dp-%dk' % (height, bitrate),
131                     'height': height,
132                     'tbr': bitrate,
133                 })
134             formats.append(f)
135         self._sort_formats(formats)
136
137         description = self._og_search_description(webpage, default=None)
138         thumbnail = self._search_regex(
139             r'(?:imageurl\s*=|poster\s*:)\s*(["\'])(?P<thumbnail>.+?)\1',
140             webpage, 'thumbnail', fatal=False, group='thumbnail')
141
142         uploader = self._html_search_regex(
143             r'(?s)<div[^>]+class=["\']submitByLink["\'][^>]*>(.+?)</div>',
144             webpage, 'uploader', fatal=False)
145         upload_date = unified_strdate(self._html_search_regex(
146             [r'Date\s+[Aa]dded:\s*<span>([^<]+)',
147              r'(?s)<div[^>]+class=["\']videoInfo(?:Date|Time)["\'][^>]*>(.+?)</div>'],
148             webpage, 'upload date', fatal=False))
149
150         age_limit = self._rta_search(webpage)
151
152         average_rating = int_or_none(self._search_regex(
153             r'<div[^>]+class=["\']videoRatingPercentage["\'][^>]*>(\d+)%</div>',
154             webpage, 'average rating', fatal=False))
155
156         view_count = str_to_int(self._search_regex(
157             r'(?s)<div[^>]+class=(["\']).*?\bvideoInfoViews\b.*?\1[^>]*>.*?(?P<count>[\d,.]+)<',
158             webpage, 'view count', fatal=False, group='count'))
159         comment_count = str_to_int(self._search_regex(
160             r'>All [Cc]omments? \(([\d,.]+)\)',
161             webpage, 'comment count', fatal=False))
162
163         def extract_tag_box(regex, title):
164             tag_box = self._search_regex(regex, webpage, title, default=None)
165             if not tag_box:
166                 return []
167             return re.findall(r'<a[^>]+href=[^>]+>([^<]+)', tag_box)
168
169         categories = extract_tag_box(
170             r'(?s)Categories:.*?</[^>]+>(.+?)</div>', 'categories')
171         tags = extract_tag_box(
172             r'(?s)Tags:.*?</div>\s*<div[^>]+class=["\']tagBoxContent["\'][^>]*>(.+?)</div>',
173             'tags')
174
175         return {
176             'id': video_id,
177             'display_id': display_id,
178             'title': title,
179             'description': description,
180             'thumbnail': thumbnail,
181             'uploader': uploader,
182             'upload_date': upload_date,
183             'average_rating': average_rating,
184             'view_count': view_count,
185             'comment_count': comment_count,
186             'categories': categories,
187             'tags': tags,
188             'age_limit': age_limit,
189             'formats': formats,
190         }