[redtube] Improve metadata extraction (closes #22492, closes #22615)
[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     merge_dicts,
10     str_to_int,
11     unified_strdate,
12     url_or_none,
13 )
14
15
16 class RedTubeIE(InfoExtractor):
17     _VALID_URL = r'https?://(?:(?:www\.)?redtube\.com/|embed\.redtube\.com/\?.*?\bid=)(?P<id>[0-9]+)'
18     _TESTS = [{
19         'url': 'http://www.redtube.com/66418',
20         'md5': 'fc08071233725f26b8f014dba9590005',
21         'info_dict': {
22             'id': '66418',
23             'ext': 'mp4',
24             'title': 'Sucked on a toilet',
25             'upload_date': '20110811',
26             'duration': 596,
27             'view_count': int,
28             'age_limit': 18,
29         }
30     }, {
31         'url': 'http://embed.redtube.com/?bgcolor=000000&id=1443286',
32         'only_matching': True,
33     }]
34
35     @staticmethod
36     def _extract_urls(webpage):
37         return re.findall(
38             r'<iframe[^>]+?src=["\'](?P<url>(?:https?:)?//embed\.redtube\.com/\?.*?\bid=\d+)',
39             webpage)
40
41     def _real_extract(self, url):
42         video_id = self._match_id(url)
43         webpage = self._download_webpage(
44             'http://www.redtube.com/%s' % video_id, video_id)
45
46         if any(s in webpage for s in ['video-deleted-info', '>This video has been removed']):
47             raise ExtractorError('Video %s has been removed' % video_id, expected=True)
48
49         info = self._search_json_ld(webpage, video_id, default={})
50
51         if not info.get('title'):
52             info['title'] = self._html_search_regex(
53                 (r'<h(\d)[^>]+class="(?:video_title_text|videoTitle)[^"]*">(?P<title>(?:(?!\1).)+)</h\1>',
54                  r'(?:videoTitle|title)\s*:\s*(["\'])(?P<title>(?:(?!\1).)+)\1',),
55                 webpage, 'title', group='title',
56                 default=None) or self._og_search_title(webpage)
57
58         formats = []
59         sources = self._parse_json(
60             self._search_regex(
61                 r'sources\s*:\s*({.+?})', webpage, 'source', default='{}'),
62             video_id, fatal=False)
63         if sources and isinstance(sources, dict):
64             for format_id, format_url in sources.items():
65                 if format_url:
66                     formats.append({
67                         'url': format_url,
68                         'format_id': format_id,
69                         'height': int_or_none(format_id),
70                     })
71         medias = self._parse_json(
72             self._search_regex(
73                 r'mediaDefinition\s*:\s*(\[.+?\])', webpage,
74                 'media definitions', default='{}'),
75             video_id, fatal=False)
76         if medias and isinstance(medias, list):
77             for media in medias:
78                 format_url = url_or_none(media.get('videoUrl'))
79                 if not format_url:
80                     continue
81                 format_id = media.get('quality')
82                 formats.append({
83                     'url': format_url,
84                     'format_id': format_id,
85                     'height': int_or_none(format_id),
86                 })
87         if not formats:
88             video_url = self._html_search_regex(
89                 r'<source src="(.+?)" type="video/mp4">', webpage, 'video URL')
90             formats.append({'url': video_url})
91         self._sort_formats(formats)
92
93         thumbnail = self._og_search_thumbnail(webpage)
94         upload_date = unified_strdate(self._search_regex(
95             r'<span[^>]+>(?:ADDED|Published on) ([^<]+)<',
96             webpage, 'upload date', default=None))
97         duration = int_or_none(self._og_search_property(
98             'video:duration', webpage, default=None) or self._search_regex(
99                 r'videoDuration\s*:\s*(\d+)', webpage, 'duration', default=None))
100         view_count = str_to_int(self._search_regex(
101             (r'<div[^>]*>Views</div>\s*<div[^>]*>\s*([\d,.]+)',
102              r'<span[^>]*>VIEWS</span>\s*</td>\s*<td>\s*([\d,.]+)',
103              r'<span[^>]+\bclass=["\']video_view_count[^>]*>\s*([\d,.]+)'),
104             webpage, 'view count', default=None))
105
106         # No self-labeling, but they describe themselves as
107         # "Home of Videos Porno"
108         age_limit = 18
109
110         return merge_dicts(info, {
111             'id': video_id,
112             'ext': 'mp4',
113             'thumbnail': thumbnail,
114             'upload_date': upload_date,
115             'duration': duration,
116             'view_count': view_count,
117             'age_limit': age_limit,
118             'formats': formats,
119         })