Merge pull request #14225 from Tithen-Firion/openload-phantomjs-method
[youtube-dl] / youtube_dl / extractor / drtuber.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     NO_DEFAULT,
8     str_to_int,
9 )
10
11
12 class DrTuberIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:www\.)?drtuber\.com/(?:video|embed)/(?P<id>\d+)(?:/(?P<display_id>[\w-]+))?'
14     _TESTS = [{
15         'url': 'http://www.drtuber.com/video/1740434/hot-perky-blonde-naked-golf',
16         'md5': '93e680cf2536ad0dfb7e74d94a89facd',
17         'info_dict': {
18             'id': '1740434',
19             'display_id': 'hot-perky-blonde-naked-golf',
20             'ext': 'mp4',
21             'title': 'hot perky blonde naked golf',
22             'like_count': int,
23             'comment_count': int,
24             'categories': ['Babe', 'Blonde', 'Erotic', 'Outdoor', 'Softcore', 'Solo'],
25             'thumbnail': r're:https?://.*\.jpg$',
26             'age_limit': 18,
27         }
28     }, {
29         'url': 'http://www.drtuber.com/embed/489939',
30         'only_matching': True,
31     }]
32
33     @staticmethod
34     def _extract_urls(webpage):
35         return re.findall(
36             r'<iframe[^>]+?src=["\'](?P<url>(?:https?:)?//(?:www\.)?drtuber\.com/embed/\d+)',
37             webpage)
38
39     def _real_extract(self, url):
40         mobj = re.match(self._VALID_URL, url)
41         video_id = mobj.group('id')
42         display_id = mobj.group('display_id') or video_id
43
44         webpage = self._download_webpage(
45             'http://www.drtuber.com/video/%s' % video_id, display_id)
46
47         video_data = self._download_json(
48             'http://www.drtuber.com/player_config_json/', video_id, query={
49                 'vid': video_id,
50                 'embed': 0,
51                 'aid': 0,
52                 'domain_id': 0,
53             })
54
55         formats = []
56         for format_id, video_url in video_data['files'].items():
57             if video_url:
58                 formats.append({
59                     'format_id': format_id,
60                     'quality': 2 if format_id == 'hq' else 1,
61                     'url': video_url
62                 })
63         self._sort_formats(formats)
64
65         title = self._html_search_regex(
66             (r'class="title_watch"[^>]*><(?:p|h\d+)[^>]*>([^<]+)<',
67              r'<p[^>]+class="title_substrate">([^<]+)</p>',
68              r'<title>([^<]+) - \d+'),
69             webpage, 'title')
70
71         thumbnail = self._html_search_regex(
72             r'poster="([^"]+)"',
73             webpage, 'thumbnail', fatal=False)
74
75         def extract_count(id_, name, default=NO_DEFAULT):
76             return str_to_int(self._html_search_regex(
77                 r'<span[^>]+(?:class|id)="%s"[^>]*>([\d,\.]+)</span>' % id_,
78                 webpage, '%s count' % name, default=default, fatal=False))
79
80         like_count = extract_count('rate_likes', 'like')
81         dislike_count = extract_count('rate_dislikes', 'dislike', default=None)
82         comment_count = extract_count('comments_count', 'comment')
83
84         cats_str = self._search_regex(
85             r'<div[^>]+class="categories_list">(.+?)</div>',
86             webpage, 'categories', fatal=False)
87         categories = [] if not cats_str else re.findall(
88             r'<a title="([^"]+)"', cats_str)
89
90         return {
91             'id': video_id,
92             'display_id': display_id,
93             'formats': formats,
94             'title': title,
95             'thumbnail': thumbnail,
96             'like_count': like_count,
97             'dislike_count': dislike_count,
98             'comment_count': comment_count,
99             'categories': categories,
100             'age_limit': self._rta_search(webpage),
101         }