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