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