]>
Commit | Line | Data |
---|---|---|
0e96b408 | 1 | import urllib.parse |
9bb2c767 | 2 | |
0c43a481 S |
3 | from .common import InfoExtractor |
4 | from ..utils import ( | |
5 | ExtractorError, | |
0c43a481 | 6 | float_or_none, |
0e96b408 | 7 | int_or_none, |
82f4f444 | 8 | parse_qs, |
0e96b408 | 9 | traverse_obj, |
29f7c58a | 10 | try_get, |
11 | unescapeHTML, | |
82f4f444 | 12 | update_url_query, |
4d9280c9 | 13 | urlencode_postdata, |
97abf05a | 14 | url_or_none, |
0c43a481 S |
15 | ) |
16 | ||
17 | ||
18 | class RedditIE(InfoExtractor): | |
4d9280c9 | 19 | _NETRC_MACHINE = 'reddit' |
7a6f6f24 | 20 | _VALID_URL = r'https?://(?P<host>(?:\w+\.)?reddit(?:media)?\.com)/(?P<slug>(?:(?:r|user)/[^/]+/)?comments/(?P<id>[^/?#&]+))' |
0c43a481 S |
21 | _TESTS = [{ |
22 | 'url': 'https://www.reddit.com/r/videos/comments/6rrwyj/that_small_heart_attack/', | |
23 | 'info_dict': { | |
24 | 'id': 'zv89llsvexdz', | |
25 | 'ext': 'mp4', | |
07689fc1 | 26 | 'display_id': '6rrwyj', |
0c43a481 | 27 | 'title': 'That small heart attack.', |
29f7c58a | 28 | 'thumbnail': r're:^https?://.*\.(?:jpg|png)', |
29 | 'thumbnails': 'count:4', | |
0c43a481 S |
30 | 'timestamp': 1501941939, |
31 | 'upload_date': '20170805', | |
32 | 'uploader': 'Antw87', | |
29f7c58a | 33 | 'duration': 12, |
0c43a481 S |
34 | 'like_count': int, |
35 | 'dislike_count': int, | |
36 | 'comment_count': int, | |
37 | 'age_limit': 0, | |
84e0e33a | 38 | 'channel_id': 'videos', |
0c43a481 S |
39 | }, |
40 | 'params': { | |
0c43a481 S |
41 | 'skip_download': True, |
42 | }, | |
2e565f5b | 43 | }, { |
44 | # 1080p fallback format | |
45 | 'url': 'https://www.reddit.com/r/aww/comments/90bu6w/heat_index_was_110_degrees_so_we_offered_him_a/', | |
46 | 'md5': '8b5902cfda3006bf90faea7adf765a49', | |
47 | 'info_dict': { | |
48 | 'id': 'gyh95hiqc0b11', | |
49 | 'ext': 'mp4', | |
50 | 'display_id': '90bu6w', | |
51 | 'title': 'Heat index was 110 degrees so we offered him a cold drink. He went for a full body soak instead', | |
52 | 'thumbnail': r're:^https?://.*\.(?:jpg|png)', | |
53 | 'thumbnails': 'count:7', | |
54 | 'timestamp': 1532051078, | |
55 | 'upload_date': '20180720', | |
56 | 'uploader': 'FootLoosePickleJuice', | |
57 | 'duration': 14, | |
58 | 'like_count': int, | |
59 | 'dislike_count': int, | |
60 | 'comment_count': int, | |
61 | 'age_limit': 0, | |
84e0e33a | 62 | 'channel_id': 'aww', |
2e565f5b | 63 | }, |
c77df98b O |
64 | }, { |
65 | # User post | |
66 | 'url': 'https://www.reddit.com/user/creepyt0es/comments/nip71r/i_plan_to_make_more_stickers_and_prints_check/', | |
67 | 'info_dict': { | |
68 | 'id': 'zasobba6wp071', | |
69 | 'ext': 'mp4', | |
70 | 'display_id': 'nip71r', | |
71 | 'title': 'I plan to make more stickers and prints! Check them out on my Etsy! Or get them through my Patreon. Links below.', | |
72 | 'thumbnail': r're:^https?://.*\.(?:jpg|png)', | |
73 | 'thumbnails': 'count:5', | |
74 | 'timestamp': 1621709093, | |
75 | 'upload_date': '20210522', | |
76 | 'uploader': 'creepyt0es', | |
77 | 'duration': 6, | |
78 | 'like_count': int, | |
79 | 'dislike_count': int, | |
80 | 'comment_count': int, | |
82f4f444 | 81 | 'age_limit': 18, |
c77df98b O |
82 | 'channel_id': 'u_creepyt0es', |
83 | }, | |
84 | 'params': { | |
85 | 'skip_download': True, | |
86 | }, | |
0e96b408 | 87 | }, { |
88 | # videos embedded in reddit text post | |
89 | 'url': 'https://www.reddit.com/r/KamenRider/comments/wzqkxp/finale_kamen_rider_revice_episode_50_family_to/', | |
90 | 'playlist_count': 2, | |
91 | 'info_dict': { | |
92 | 'id': 'wzqkxp', | |
93 | 'title': 'md5:72d3d19402aa11eff5bd32fc96369b37', | |
94 | }, | |
1fc08914 | 95 | }, { |
96 | # crossposted reddit-hosted media | |
97 | 'url': 'https://www.reddit.com/r/dumbfuckers_club/comments/zjjw82/cringe/', | |
98 | 'md5': '746180895c7b75a9d6b05341f507699a', | |
99 | 'info_dict': { | |
100 | 'id': 'a1oneun6pa5a1', | |
101 | 'ext': 'mp4', | |
102 | 'display_id': 'zjjw82', | |
103 | 'title': 'Cringe', | |
104 | 'uploader': 'Otaku-senpai69420', | |
105 | 'thumbnail': r're:^https?://.*\.(?:jpg|png)', | |
106 | 'upload_date': '20221212', | |
107 | 'timestamp': 1670812309, | |
108 | 'duration': 16, | |
109 | 'like_count': int, | |
110 | 'dislike_count': int, | |
111 | 'comment_count': int, | |
112 | 'age_limit': 0, | |
c77df98b | 113 | 'channel_id': 'dumbfuckers_club', |
1fc08914 | 114 | }, |
7a6f6f24 | 115 | }, { |
116 | # post link without subreddit | |
117 | 'url': 'https://www.reddit.com/comments/124pp33', | |
118 | 'md5': '15eec9d828adcef4468b741a7e45a395', | |
119 | 'info_dict': { | |
120 | 'id': 'antsenjc2jqa1', | |
121 | 'ext': 'mp4', | |
122 | 'display_id': '124pp33', | |
123 | 'title': 'Harmless prank of some old friends', | |
124 | 'uploader': 'Dudezila', | |
125 | 'channel_id': 'ContagiousLaughter', | |
126 | 'duration': 17, | |
127 | 'upload_date': '20230328', | |
128 | 'timestamp': 1680012043, | |
129 | 'thumbnail': r're:^https?://.*\.(?:jpg|png)', | |
130 | 'age_limit': 0, | |
131 | 'comment_count': int, | |
132 | 'dislike_count': int, | |
133 | 'like_count': int, | |
134 | }, | |
135 | }, { | |
136 | # quarantined subreddit post | |
137 | 'url': 'https://old.reddit.com/r/GenZedong/comments/12fujy3/based_hasan/', | |
138 | 'md5': '3156ea69e3c1f1b6259683c5abd36e71', | |
139 | 'info_dict': { | |
140 | 'id': '8bwtclfggpsa1', | |
141 | 'ext': 'mp4', | |
142 | 'display_id': '12fujy3', | |
143 | 'title': 'Based Hasan?', | |
144 | 'uploader': 'KingNigelXLII', | |
145 | 'channel_id': 'GenZedong', | |
146 | 'duration': 16, | |
147 | 'upload_date': '20230408', | |
148 | 'timestamp': 1680979138, | |
149 | 'age_limit': 0, | |
150 | 'comment_count': int, | |
151 | 'dislike_count': int, | |
152 | 'like_count': int, | |
153 | }, | |
154 | 'skip': 'Requires account that has opted-in to the GenZedong subreddit', | |
82f4f444 | 155 | }, { |
156 | # subtitles in HLS manifest | |
157 | 'url': 'https://www.reddit.com/r/Unexpected/comments/1cl9h0u/the_insurance_claim_will_be_interesting/', | |
158 | 'info_dict': { | |
159 | 'id': 'a2mdj5d57qyc1', | |
160 | 'ext': 'mp4', | |
161 | 'display_id': '1cl9h0u', | |
162 | 'title': 'The insurance claim will be interesting', | |
163 | 'uploader': 'darrenpauli', | |
164 | 'channel_id': 'Unexpected', | |
165 | 'duration': 53, | |
166 | 'upload_date': '20240506', | |
167 | 'timestamp': 1714966382, | |
168 | 'age_limit': 0, | |
169 | 'comment_count': int, | |
170 | 'dislike_count': int, | |
171 | 'like_count': int, | |
172 | 'subtitles': {'en': 'mincount:1'}, | |
173 | }, | |
174 | 'params': { | |
175 | 'skip_download': True, | |
176 | }, | |
177 | }, { | |
178 | # subtitles from caption-url | |
179 | 'url': 'https://www.reddit.com/r/soccer/comments/1cxwzso/tottenham_1_0_newcastle_united_james_maddison_31/', | |
180 | 'info_dict': { | |
181 | 'id': 'xbmj4t3igy1d1', | |
182 | 'ext': 'mp4', | |
183 | 'display_id': '1cxwzso', | |
184 | 'title': 'Tottenham [1] - 0 Newcastle United - James Maddison 31\'', | |
185 | 'uploader': 'Woodstovia', | |
186 | 'channel_id': 'soccer', | |
187 | 'duration': 30, | |
188 | 'upload_date': '20240522', | |
189 | 'timestamp': 1716373798, | |
190 | 'age_limit': 0, | |
191 | 'comment_count': int, | |
192 | 'dislike_count': int, | |
193 | 'like_count': int, | |
194 | 'subtitles': {'en': 'mincount:1'}, | |
195 | }, | |
196 | 'params': { | |
197 | 'skip_download': True, | |
198 | 'writesubtitles': True, | |
199 | }, | |
0c43a481 S |
200 | }, { |
201 | 'url': 'https://www.reddit.com/r/videos/comments/6rrwyj', | |
202 | 'only_matching': True, | |
203 | }, { | |
204 | # imgur | |
205 | 'url': 'https://www.reddit.com/r/MadeMeSmile/comments/6t7wi5/wait_for_it/', | |
206 | 'only_matching': True, | |
12b0d4e0 M |
207 | }, { |
208 | # imgur @ old reddit | |
209 | 'url': 'https://old.reddit.com/r/MadeMeSmile/comments/6t7wi5/wait_for_it/', | |
210 | 'only_matching': True, | |
0c43a481 S |
211 | }, { |
212 | # streamable | |
213 | 'url': 'https://www.reddit.com/r/videos/comments/6t7sg9/comedians_hilarious_joke_about_the_guam_flag/', | |
214 | 'only_matching': True, | |
215 | }, { | |
216 | # youtube | |
217 | 'url': 'https://www.reddit.com/r/videos/comments/6t75wq/southern_man_tries_to_speak_without_an_accent/', | |
218 | 'only_matching': True, | |
dbd5c502 | 219 | }, { |
220 | # reddit video @ nm reddit | |
221 | 'url': 'https://nm.reddit.com/r/Cricket/comments/8idvby/lousy_cameraman_finds_himself_in_cairns_line_of/', | |
222 | 'only_matching': True, | |
c470901c | 223 | }, { |
224 | 'url': 'https://www.redditmedia.com/r/serbia/comments/pu9wbx/ako_vu%C4%8Di%C4%87_izgubi_izbore_ja_%C4%87u_da_crknem/', | |
225 | 'only_matching': True, | |
0c43a481 S |
226 | }] |
227 | ||
4d9280c9 | 228 | def _perform_login(self, username, password): |
229 | captcha = self._download_json( | |
230 | 'https://www.reddit.com/api/requires_captcha/login.json', None, | |
231 | 'Checking login requirement')['required'] | |
232 | if captcha: | |
233 | raise ExtractorError('Reddit is requiring captcha before login', expected=True) | |
234 | login = self._download_json( | |
235 | f'https://www.reddit.com/api/login/{username}', None, data=urlencode_postdata({ | |
236 | 'op': 'login-main', | |
237 | 'user': username, | |
238 | 'passwd': password, | |
239 | 'api_type': 'json', | |
240 | }), note='Logging in', errnote='Login request failed') | |
241 | errors = '; '.join(traverse_obj(login, ('json', 'errors', ..., 1))) | |
242 | if errors: | |
243 | raise ExtractorError(f'Unable to login, Reddit API says {errors}', expected=True) | |
244 | elif not traverse_obj(login, ('json', 'data', 'cookie', {str})): | |
245 | raise ExtractorError('Unable to login, no cookie was returned') | |
246 | ||
82f4f444 | 247 | def _get_subtitles(self, video_id): |
248 | # Fallback if there were no subtitles provided by DASH or HLS manifests | |
249 | caption_url = f'https://v.redd.it/{video_id}/wh_ben_en.vtt' | |
250 | if self._is_valid_url(caption_url, video_id, item='subtitles'): | |
251 | return {'en': [{'url': caption_url}]} | |
252 | ||
8e3fd7e0 | 253 | def _real_extract(self, url): |
7a6f6f24 | 254 | host, slug, video_id = self._match_valid_url(url).group('host', 'slug', 'id') |
8e3fd7e0 | 255 | |
7a6f6f24 | 256 | data = self._download_json( |
257 | f'https://{host}/{slug}/.json', video_id, fatal=False, expected_status=403) | |
8e3fd7e0 | 258 | if not data: |
7a6f6f24 | 259 | fallback_host = 'old.reddit.com' if host != 'old.reddit.com' else 'www.reddit.com' |
260 | self.to_screen(f'{host} request failed, retrying with {fallback_host}') | |
261 | data = self._download_json( | |
262 | f'https://{fallback_host}/{slug}/.json', video_id, expected_status=403) | |
263 | ||
264 | if traverse_obj(data, 'error') == 403: | |
265 | reason = data.get('reason') | |
266 | if reason == 'quarantined': | |
267 | self.raise_login_required('Quarantined subreddit; an account that has opted in is required') | |
268 | elif reason == 'private': | |
269 | self.raise_login_required('Private subreddit; an account that has been approved is required') | |
270 | else: | |
271 | raise ExtractorError(f'HTTP Error 403 Forbidden; reason given: {reason}') | |
272 | ||
8e3fd7e0 | 273 | data = data[0]['data']['children'][0]['data'] |
0c43a481 S |
274 | video_url = data['url'] |
275 | ||
0c43a481 S |
276 | over_18 = data.get('over_18') |
277 | if over_18 is True: | |
278 | age_limit = 18 | |
279 | elif over_18 is False: | |
280 | age_limit = 0 | |
281 | else: | |
282 | age_limit = None | |
283 | ||
29f7c58a | 284 | thumbnails = [] |
285 | ||
286 | def add_thumbnail(src): | |
287 | if not isinstance(src, dict): | |
288 | return | |
289 | thumbnail_url = url_or_none(src.get('url')) | |
290 | if not thumbnail_url: | |
291 | return | |
292 | thumbnails.append({ | |
293 | 'url': unescapeHTML(thumbnail_url), | |
294 | 'width': int_or_none(src.get('width')), | |
295 | 'height': int_or_none(src.get('height')), | |
9a04113d | 296 | 'http_headers': {'Accept': '*/*'}, |
29f7c58a | 297 | }) |
298 | ||
299 | for image in try_get(data, lambda x: x['preview']['images']) or []: | |
300 | if not isinstance(image, dict): | |
301 | continue | |
302 | add_thumbnail(image.get('source')) | |
303 | resolutions = image.get('resolutions') | |
304 | if isinstance(resolutions, list): | |
305 | for resolution in resolutions: | |
306 | add_thumbnail(resolution) | |
307 | ||
e16fefd8 | 308 | info = { |
0c43a481 | 309 | 'title': data.get('title'), |
29f7c58a | 310 | 'thumbnails': thumbnails, |
0c43a481 S |
311 | 'timestamp': float_or_none(data.get('created_utc')), |
312 | 'uploader': data.get('author'), | |
84e0e33a | 313 | 'channel_id': data.get('subreddit'), |
0c43a481 S |
314 | 'like_count': int_or_none(data.get('ups')), |
315 | 'dislike_count': int_or_none(data.get('downs')), | |
316 | 'comment_count': int_or_none(data.get('num_comments')), | |
317 | 'age_limit': age_limit, | |
318 | } | |
e16fefd8 | 319 | |
0e96b408 | 320 | parsed_url = urllib.parse.urlparse(video_url) |
321 | ||
322 | # Check for embeds in text posts, or else raise to avoid recursing into the same reddit URL | |
323 | if 'reddit.com' in parsed_url.netloc and f'/{video_id}/' in parsed_url.path: | |
324 | entries = [] | |
325 | for media in traverse_obj(data, ('media_metadata', ...), expected_type=dict): | |
326 | if not media.get('id') or media.get('e') != 'RedditVideo': | |
327 | continue | |
328 | formats = [] | |
329 | if media.get('hlsUrl'): | |
330 | formats.extend(self._extract_m3u8_formats( | |
331 | unescapeHTML(media['hlsUrl']), video_id, 'mp4', m3u8_id='hls', fatal=False)) | |
332 | if media.get('dashUrl'): | |
333 | formats.extend(self._extract_mpd_formats( | |
334 | unescapeHTML(media['dashUrl']), video_id, mpd_id='dash', fatal=False)) | |
335 | if formats: | |
336 | entries.append({ | |
337 | 'id': media['id'], | |
338 | 'display_id': video_id, | |
339 | 'formats': formats, | |
340 | **info, | |
341 | }) | |
342 | if entries: | |
343 | return self.playlist_result(entries, video_id, info.get('title')) | |
344 | raise ExtractorError('No media found', expected=True) | |
345 | ||
e16fefd8 | 346 | # Check if media is hosted on reddit: |
1fc08914 | 347 | reddit_video = traverse_obj(data, ( |
348 | (None, ('crosspost_parent_list', ...)), ('secure_media', 'media'), 'reddit_video'), get_all=False) | |
e16fefd8 JL |
349 | if reddit_video: |
350 | playlist_urls = [ | |
351 | try_get(reddit_video, lambda x: unescapeHTML(x[y])) | |
352 | for y in ('dash_url', 'hls_url') | |
353 | ] | |
354 | ||
355 | # Update video_id | |
356 | display_id = video_id | |
357 | video_id = self._search_regex( | |
358 | r'https?://v\.redd\.it/(?P<id>[^/?#&]+)', reddit_video['fallback_url'], | |
359 | 'video_id', default=display_id) | |
360 | ||
361 | dash_playlist_url = playlist_urls[0] or f'https://v.redd.it/{video_id}/DASHPlaylist.mpd' | |
362 | hls_playlist_url = playlist_urls[1] or f'https://v.redd.it/{video_id}/HLSPlaylist.m3u8' | |
82f4f444 | 363 | qs = traverse_obj(parse_qs(hls_playlist_url), { |
364 | 'f': ('f', 0, {lambda x: ','.join([x, 'subsAll']) if x else 'hd,subsAll'}), | |
365 | }) | |
366 | hls_playlist_url = update_url_query(hls_playlist_url, qs) | |
e16fefd8 | 367 | |
2e565f5b | 368 | formats = [{ |
369 | 'url': unescapeHTML(reddit_video['fallback_url']), | |
370 | 'height': int_or_none(reddit_video.get('height')), | |
371 | 'width': int_or_none(reddit_video.get('width')), | |
372 | 'tbr': int_or_none(reddit_video.get('bitrate_kbps')), | |
373 | 'acodec': 'none', | |
02b2f9fa | 374 | 'vcodec': 'h264', |
2e565f5b | 375 | 'ext': 'mp4', |
376 | 'format_id': 'fallback', | |
377 | 'format_note': 'DASH video, mp4_dash', | |
378 | }] | |
20c3c9b4 | 379 | hls_fmts, subtitles = self._extract_m3u8_formats_and_subtitles( |
380 | hls_playlist_url, display_id, 'mp4', m3u8_id='hls', fatal=False) | |
381 | formats.extend(hls_fmts) | |
382 | dash_fmts, dash_subs = self._extract_mpd_formats_and_subtitles( | |
383 | dash_playlist_url, display_id, mpd_id='dash', fatal=False) | |
384 | formats.extend(dash_fmts) | |
385 | self._merge_subtitles(dash_subs, target=subtitles) | |
e16fefd8 JL |
386 | |
387 | return { | |
388 | **info, | |
389 | 'id': video_id, | |
390 | 'display_id': display_id, | |
391 | 'formats': formats, | |
82f4f444 | 392 | 'subtitles': subtitles or self.extract_subtitles(video_id), |
e16fefd8 JL |
393 | 'duration': int_or_none(reddit_video.get('duration')), |
394 | } | |
395 | ||
07689fc1 | 396 | if parsed_url.netloc == 'v.redd.it': |
397 | self.raise_no_formats('This video is processing', expected=True, video_id=video_id) | |
398 | return { | |
399 | **info, | |
400 | 'id': parsed_url.path.split('/')[1], | |
401 | 'display_id': video_id, | |
402 | } | |
403 | ||
e16fefd8 JL |
404 | # Not hosted on reddit, must continue extraction |
405 | return { | |
406 | **info, | |
407 | 'display_id': video_id, | |
408 | '_type': 'url_transparent', | |
409 | 'url': video_url, | |
410 | } |