[pornhd] Adapt to new sources scheme (Fixes #4446)
[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         sources = json.loads(js_to_json(self._search_regex(
50             r"(?s)'sources'\s*:\s*(\{.+?\})\s*\}\);", webpage, 'sources')))
51         formats = []
52         for container, s in sources.items():
53             for qname, video_url in s.items():
54                 formats.append({
55                     'url': video_url,
56                     'container': container,
57                     'format_id': '%s-%s' % (container, qname),
58                     'quality': quality(qname),
59                 })
60         self._sort_formats(formats)
61
62         return {
63             'id': video_id,
64             'display_id': display_id,
65             'title': title,
66             'description': description,
67             'thumbnail': thumbnail,
68             'view_count': view_count,
69             'formats': formats,
70             'age_limit': 18,
71         }