Add the missing age_limit tags; added a devscript to do a superficial check for porn...
[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             u"age_limit": 18
25         }
26     }
27
28     def _real_extract(self, url):
29         mobj = re.match(self._VALID_URL, url)
30         video_id = mobj.group('videoid')
31         url = 'http://www.' + mobj.group('url')
32
33         req = compat_urllib_request.Request(url)
34         req.add_header('Cookie', 'age_verified=1')
35         webpage = self._download_webpage(req, video_id)
36
37         video_title = self._html_search_regex(r'<h1 [^>]+>([^<]+)', webpage, u'title')
38         video_uploader = self._html_search_regex(r'<b>From: </b>(?:\s|<[^>]*>)*(.+?)<', webpage, u'uploader', fatal=False)
39         thumbnail = self._html_search_regex(r'"image_url":"([^"]+)', webpage, u'thumbnail', fatal=False)
40         if thumbnail:
41             thumbnail = compat_urllib_parse.unquote(thumbnail)
42
43         video_urls = list(map(compat_urllib_parse.unquote , re.findall(r'"quality_[0-9]{3}p":"([^"]+)', webpage)))
44         if webpage.find('"encrypted":true') != -1:
45             password = self._html_search_regex(r'"video_title":"([^"]+)', webpage, u'password').replace('+', ' ')
46             video_urls = list(map(lambda s: aes_decrypt_text(s, password, 32).decode('utf-8'), video_urls))
47
48         formats = []
49         for video_url in video_urls:
50             path = compat_urllib_parse_urlparse( video_url ).path
51             extension = os.path.splitext( path )[1][1:]
52             format = path.split('/')[5].split('_')[:2]
53             format = "-".join( format )
54             formats.append({
55                 'url': video_url,
56                 'ext': extension,
57                 'format': format,
58                 'format_id': format,
59             })
60         formats.sort(key=lambda format: list(map(lambda s: s.zfill(6), format['format'].split('-'))))
61
62         return {
63             'id': video_id,
64             'uploader': video_uploader,
65             'title': video_title,
66             'thumbnail': thumbnail,
67             'formats': formats,
68             'age_limit': 18,
69         }