Replace np links with www to fix 403 forbidden error.

This commit is contained in:
Michael Lazar
2016-02-04 15:33:50 -08:00
parent 20a17ec2b3
commit ff29429601
4 changed files with 2681 additions and 1330 deletions

View File

@@ -26,7 +26,6 @@ _logger = logging.getLogger(__name__)
# ptrace_scope to 0 in /etc/sysctl.d/10-ptrace.conf. # ptrace_scope to 0 in /etc/sysctl.d/10-ptrace.conf.
# http://blog.mellenthin.de/archives/2010/10/18/gdb-attach-fails # http://blog.mellenthin.de/archives/2010/10/18/gdb-attach-fails
def main(): def main():
"Main entry point" "Main entry point"
@@ -60,6 +59,14 @@ def main():
config.delete_refresh_token() config.delete_refresh_token()
if config['log']: if config['log']:
# Log request headers to the file (print hack only works on python 3.x)
# from http import client
# _http_logger = logging.getLogger('http.client')
# client.HTTPConnection.debuglevel = 2
# def print_to_file(*args, **_):
# if args[0] != "header:":
# _http_logger.info(' '.join(args))
# client.print = print_to_file
logging.basicConfig(level=logging.DEBUG, filename=config['log']) logging.basicConfig(level=logging.DEBUG, filename=config['log'])
else: else:
# Add an empty handler so the logger doesn't complain # Add an empty handler so the logger doesn't complain

View File

@@ -251,7 +251,10 @@ class SubmissionContent(Content):
def from_url(cls, reddit, url, loader, indent_size=2, max_indent_level=8, def from_url(cls, reddit, url, loader, indent_size=2, max_indent_level=8,
order=None): order=None):
url = url.replace('http:', 'https:') url = url.replace('http:', 'https:') # Reddit forces SSL
# Sometimes reddit will return a 403 FORBIDDEN when trying to access an
# np link while using OAUTH. Cause is unknown.
url = url.replace('https://np.', 'https://www.')
submission = reddit.get_submission(url, comment_sort=order) submission = reddit.get_submission(url, comment_sort=order)
return cls(submission, loader, indent_size, max_indent_level, order) return cls(submission, loader, indent_size, max_indent_level, order)

File diff suppressed because it is too large Load Diff

View File

@@ -143,7 +143,7 @@ def test_content_submission_load_more_comments(reddit, terminal):
assert content.get(390)['type'] == 'Comment' assert content.get(390)['type'] == 'Comment'
def test_content_submission_from_url(reddit, terminal): def test_content_submission_from_url(reddit, oauth, refresh_token, terminal):
url = 'https://www.reddit.com/r/AskReddit/comments/2np694/' url = 'https://www.reddit.com/r/AskReddit/comments/2np694/'
SubmissionContent.from_url(reddit, url, terminal.loader) SubmissionContent.from_url(reddit, url, terminal.loader)
@@ -159,6 +159,14 @@ def test_content_submission_from_url(reddit, terminal):
SubmissionContent.from_url(reddit, url[:-2], terminal.loader) SubmissionContent.from_url(reddit, url[:-2], terminal.loader)
assert isinstance(terminal.loader.exception, praw.errors.NotFound) assert isinstance(terminal.loader.exception, praw.errors.NotFound)
# np.* urls should not raise a 403 error when logged into oauth
oauth.config.refresh_token = refresh_token
oauth.authorize()
url = 'https://np.reddit.com//r/LifeProTips/comments/441hsf//czmp112.json'
with terminal.loader():
SubmissionContent.from_url(reddit, url, terminal.loader)
assert not terminal.loader.exception
def test_content_subreddit_initialize(reddit, terminal): def test_content_subreddit_initialize(reddit, terminal):