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