[extractor/generic] Add support for redtube embds (closes #11099)
[youtube-dl] / youtube_dl / extractor / redtube.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     str_to_int,
10     unified_strdate,
11 )
12
13
14 class RedTubeIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:(?:www\.)?redtube\.com/|embed\.redtube\.com/\?.*?\bid=)(?P<id>[0-9]+)'
16     _TESTS = [{
17         'url': 'http://www.redtube.com/66418',
18         'md5': '7b8c22b5e7098a3e1c09709df1126d2d',
19         'info_dict': {
20             'id': '66418',
21             'ext': 'mp4',
22             'title': 'Sucked on a toilet',
23             'upload_date': '20120831',
24             'duration': 596,
25             'view_count': int,
26             'age_limit': 18,
27         }
28     }, {
29         'url': 'http://embed.redtube.com/?bgcolor=000000&id=1443286',
30         'only_matching': True,
31     }]
32
33     @staticmethod
34     def _extract_urls(webpage):
35         return re.findall(
36             r'<iframe[^>]+?src=["\'](?P<url>(?:https?:)?//embed\.redtube\.com/\?.*?\bid=\d+)',
37             webpage)
38
39     def _real_extract(self, url):
40         video_id = self._match_id(url)
41         webpage = self._download_webpage(
42             'http://www.redtube.com/%s' % video_id, video_id)
43
44         if any(s in webpage for s in ['video-deleted-info', '>This video has been removed']):
45             raise ExtractorError('Video %s has been removed' % video_id, expected=True)
46
47         title = self._html_search_regex(
48             (r'<h1 class="videoTitle[^"]*">(?P<title>.+?)</h1>',
49              r'videoTitle\s*:\s*(["\'])(?P<title>)\1'),
50             webpage, 'title', group='title')
51
52         formats = []
53         sources = self._parse_json(
54             self._search_regex(
55                 r'sources\s*:\s*({.+?})', webpage, 'source', default='{}'),
56             video_id, fatal=False)
57         if sources and isinstance(sources, dict):
58             for format_id, format_url in sources.items():
59                 if format_url:
60                     formats.append({
61                         'url': format_url,
62                         'format_id': format_id,
63                         'height': int_or_none(format_id),
64                     })
65         else:
66             video_url = self._html_search_regex(
67                 r'<source src="(.+?)" type="video/mp4">', webpage, 'video URL')
68             formats.append({'url': video_url})
69         self._sort_formats(formats)
70
71         thumbnail = self._og_search_thumbnail(webpage)
72         upload_date = unified_strdate(self._search_regex(
73             r'<span[^>]+class="added-time"[^>]*>ADDED ([^<]+)<',
74             webpage, 'upload date', fatal=False))
75         duration = int_or_none(self._search_regex(
76             r'videoDuration\s*:\s*(\d+)', webpage, 'duration', fatal=False))
77         view_count = str_to_int(self._search_regex(
78             r'<span[^>]*>VIEWS</span></td>\s*<td>([\d,.]+)',
79             webpage, 'view count', fatal=False))
80
81         # No self-labeling, but they describe themselves as
82         # "Home of Videos Porno"
83         age_limit = 18
84
85         return {
86             'id': video_id,
87             'ext': 'mp4',
88             'title': title,
89             'thumbnail': thumbnail,
90             'upload_date': upload_date,
91             'duration': duration,
92             'view_count': view_count,
93             'age_limit': age_limit,
94             'formats': formats,
95         }