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