]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/rumble.py
[cleanup] Misc fixes (see desc)
[yt-dlp.git] / yt_dlp / extractor / rumble.py
1 import itertools
2 import re
3
4 from .common import InfoExtractor
5 from ..compat import compat_str, compat_HTTPError
6 from ..utils import (
7 determine_ext,
8 int_or_none,
9 parse_iso8601,
10 try_get,
11 unescapeHTML,
12 ExtractorError,
13 )
14
15
16 class RumbleEmbedIE(InfoExtractor):
17 _VALID_URL = r'https?://(?:www\.)?rumble\.com/embed/(?:[0-9a-z]+\.)?(?P<id>[0-9a-z]+)'
18 _TESTS = [{
19 'url': 'https://rumble.com/embed/v5pv5f',
20 'md5': '36a18a049856720189f30977ccbb2c34',
21 'info_dict': {
22 'id': 'v5pv5f',
23 'ext': 'mp4',
24 'title': 'WMAR 2 News Latest Headlines | October 20, 6pm',
25 'timestamp': 1571611968,
26 'upload_date': '20191020',
27 'channel_url': 'https://rumble.com/c/WMAR',
28 'channel': 'WMAR',
29 'thumbnail': 'https://sp.rmbl.ws/s8/1/5/M/z/1/5Mz1a.OvCc-small-WMAR-2-News-Latest-Headline.jpg',
30 'duration': 234,
31 'uploader': 'WMAR',
32 }
33 }, {
34 'url': 'https://rumble.com/embed/vslb7v',
35 'md5': '7418035de1a30a178b8af34dc2b6a52b',
36 'info_dict': {
37 'id': 'vslb7v',
38 'ext': 'mp4',
39 'title': 'Defense Sec. says US Commitment to NATO Defense \'Ironclad\'',
40 'timestamp': 1645142135,
41 'upload_date': '20220217',
42 'channel_url': 'https://rumble.com/c/CyberTechNews',
43 'channel': 'CTNews',
44 'thumbnail': 'https://sp.rmbl.ws/s8/6/7/i/9/h/7i9hd.OvCc.jpg',
45 'duration': 901,
46 'uploader': 'CTNews',
47 }
48 }, {
49 'url': 'https://rumble.com/embed/ufe9n.v5pv5f',
50 'only_matching': True,
51 }]
52
53 @staticmethod
54 def _extract_urls(webpage):
55 return [
56 mobj.group('url')
57 for mobj in re.finditer(
58 r'(?:<(?:script|iframe)[^>]+\bsrc=|["\']embedUrl["\']\s*:\s*)["\'](?P<url>%s)' % RumbleEmbedIE._VALID_URL,
59 webpage)]
60
61 def _real_extract(self, url):
62 video_id = self._match_id(url)
63 video = self._download_json(
64 'https://rumble.com/embedJS/', video_id,
65 query={'request': 'video', 'v': video_id})
66 title = unescapeHTML(video['title'])
67
68 formats = []
69 for height, ua in (video.get('ua') or {}).items():
70 for i in range(2):
71 f_url = try_get(ua, lambda x: x[i], compat_str)
72 if f_url:
73 ext = determine_ext(f_url)
74 f = {
75 'ext': ext,
76 'format_id': '%s-%sp' % (ext, height),
77 'height': int_or_none(height),
78 'url': f_url,
79 }
80 bitrate = try_get(ua, lambda x: x[i + 2]['bitrate'])
81 if bitrate:
82 f['tbr'] = int_or_none(bitrate)
83 formats.append(f)
84 self._sort_formats(formats)
85
86 subtitles = {
87 lang: [{
88 'url': sub_info['path'],
89 'name': sub_info.get('language') or '',
90 }] for lang, sub_info in (video.get('cc') or {}).items() if sub_info.get('path')
91 }
92
93 author = video.get('author') or {}
94
95 return {
96 'id': video_id,
97 'title': title,
98 'formats': formats,
99 'subtitles': subtitles,
100 'thumbnail': video.get('i'),
101 'timestamp': parse_iso8601(video.get('pubDate')),
102 'channel': author.get('name'),
103 'channel_url': author.get('url'),
104 'duration': int_or_none(video.get('duration')),
105 'uploader': author.get('name'),
106 }
107
108
109 class RumbleChannelIE(InfoExtractor):
110 _VALID_URL = r'(?P<url>https?://(?:www\.)?rumble\.com/(?:c|user)/(?P<id>[^&?#$/]+))'
111
112 _TESTS = [{
113 'url': 'https://rumble.com/c/Styxhexenhammer666',
114 'playlist_mincount': 1160,
115 'info_dict': {
116 'id': 'Styxhexenhammer666',
117 },
118 }, {
119 'url': 'https://rumble.com/user/goldenpoodleharleyeuna',
120 'playlist_count': 4,
121 'info_dict': {
122 'id': 'goldenpoodleharleyeuna',
123 },
124 }]
125
126 def entries(self, url, playlist_id):
127 for page in itertools.count(1):
128 try:
129 webpage = self._download_webpage(f'{url}?page={page}', playlist_id, note='Downloading page %d' % page)
130 except ExtractorError as e:
131 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 404:
132 break
133 raise
134 for video_url in re.findall(r'class=video-item--a\s?href=([^>]+\.html)', webpage):
135 yield self.url_result('https://rumble.com' + video_url)
136
137 def _real_extract(self, url):
138 url, playlist_id = self._match_valid_url(url).groups()
139 return self.playlist_result(self.entries(url, playlist_id), playlist_id=playlist_id)