[pornhd] Detect removed videos and modernize
[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     _TEST = {
16         'url': 'http://www.pornhd.com/videos/1962/sierra-day-gets-his-cum-all-over-herself-hd-porn-video',
17         'md5': '956b8ca569f7f4d8ec563e2c41598441',
18         'info_dict': {
19             'id': '1962',
20             'display_id': 'sierra-day-gets-his-cum-all-over-herself-hd-porn-video',
21             'ext': 'mp4',
22             'title': 'Sierra loves doing laundry',
23             'description': 'md5:8ff0523848ac2b8f9b065ba781ccf294',
24             'thumbnail': 're:^https?://.*\.jpg',
25             'view_count': int,
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('id')
33         display_id = mobj.group('display_id')
34
35         webpage = self._download_webpage(url, display_id or video_id)
36
37         title = self._html_search_regex(
38             [r'<span[^>]+class=["\']video-name["\'][^>]*>([^<]+)',
39              r'<title>(.+?) - .*?[Pp]ornHD.*?</title>'], webpage, 'title')
40
41         sources = self._parse_json(js_to_json(self._search_regex(
42             r"(?s)'sources'\s*:\s*(\{.+?\})\s*\}[;,)]",
43             webpage, 'sources', default='{}')), video_id)
44
45         if not sources:
46             message = self._html_search_regex(
47                 r'(?s)<(div|p)[^>]+class="no-video"[^>]*>(?P<value>.+?)</\1',
48                 webpage, 'error message', group='value')
49             raise ExtractorError('%s said: %s' % (self.IE_NAME, message), expected=True)
50
51         formats = []
52         for format_id, video_url in sources.items():
53             if not video_url:
54                 continue
55             height = int_or_none(self._search_regex(
56                 r'^(\d+)[pP]', format_id, 'height', default=None))
57             formats.append({
58                 'url': video_url,
59                 'format_id': format_id,
60                 'height': height,
61             })
62         self._sort_formats(formats)
63
64         description = self._html_search_regex(
65             r'<(div|p)[^>]+class="description"[^>]*>(?P<value>[^<]+)</\1',
66             webpage, 'description', fatal=False, group='value')
67         view_count = int_or_none(self._html_search_regex(
68             r'(\d+) views\s*<', webpage, 'view count', fatal=False))
69         thumbnail = self._search_regex(
70             r"'poster'\s*:\s*'([^']+)'", webpage, 'thumbnail', fatal=False)
71
72         return {
73             'id': video_id,
74             'display_id': display_id,
75             'title': title,
76             'description': description,
77             'thumbnail': thumbnail,
78             'view_count': view_count,
79             'formats': formats,
80             'age_limit': 18,
81         }