]> git.bitcoin.ninja Git - youtube-dl/blob - youporn.py
c85fd4b5af0ccdd3f259bd403ddd4311f2de5fdb
[youtube-dl] / 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         }
31     }
32
33     def _print_formats(self, formats):
34         """Print all available formats"""
35         print(u'Available formats:')
36         print(u'ext\t\tformat')
37         print(u'---------------------------------')
38         for format in formats:
39             print(u'%s\t\t%s'  % (format['ext'], format['format']))
40
41     def _specific(self, req_format, formats):
42         for x in formats:
43             if x["format"] == req_format:
44                 return x
45         return None
46
47     def _real_extract(self, url):
48         mobj = re.match(self._VALID_URL, url)
49         video_id = mobj.group('videoid')
50
51         req = compat_urllib_request.Request(url)
52         req.add_header('Cookie', 'age_verified=1')
53         webpage = self._download_webpage(req, video_id)
54
55         # Get JSON parameters
56         json_params = self._search_regex(r'var currentVideo = new Video\((.*)\);', webpage, u'JSON parameters')
57         try:
58             params = json.loads(json_params)
59         except:
60             raise ExtractorError(u'Invalid JSON')
61
62         self.report_extraction(video_id)
63         try:
64             video_title = params['title']
65             upload_date = unified_strdate(params['release_date_f'])
66             video_description = params['description']
67             video_uploader = params['submitted_by']
68             thumbnail = params['thumbnails'][0]['image']
69         except KeyError:
70             raise ExtractorError('Missing JSON parameter: ' + sys.exc_info()[1])
71
72         # Get all of the formats available
73         DOWNLOAD_LIST_RE = r'(?s)<ul class="downloadList">(?P<download_list>.*?)</ul>'
74         download_list_html = self._search_regex(DOWNLOAD_LIST_RE,
75             webpage, u'download list').strip()
76
77         # Get all of the links from the page
78         LINK_RE = r'(?s)<a href="(?P<url>[^"]+)">'
79         links = re.findall(LINK_RE, download_list_html)
80         
81         # Get link of hd video if available
82         mobj = re.search(r'var encryptedQuality720URL = \'(?P<encrypted_video_url>[a-zA-Z0-9+/]+={0,2})\';', webpage)
83         if mobj != None:
84             encrypted_video_url = mobj.group(u'encrypted_video_url')
85             video_url = aes_decrypt_text(encrypted_video_url, video_title, 32).decode('utf-8')
86             links = [video_url] + links
87         
88         if not links:
89             raise ExtractorError(u'ERROR: no known formats available for video')
90
91         self.to_screen(u'Links found: %d' % len(links))
92
93         formats = []
94         for link in links:
95
96             # A link looks like this:
97             # 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
98             # A path looks like this:
99             # /201210/31/8004515/480p_370k_8004515/YouPorn%20-%20Nubile%20Films%20The%20Pillow%20Fight.mp4
100             video_url = unescapeHTML( link )
101             path = compat_urllib_parse_urlparse( video_url ).path
102             extension = os.path.splitext( path )[1][1:]
103             format = path.split('/')[4].split('_')[:2]
104             # size = format[0]
105             # bitrate = format[1]
106             format = "-".join( format )
107             # title = u'%s-%s-%s' % (video_title, size, bitrate)
108
109             formats.append({
110                 'id': video_id,
111                 'url': video_url,
112                 'uploader': video_uploader,
113                 'upload_date': upload_date,
114                 'title': video_title,
115                 'ext': extension,
116                 'format': format,
117                 'thumbnail': thumbnail,
118                 'description': video_description
119             })
120
121         if self._downloader.params.get('listformats', None):
122             self._print_formats(formats)
123             return
124
125         req_format = self._downloader.params.get('format', 'best')
126         self.to_screen(u'Format: %s' % req_format)
127
128         if req_format is None or req_format == 'best':
129             return [formats[0]]
130         elif req_format == 'worst':
131             return [formats[-1]]
132         elif req_format in ('-1', 'all'):
133             return formats
134         else:
135             format = self._specific( req_format, formats )
136             if format is None:
137                 raise ExtractorError(u'Requested format not available')
138             return [format]