[ie/reddit] Fix unauthenticated extraction (#16839)

Closes #16877
Authored by: jdesgats, bashonly, 0xvd

Co-authored-by: bashonly <88596187+bashonly@users.noreply.github.com>
Co-authored-by: 0xvd <199783523+0xvd@users.noreply.github.com>
This commit is contained in:
Julien Desgats 2026-06-09 07:18:39 +01:00 committed by GitHub
parent 59bba1be7b
commit 72ac620515
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -265,7 +265,14 @@ class RedditIE(InfoExtractor):
'only_matching': True, 'only_matching': True,
}] }]
@property
def _is_logged_in(self):
return bool(self._get_cookies('https://www.reddit.com/').get('reddit_session'))
def _perform_login(self, username, password): def _perform_login(self, username, password):
if self._is_logged_in:
return
captcha = self._download_json( captcha = self._download_json(
'https://www.reddit.com/api/requires_captcha/login.json', None, 'https://www.reddit.com/api/requires_captcha/login.json', None,
'Checking login requirement')['required'] 'Checking login requirement')['required']
@ -285,6 +292,12 @@ class RedditIE(InfoExtractor):
raise ExtractorError('Unable to login, no cookie was returned') raise ExtractorError('Unable to login, no cookie was returned')
def _real_initialize(self): def _real_initialize(self):
if not self._is_logged_in:
# We need to get a 'loid' cookie to access the API anonymously
self._request_webpage(
'https://old.reddit.com/', None,
'Setting up session via old reddit', 'Session request failed', fatal=False)
# Set cookie to opt-in to age-restricted subreddits # Set cookie to opt-in to age-restricted subreddits
self._set_cookie('reddit.com', 'over18', '1') self._set_cookie('reddit.com', 'over18', '1')
# Set cookie to opt-in to "gated" subreddits # Set cookie to opt-in to "gated" subreddits
@ -302,12 +315,24 @@ class RedditIE(InfoExtractor):
def _real_extract(self, url): def _real_extract(self, url):
slug, video_id = self._match_valid_url(url).group('slug', 'id') slug, video_id = self._match_valid_url(url).group('slug', 'id')
# Fallback for if old.reddit session request failed
if not self._is_logged_in and not self._get_cookies('https://www.reddit.com/').get('loid'):
self._request_webpage(
f'https://www.reddit.com/svc/shreddit/{slug}', video_id,
'Setting up session via shreddit', 'Session request failed',
fatal=False, impersonate=True,
query={
'seeker-session': 'false',
'render-mode': 'partial',
'referer': url,
})
try: try:
data = self._download_json( data = self._download_json(
f'https://www.reddit.com/{slug}/.json', video_id, expected_status=403) f'https://www.reddit.com/{slug}/.json', video_id, expected_status=403)
except ExtractorError as e: except ExtractorError as e:
if isinstance(e.cause, json.JSONDecodeError): if isinstance(e.cause, json.JSONDecodeError):
if self._get_cookies('https://www.reddit.com/').get('reddit_session'): if self._is_logged_in:
raise ExtractorError('Your IP address is unable to access the Reddit API', expected=True) raise ExtractorError('Your IP address is unable to access the Reddit API', expected=True)
self.raise_login_required('Account authentication is required') self.raise_login_required('Account authentication is required')
raise raise