[youporn] Fix JSON parameter regexp (Fixes #4384)
[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
8 from .common import InfoExtractor
9 from ..utils import (
10     compat_urllib_parse_urlparse,
11     compat_urllib_request,
12
13     ExtractorError,
14     unescapeHTML,
15     unified_strdate,
16 )
17 from ..aes import (
18     aes_decrypt_text
19 )
20
21
22 class YouPornIE(InfoExtractor):
23     _VALID_URL = r'^(?P<proto>https?://)(?:www\.)?(?P<url>youporn\.com/watch/(?P<videoid>[0-9]+)/(?P<title>[^/]+))'
24     _TEST = {
25         'url': 'http://www.youporn.com/watch/505835/sex-ed-is-it-safe-to-masturbate-daily/',
26         'info_dict': {
27             'id': '505835',
28             'ext': 'mp4',
29             'upload_date': '20101221',
30             'description': 'Love & Sex Answers: http://bit.ly/DanAndJenn -- Is It Unhealthy To Masturbate Daily?',
31             'uploader': 'Ask Dan And Jennifer',
32             'title': 'Sex Ed: Is It Safe To Masturbate Daily?',
33             'age_limit': 18,
34         }
35     }
36
37     def _real_extract(self, url):
38         mobj = re.match(self._VALID_URL, url)
39         video_id = mobj.group('videoid')
40         url = mobj.group('proto') + 'www.' + mobj.group('url')
41
42         req = compat_urllib_request.Request(url)
43         req.add_header('Cookie', 'age_verified=1')
44         webpage = self._download_webpage(req, video_id)
45         age_limit = self._rta_search(webpage)
46
47         # Get JSON parameters
48         json_params = self._search_regex(
49             r'var currentVideo = new Video\((.*)\)[,;]',
50             webpage, 'JSON parameters')
51         try:
52             params = json.loads(json_params)
53         except:
54             raise ExtractorError('Invalid JSON')
55
56         self.report_extraction(video_id)
57         try:
58             video_title = params['title']
59             upload_date = unified_strdate(params['release_date_f'])
60             video_description = params['description']
61             video_uploader = params['submitted_by']
62             thumbnail = params['thumbnails'][0]['image']
63         except KeyError:
64             raise ExtractorError('Missing JSON parameter: ' + sys.exc_info()[1])
65
66         # Get all of the links from the page
67         DOWNLOAD_LIST_RE = r'(?s)<ul class="downloadList">(?P<download_list>.*?)</ul>'
68         download_list_html = self._search_regex(DOWNLOAD_LIST_RE,
69                                                 webpage, 'download list').strip()
70         LINK_RE = r'<a href="([^"]+)">'
71         links = re.findall(LINK_RE, download_list_html)
72
73         # Get all encrypted links
74         encrypted_links = re.findall(r'var encryptedQuality[0-9]{3}URL = \'([a-zA-Z0-9+/]+={0,2})\';', webpage)
75         for encrypted_link in encrypted_links:
76             link = aes_decrypt_text(encrypted_link, video_title, 32).decode('utf-8')
77             links.append(link)
78
79         formats = []
80         for link in links:
81             # A link looks like this:
82             # http://cdn1.download.youporn.phncdn.com/201210/31/8004515/480p_370k_8004515/YouPorn%20-%20Nubile%20Films%20The%20Pillow%20Fight.mp4?nvb=20121113051249&nva=20121114051249&ir=1200&sr=1200&hash=014b882080310e95fb6a0
83             # A path looks like this:
84             # /201210/31/8004515/480p_370k_8004515/YouPorn%20-%20Nubile%20Films%20The%20Pillow%20Fight.mp4
85             video_url = unescapeHTML(link)
86             path = compat_urllib_parse_urlparse(video_url).path
87             format_parts = path.split('/')[4].split('_')[:2]
88
89             dn = compat_urllib_parse_urlparse(video_url).netloc.partition('.')[0]
90
91             resolution = format_parts[0]
92             height = int(resolution[:-len('p')])
93             bitrate = int(format_parts[1][:-len('k')])
94             format = '-'.join(format_parts) + '-' + dn
95
96             formats.append({
97                 'url': video_url,
98                 'format': format,
99                 'format_id': format,
100                 'height': height,
101                 'tbr': bitrate,
102                 'resolution': resolution,
103             })
104
105         self._sort_formats(formats)
106
107         if not formats:
108             raise ExtractorError('ERROR: no known formats available for video')
109
110         return {
111             'id': video_id,
112             'uploader': video_uploader,
113             'upload_date': upload_date,
114             'title': video_title,
115             'thumbnail': thumbnail,
116             'description': video_description,
117             'age_limit': age_limit,
118             'formats': formats,
119         }