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