aboutsummaryrefslogtreecommitdiffstats
path: root/yt_dlp
diff options
context:
space:
mode:
authorbashonly <88596187+bashonly@users.noreply.github.com>2023-04-29 13:19:35 -0500
committerGitHub <noreply@github.com>2023-04-29 18:19:35 +0000
commit4d9280c9c853733534dda60486fa949bcca36c9e (patch)
tree7be087b6c010e850f31a2a182c1145b4e7bf125c /yt_dlp
parent17ba4343cf99701692a7f4798fd42b50f644faba (diff)
downloadhypervideo-pre-4d9280c9c853733534dda60486fa949bcca36c9e.tar.lz
hypervideo-pre-4d9280c9c853733534dda60486fa949bcca36c9e.tar.xz
hypervideo-pre-4d9280c9c853733534dda60486fa949bcca36c9e.zip
[extractor/reddit] Add login support (#6950)
Closes #6949 Authored by: bashonly
Diffstat (limited to 'yt_dlp')
-rw-r--r--yt_dlp/extractor/reddit.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/yt_dlp/extractor/reddit.py b/yt_dlp/extractor/reddit.py
index 3e458456c..13615e82f 100644
--- a/yt_dlp/extractor/reddit.py
+++ b/yt_dlp/extractor/reddit.py
@@ -8,11 +8,13 @@ from ..utils import (
traverse_obj,
try_get,
unescapeHTML,
+ urlencode_postdata,
url_or_none,
)
class RedditIE(InfoExtractor):
+ _NETRC_MACHINE = 'reddit'
_VALID_URL = r'https?://(?P<host>(?:\w+\.)?reddit(?:media)?\.com)/(?P<slug>(?:(?:r|user)/[^/]+/)?comments/(?P<id>[^/?#&]+))'
_TESTS = [{
'url': 'https://www.reddit.com/r/videos/comments/6rrwyj/that_small_heart_attack/',
@@ -176,6 +178,25 @@ class RedditIE(InfoExtractor):
'only_matching': True,
}]
+ def _perform_login(self, username, password):
+ captcha = self._download_json(
+ 'https://www.reddit.com/api/requires_captcha/login.json', None,
+ 'Checking login requirement')['required']
+ if captcha:
+ raise ExtractorError('Reddit is requiring captcha before login', expected=True)
+ login = self._download_json(
+ f'https://www.reddit.com/api/login/{username}', None, data=urlencode_postdata({
+ 'op': 'login-main',
+ 'user': username,
+ 'passwd': password,
+ 'api_type': 'json',
+ }), note='Logging in', errnote='Login request failed')
+ errors = '; '.join(traverse_obj(login, ('json', 'errors', ..., 1)))
+ if errors:
+ raise ExtractorError(f'Unable to login, Reddit API says {errors}', expected=True)
+ elif not traverse_obj(login, ('json', 'data', 'cookie', {str})):
+ raise ExtractorError('Unable to login, no cookie was returned')
+
def _real_extract(self, url):
host, slug, video_id = self._match_valid_url(url).group('host', 'slug', 'id')