Merge pull request #1632 from rzhxeo/cinemassacre
[youtube-dl] / youtube_dl / extractor / youporn.py
1 import json
2 import os
3 import re
4 import sys
5
6 from .common import InfoExtractor
7 from ..utils import (
8     compat_urllib_parse_urlparse,
9     compat_urllib_request,
10
11     ExtractorError,
12     unescapeHTML,
13     unified_strdate,
14 )
15 from ..aes import (
16     aes_decrypt_text
17 )
18
19 class YouPornIE(InfoExtractor):
20     _VALID_URL = r'^(?:https?://)?(?:\w+\.)?youporn\.com/watch/(?P<videoid>[0-9]+)/(?P<title>[^/]+)'
21     _TEST = {
22         u'url': u'http://www.youporn.com/watch/505835/sex-ed-is-it-safe-to-masturbate-daily/',
23         u'file': u'505835.mp4',
24         u'md5': u'71ec5fcfddacf80f495efa8b6a8d9a89',
25         u'info_dict': {
26             u"upload_date": u"20101221", 
27             u"description": u"Love & Sex Answers: http://bit.ly/DanAndJenn -- Is It Unhealthy To Masturbate Daily?", 
28             u"uploader": u"Ask Dan And Jennifer", 
29             u"title": u"Sex Ed: Is It Safe To Masturbate Daily?",
30             u"age_limit": 18,
31         }
32     }
33
34     def _print_formats(self, formats):
35         """Print all available formats"""
36         print(u'Available formats:')
37         print(u'ext\t\tformat')
38         print(u'---------------------------------')
39         for format in formats:
40             print(u'%s\t\t%s'  % (format['ext'], format['format']))
41
42     def _specific(self, req_format, formats):
43         for x in formats:
44             if x["format"] == req_format:
45                 return x
46         return None
47
48     def _real_extract(self, url):
49         mobj = re.match(self._VALID_URL, url)
50         video_id = mobj.group('videoid')
51
52         req = compat_urllib_request.Request(url)
53         req.add_header('Cookie', 'age_verified=1')
54         webpage = self._download_webpage(req, video_id)
55         age_limit = self._rta_search(webpage)
56
57         # Get JSON parameters
58         json_params = self._search_regex(r'var currentVideo = new Video\((.*)\);', webpage, u'JSON parameters')
59         try:
60             params = json.loads(json_params)
61         except:
62             raise ExtractorError(u'Invalid JSON')
63
64         self.report_extraction(video_id)
65         try:
66             video_title = params['title']
67             upload_date = unified_strdate(params['release_date_f'])
68             video_description = params['description']
69             video_uploader = params['submitted_by']
70             thumbnail = params['thumbnails'][0]['image']
71         except KeyError:
72             raise ExtractorError('Missing JSON parameter: ' + sys.exc_info()[1])
73
74         # Get all of the formats available
75         DOWNLOAD_LIST_RE = r'(?s)<ul class="downloadList">(?P<download_list>.*?)</ul>'
76         download_list_html = self._search_regex(DOWNLOAD_LIST_RE,
77             webpage, u'download list').strip()
78
79         # Get all of the links from the page
80         LINK_RE = r'(?s)<a href="(?P<url>[^"]+)">'
81         links = re.findall(LINK_RE, download_list_html)
82         
83         # Get link of hd video if available
84         mobj = re.search(r'var encryptedQuality720URL = \'(?P<encrypted_video_url>[a-zA-Z0-9+/]+={0,2})\';', webpage)
85         if mobj != None:
86             encrypted_video_url = mobj.group(u'encrypted_video_url')
87             video_url = aes_decrypt_text(encrypted_video_url, video_title, 32).decode('utf-8')
88             links = [video_url] + links
89         
90         if not links:
91             raise ExtractorError(u'ERROR: no known formats available for video')
92
93         self.to_screen(u'Links found: %d' % len(links))
94
95         formats = []
96         for link in links:
97
98             # A link looks like this:
99             # 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
100             # A path looks like this:
101             # /201210/31/8004515/480p_370k_8004515/YouPorn%20-%20Nubile%20Films%20The%20Pillow%20Fight.mp4
102             video_url = unescapeHTML( link )
103             path = compat_urllib_parse_urlparse( video_url ).path
104             extension = os.path.splitext( path )[1][1:]
105             format = path.split('/')[4].split('_')[:2]
106             # size = format[0]
107             # bitrate = format[1]
108             format = "-".join( format )
109             # title = u'%s-%s-%s' % (video_title, size, bitrate)
110
111             formats.append({
112                 'id': video_id,
113                 'url': video_url,
114                 'uploader': video_uploader,
115                 'upload_date': upload_date,
116                 'title': video_title,
117                 'ext': extension,
118                 'format': format,
119                 'thumbnail': thumbnail,
120                 'description': video_description,
121                 'age_limit': age_limit,
122             })
123
124         if self._downloader.params.get('listformats', None):
125             self._print_formats(formats)
126             return
127
128         req_format = self._downloader.params.get('format', 'best')
129         self.to_screen(u'Format: %s' % req_format)
130
131         if req_format is None or req_format == 'best':
132             return [formats[0]]
133         elif req_format == 'worst':
134             return [formats[-1]]
135         elif req_format in ('-1', 'all'):
136             return formats
137         else:
138             format = self._specific( req_format, formats )
139             if format is None:
140                 raise ExtractorError(u'Requested format not available')
141             return [format]