Merge remote-tracking branch '5moufl/behindkink'
[youtube-dl] / youtube_dl / extractor / pornhd.py
1 from __future__ import unicode_literals
2
3 import re
4 import json
5
6 from .common import InfoExtractor
7 from ..utils import int_or_none
8
9
10 class PornHdIE(InfoExtractor):
11     _VALID_URL = r'http://(?:www\.)?pornhd\.com/(?:[a-z]{2,4}/)?videos/(?P<id>\d+)'
12     _TEST = {
13         'url': 'http://www.pornhd.com/videos/1962/sierra-day-gets-his-cum-all-over-herself-hd-porn-video',
14         'md5': '956b8ca569f7f4d8ec563e2c41598441',
15         'info_dict': {
16             'id': '1962',
17             'ext': 'mp4',
18             'title': 'Sierra loves doing laundry',
19             'description': 'md5:8ff0523848ac2b8f9b065ba781ccf294',
20             'age_limit': 18,
21         }
22     }
23
24     def _real_extract(self, url):
25         mobj = re.match(self._VALID_URL, url)
26         video_id = mobj.group('id')
27
28         webpage = self._download_webpage(url, video_id)
29
30         title = self._html_search_regex(
31             r'<title>(.+) porn HD.+?</title>', webpage, 'title')
32         description = self._html_search_regex(
33             r'<div class="description">([^<]+)</div>', webpage, 'description', fatal=False)
34         view_count = int_or_none(self._html_search_regex(
35             r'(\d+) views\s*</span>', webpage, 'view count', fatal=False))
36
37         videos = re.findall(
38             r'var __video([\da-zA-Z]+?)(Low|High)StreamUrl = \'(http://.+?)\?noProxy=1\'', webpage)
39
40         mobj = re.search(r'flashVars = (?P<flashvars>{.+?});', webpage)
41         if mobj:
42             flashvars = json.loads(mobj.group('flashvars'))
43             for key, quality in [('hashlink', 'low'), ('hd', 'high')]:
44                 redirect_url = flashvars.get(key)
45                 if redirect_url:
46                     videos.append(('flv', quality, redirect_url))
47             thumbnail = flashvars['urlWallpaper']
48         else:
49             thumbnail = self._og_search_thumbnail(webpage)
50
51         formats = []
52         for format_, quality, redirect_url in videos:
53             format_id = '%s-%s' % (format_.lower(), quality.lower())
54             video_url = self._download_webpage(
55                 redirect_url, video_id, 'Downloading %s video link' % format_id, fatal=False)
56             if not video_url:
57                 continue
58             formats.append({
59                 'url': video_url,
60                 'ext': format_.lower(),
61                 'format_id': format_id,
62                 'quality': 1 if quality.lower() == 'high' else 0,
63             })
64         self._sort_formats(formats)
65
66         return {
67             'id': video_id,
68             'title': title,
69             'description': description,
70             'thumbnail': thumbnail,
71             'view_count': view_count,
72             'formats': formats,
73             'age_limit': 18,
74         }