[pornhd] Fix metadata extraction
[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 (
8     int_or_none,
9     js_to_json,
10     qualities,
11 )
12
13
14 class PornHdIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:www\.)?pornhd\.com/(?:[a-z]{2,4}/)?videos/(?P<id>\d+)(?:/(?P<display_id>.+))?'
16     _TEST = {
17         'url': 'http://www.pornhd.com/videos/1962/sierra-day-gets-his-cum-all-over-herself-hd-porn-video',
18         'md5': '956b8ca569f7f4d8ec563e2c41598441',
19         'info_dict': {
20             'id': '1962',
21             'display_id': 'sierra-day-gets-his-cum-all-over-herself-hd-porn-video',
22             'ext': 'mp4',
23             'title': 'Sierra loves doing laundry',
24             'description': 'md5:8ff0523848ac2b8f9b065ba781ccf294',
25             'thumbnail': 're:^https?://.*\.jpg',
26             'view_count': int,
27             'age_limit': 18,
28         }
29     }
30
31     def _real_extract(self, url):
32         mobj = re.match(self._VALID_URL, url)
33         video_id = mobj.group('id')
34         display_id = mobj.group('display_id')
35
36         webpage = self._download_webpage(url, display_id or video_id)
37
38         title = self._html_search_regex(
39             [r'<span[^>]+class=["\']video-name["\'][^>]*>([^<]+)',
40              r'<title>(.+?) - .*?[Pp]ornHD.*?</title>'], webpage, 'title')
41         description = self._html_search_regex(
42             r'<(div|p)[^>]+class="description"[^>]*>(?P<value>[^<]+)</\1',
43             webpage, 'description', fatal=False, group='value')
44         view_count = int_or_none(self._html_search_regex(
45             r'(\d+) views\s*<', webpage, 'view count', fatal=False))
46         thumbnail = self._search_regex(
47             r"'poster'\s*:\s*'([^']+)'", webpage, 'thumbnail', fatal=False)
48
49         quality = qualities(['sd', 'hd'])
50         sources = json.loads(js_to_json(self._search_regex(
51             r"(?s)'sources'\s*:\s*(\{.+?\})\s*\}[;,)]",
52             webpage, 'sources')))
53         formats = []
54         for qname, video_url in sources.items():
55             if not video_url:
56                 continue
57             formats.append({
58                 'url': video_url,
59                 'format_id': qname,
60                 'quality': quality(qname),
61             })
62         self._sort_formats(formats)
63
64         return {
65             'id': video_id,
66             'display_id': display_id,
67             'title': title,
68             'description': description,
69             'thumbnail': thumbnail,
70             'view_count': view_count,
71             'formats': formats,
72             'age_limit': 18,
73         }