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