3dbd2ab699e31ab27c2e191ef757ac5e70eff940
[youtube-dl] / youtube_dl / extractor / pornhub.py
1 import os
2 import re
3
4 from .common import InfoExtractor
5 from ..utils import (
6     compat_urllib_parse_urlparse,
7     compat_urllib_request,
8     compat_urllib_parse,
9     unescapeHTML,
10 )
11 from ..aes import (
12     aes_decrypt_text
13 )
14
15 class PornHubIE(InfoExtractor):
16     _VALID_URL = r'^(?:https?://)?(?:www\.)?(?P<url>pornhub\.com/view_video\.php\?viewkey=(?P<videoid>[0-9]+))'
17     _TEST = {
18         u'url': u'http://www.pornhub.com/view_video.php?viewkey=648719015',
19         u'file': u'648719015.mp4',
20         u'md5': u'882f488fa1f0026f023f33576004a2ed',
21         u'info_dict': {
22             u"uploader": u"BABES-COM", 
23             u"title": u"Seductive Indian beauty strips down and fingers her pink pussy",
24         }
25     }
26
27     def _real_extract(self, url):
28         mobj = re.match(self._VALID_URL, url)
29         video_id = mobj.group('videoid')
30         url = 'http://www.' + mobj.group('url')
31
32         req = compat_urllib_request.Request(url)
33         req.add_header('Cookie', 'age_verified=1')
34         webpage = self._download_webpage(req, video_id)
35
36         video_title = self._html_search_regex(r'<h1 [^>]+>([^<]+)', webpage, u'title')
37         video_uploader = self._html_search_regex(r'<b>From: </b>(?:\s|<[^>]*>)*(.+?)<', webpage, u'uploader', fatal=False)
38         thumbnail = self._html_search_regex(r'"image_url":"([^"]+)', webpage, u'thumbnail', fatal=False)
39         if thumbnail:
40             thumbnail = compat_urllib_parse.unquote(thumbnail)
41
42         video_urls = list(map(compat_urllib_parse.unquote , re.findall(r'"quality_[0-9]{3}p":"([^"]+)', webpage)))
43         if webpage.find('"encrypted":true') != -1:
44             password = self._html_search_regex(r'"video_title":"([^"]+)', webpage, u'password').replace('+', ' ')
45             video_urls = list(map(lambda s: aes_decrypt_text(s, password, 32).decode('utf-8'), video_urls))
46
47         formats = []
48         for video_url in video_urls:
49             path = compat_urllib_parse_urlparse( video_url ).path
50             extension = os.path.splitext( path )[1][1:]
51             format = path.split('/')[5].split('_')[:2]
52             format = "-".join( format )
53             formats.append({
54                 'url': video_url,
55                 'ext': extension,
56                 'format': format,
57                 'format_id': format,
58             })
59         formats.sort(key=lambda format: list(map(lambda s: s.zfill(6), format['format'].split('-'))))
60
61         return {
62             'id': video_id,
63             'uploader': video_uploader,
64             'title': video_title,
65             'thumbnail': thumbnail,
66             'formats': formats,
67         }