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