]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/pornhub.py
Remove unused imports and clarify variable names
[yt-dlp.git] / youtube_dl / extractor / pornhub.py
CommitLineData
9933b574
PH
1from __future__ import unicode_literals
2
125cfd78 3import os
4import re
5
6from .common import InfoExtractor
7from ..utils import (
8 compat_urllib_parse_urlparse,
9 compat_urllib_request,
10 compat_urllib_parse,
125cfd78 11)
12from ..aes import (
13 aes_decrypt_text
14)
15
9933b574 16
125cfd78 17class PornHubIE(InfoExtractor):
00381b4c 18 _VALID_URL = r'^(?:https?://)?(?:www\.)?(?P<url>pornhub\.com/view_video\.php\?viewkey=(?P<videoid>[0-9a-f]+))'
125cfd78 19 _TEST = {
9933b574
PH
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
125cfd78 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
9933b574
PH
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)
125cfd78 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:
ee95c093 47 password = compat_urllib_parse.unquote_plus(self._html_search_regex(r'"video_title":"([^"]+)', webpage, 'password'))
125cfd78 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:
a56f9de1
JMF
52 path = compat_urllib_parse_urlparse(video_url).path
53 extension = os.path.splitext(path)[1][1:]
125cfd78 54 format = path.split('/')[5].split('_')[:2]
a56f9de1 55 format = "-".join(format)
9933b574
PH
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
125cfd78 65 formats.append({
66 'url': video_url,
67 'ext': extension,
68 'format': format,
69 'format_id': format,
9933b574
PH
70 'tbr': tbr,
71 'height': height,
125cfd78 72 })
9933b574 73 self._sort_formats(formats)
125cfd78 74
75 return {
76 'id': video_id,
77 'uploader': video_uploader,
78 'title': video_title,
79 'thumbnail': thumbnail,
80 'formats': formats,
750e9833 81 'age_limit': 18,
125cfd78 82 }