[pornhd] Extract like count
[youtube-dl] / youtube_dl / extractor / pornhd.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     ExtractorError,
8     int_or_none,
9     js_to_json,
10 )
11
12
13 class PornHdIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:www\.)?pornhd\.com/(?:[a-z]{2,4}/)?videos/(?P<id>\d+)(?:/(?P<display_id>.+))?'
15     _TESTS = [{
16         'url': 'http://www.pornhd.com/videos/9864/selfie-restroom-masturbation-fun-with-chubby-cutie-hd-porn-video',
17         'md5': 'c8b964b1f0a4b5f7f28ae3a5c9f86ad5',
18         'info_dict': {
19             'id': '9864',
20             'display_id': 'selfie-restroom-masturbation-fun-with-chubby-cutie-hd-porn-video',
21             'ext': 'mp4',
22             'title': 'Restroom selfie masturbation',
23             'description': 'md5:3748420395e03e31ac96857a8f125b2b',
24             'thumbnail': r're:^https?://.*\.jpg',
25             'view_count': int,
26             'like_count': int,
27             'age_limit': 18,
28         }
29     }, {
30         # removed video
31         'url': 'http://www.pornhd.com/videos/1962/sierra-day-gets-his-cum-all-over-herself-hd-porn-video',
32         'md5': '956b8ca569f7f4d8ec563e2c41598441',
33         'info_dict': {
34             'id': '1962',
35             'display_id': 'sierra-day-gets-his-cum-all-over-herself-hd-porn-video',
36             'ext': 'mp4',
37             'title': 'Sierra loves doing laundry',
38             'description': 'md5:8ff0523848ac2b8f9b065ba781ccf294',
39             'thumbnail': r're:^https?://.*\.jpg',
40             'view_count': int,
41             'like_count': int,
42             'age_limit': 18,
43         },
44         'skip': 'Not available anymore',
45     }]
46
47     def _real_extract(self, url):
48         mobj = re.match(self._VALID_URL, url)
49         video_id = mobj.group('id')
50         display_id = mobj.group('display_id')
51
52         webpage = self._download_webpage(url, display_id or video_id)
53
54         title = self._html_search_regex(
55             [r'<span[^>]+class=["\']video-name["\'][^>]*>([^<]+)',
56              r'<title>(.+?) - .*?[Pp]ornHD.*?</title>'], webpage, 'title')
57
58         sources = self._parse_json(js_to_json(self._search_regex(
59             r"(?s)sources'?\s*[:=]\s*(\{.+?\})",
60             webpage, 'sources', default='{}')), video_id)
61
62         if not sources:
63             message = self._html_search_regex(
64                 r'(?s)<(div|p)[^>]+class="no-video"[^>]*>(?P<value>.+?)</\1',
65                 webpage, 'error message', group='value')
66             raise ExtractorError('%s said: %s' % (self.IE_NAME, message), expected=True)
67
68         formats = []
69         for format_id, video_url in sources.items():
70             if not video_url:
71                 continue
72             height = int_or_none(self._search_regex(
73                 r'^(\d+)[pP]', format_id, 'height', default=None))
74             formats.append({
75                 'url': video_url,
76                 'format_id': format_id,
77                 'height': height,
78             })
79         self._sort_formats(formats)
80
81         description = self._html_search_regex(
82             r'<(div|p)[^>]+class="description"[^>]*>(?P<value>[^<]+)</\1',
83             webpage, 'description', fatal=False, group='value')
84         view_count = int_or_none(self._html_search_regex(
85             r'(\d+) views\s*<', webpage, 'view count', fatal=False))
86         thumbnail = self._search_regex(
87             r"poster'?\s*:\s*([\"'])(?P<url>(?:(?!\1).)+)\1", webpage,
88             'thumbnail', fatal=False, group='url')
89
90         like_count = int_or_none(self._search_regex(
91             (r'(\d+)\s*</11[^>]+>(?:&nbsp;|\s)*\blikes',
92              r'class=["\']save-count["\'][^>]*>\s*(\d+)'),
93             webpage, 'like count', fatal=False))
94
95         return {
96             'id': video_id,
97             'display_id': display_id,
98             'title': title,
99             'description': description,
100             'thumbnail': thumbnail,
101             'view_count': view_count,
102             'like_count': like_count,
103             'formats': formats,
104             'age_limit': 18,
105         }