]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/pornhub.py
Merge branch 'ustream-embed-recorded2' of https://github.com/anovicecodemonkey/youtub...
[youtube-dl] / 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     str_to_int,
12 )
13 from ..aes import (
14     aes_decrypt_text
15 )
16
17
18 class PornHubIE(InfoExtractor):
19     _VALID_URL = r'^(?:https?://)?(?:www\.)?(?P<url>pornhub\.com/view_video\.php\?viewkey=(?P<videoid>[0-9a-f]+))'
20     _TEST = {
21         'url': 'http://www.pornhub.com/view_video.php?viewkey=648719015',
22         'file': '648719015.mp4',
23         'md5': '882f488fa1f0026f023f33576004a2ed',
24         'info_dict': {
25             "uploader": "BABES-COM",
26             "title": "Seductive Indian beauty strips down and fingers her pink pussy",
27             "age_limit": 18
28         }
29     }
30
31     def _extract_count(self, pattern, webpage, name):
32         count = self._html_search_regex(pattern, webpage, '%s count' % name, fatal=False)
33         if count:
34             count = str_to_int(count)
35         return count
36
37     def _real_extract(self, url):
38         mobj = re.match(self._VALID_URL, url)
39         video_id = mobj.group('videoid')
40         url = 'http://www.' + mobj.group('url')
41
42         req = compat_urllib_request.Request(url)
43         req.add_header('Cookie', 'age_verified=1')
44         webpage = self._download_webpage(req, video_id)
45
46         video_title = self._html_search_regex(r'<h1 [^>]+>([^<]+)', webpage, 'title')
47         video_uploader = self._html_search_regex(
48             r'(?s)From:&nbsp;.+?<(?:a href="/users/|<span class="username)[^>]+>(.+?)<',
49             webpage, 'uploader', fatal=False)
50         thumbnail = self._html_search_regex(r'"image_url":"([^"]+)', webpage, 'thumbnail', fatal=False)
51         if thumbnail:
52             thumbnail = compat_urllib_parse.unquote(thumbnail)
53
54         view_count = self._extract_count(r'<span class="count">([\d,\.]+)</span> views', webpage, 'view')
55         like_count = self._extract_count(r'<span class="votesUp">([\d,\.]+)</span>', webpage, 'like')
56         dislike_count = self._extract_count(r'<span class="votesDown">([\d,\.]+)</span>', webpage, 'dislike')
57         comment_count = self._extract_count(
58             r'All comments \(<var class="videoCommentCount">([\d,\.]+)</var>', webpage, 'comment')
59
60         video_urls = list(map(compat_urllib_parse.unquote , re.findall(r'"quality_[0-9]{3}p":"([^"]+)', webpage)))
61         if webpage.find('"encrypted":true') != -1:
62             password = compat_urllib_parse.unquote_plus(self._html_search_regex(r'"video_title":"([^"]+)', webpage, 'password'))
63             video_urls = list(map(lambda s: aes_decrypt_text(s, password, 32).decode('utf-8'), video_urls))
64
65         formats = []
66         for video_url in video_urls:
67             path = compat_urllib_parse_urlparse(video_url).path
68             extension = os.path.splitext(path)[1][1:]
69             format = path.split('/')[5].split('_')[:2]
70             format = "-".join(format)
71
72             m = re.match(r'^(?P<height>[0-9]+)P-(?P<tbr>[0-9]+)K$', format)
73             if m is None:
74                 height = None
75                 tbr = None
76             else:
77                 height = int(m.group('height'))
78                 tbr = int(m.group('tbr'))
79
80             formats.append({
81                 'url': video_url,
82                 'ext': extension,
83                 'format': format,
84                 'format_id': format,
85                 'tbr': tbr,
86                 'height': height,
87             })
88         self._sort_formats(formats)
89
90         return {
91             'id': video_id,
92             'uploader': video_uploader,
93             'title': video_title,
94             'thumbnail': thumbnail,
95             'view_count': view_count,
96             'like_count': like_count,
97             'dislike_count': dislike_count,
98             'comment_count': comment_count,
99             'formats': formats,
100             'age_limit': 18,
101         }