From 9d956e774412d604b6a4d24428c5c815166fdff5 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Sun, 14 Oct 2018 18:19:58 -0400 Subject: [PATCH] Add support for best/ being the default --- rtv/content.py | 6 +++--- rtv/docs.py | 5 +++-- rtv/packages/praw/__init__.py | 39 ++++++++++++++++++++++++++++++----- rtv/packages/praw/internal.py | 2 +- rtv/packages/praw/objects.py | 6 ++++-- rtv/submission_page.py | 4 ++++ rtv/subreddit_page.py | 8 +++++++ rtv/templates/rtv.cfg | 1 + 8 files changed, 58 insertions(+), 13 deletions(-) diff --git a/rtv/content.py b/rtv/content.py index 411110d8..ca3ac668 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -593,7 +593,7 @@ def from_name(cls, reddit, name, loader, order=None, query=None): orders = ['relevance', 'top', 'comments', 'new', None] period_allowed = ['top', 'comments'] else: - orders = ['hot', 'top', 'rising', 'new', 'controversial', 'gilded', None] + orders = ['best', 'hot', 'top', 'rising', 'new', 'controversial', 'gilded', None] period_allowed = ['top', 'controversial'] if order not in orders: @@ -613,7 +613,7 @@ def from_name(cls, reddit, name, loader, order=None, query=None): elif order: method_alias = 'get_{0}'.format(order) else: - method_alias = 'get_hot' + method_alias = 'get_best' # Here's where we start to build the submission generators if query: @@ -672,7 +672,7 @@ def from_name(cls, reddit, name, loader, order=None, query=None): submissions = method(sort=order, time=period, limit=None) elif resource == 'front': - if order in (None, 'hot'): + if order in (None, 'best'): submissions = reddit.get_front_page(limit=None) elif period: # For the front page, praw makes you send the period as `t` diff --git a/rtv/docs.py b/rtv/docs.py index c9bf00a1..462c8da1 100644 --- a/rtv/docs.py +++ b/rtv/docs.py @@ -38,6 +38,7 @@ G : Jump to the last post J : Jump to the next sibling comment K : Jump to the parent comment + 0 : Sort by best 1 : Sort by hot 2 : Sort by top 3 : Sort by rising @@ -93,11 +94,11 @@ """ BANNER_SUBREDDIT = """ -[1]hot [2]top [3]rising [4]new [5]controversial [6]gilded +[0]best [1]hot [2]top [3]rising [4]new [5]controversial [6]gilded """ BANNER_SUBMISSION = """ -[1]hot [2]top [3]rising [4]new [5]controversial +[0]best [1]hot [2]top [3]rising [4]new [5]controversial """ BANNER_SEARCH = """ diff --git a/rtv/packages/praw/__init__.py b/rtv/packages/praw/__init__.py index c26c04ac..a30f73e2 100644 --- a/rtv/packages/praw/__init__.py +++ b/rtv/packages/praw/__init__.py @@ -62,6 +62,7 @@ class Config(object): # pylint: disable=R0903 'approve': 'api/approve/', 'authorize': 'api/v1/authorize/', 'banned': 'r/{subreddit}/about/banned/', + 'best': 'best/', 'blocked': 'prefs/blocked/', 'by_id': 'by_id/', 'captcha': 'captcha/', @@ -99,6 +100,7 @@ class Config(object): # pylint: disable=R0903 'gilded': 'gilded/', 'help': 'help/', 'hide': 'api/hide/', + 'hot': 'hot/', 'ignore_reports': 'api/ignore_reports/', 'inbox': 'message/inbox/', 'info': 'api/info/', @@ -833,7 +835,7 @@ def get_controversial(self, *args, **kwargs): return self.get_content(self.config['controversial'], *args, **kwargs) @decorators.restrict_access(scope='read') - def get_domain_listing(self, domain, sort='hot', period=None, *args, + def get_domain_listing(self, domain, sort='best', period=None, *args, **kwargs): """Return a get_content generator for submissions by domain. @@ -841,8 +843,8 @@ def get_domain_listing(self, domain, sort='hot', period=None, *args, ``https://www.reddit.com/domain/{domain}``. :param domain: The domain to generate a submission listing for. - :param sort: When provided must be one of 'hot', 'new', 'rising', - 'controversial, 'gilded', or 'top'. Defaults to 'hot'. + :param sort: When provided must be one of 'best', 'hot', 'new', 'rising', + 'controversial, 'gilded', or 'top'. Defaults to 'best'. :param period: When sort is either 'controversial', or 'top' the period can be either None (for account default), 'all', 'year', 'month', 'week', 'day', or 'hour'. @@ -852,7 +854,7 @@ def get_domain_listing(self, domain, sort='hot', period=None, *args, """ # Verify arguments - if sort not in ('controversial', 'hot', 'new', 'rising', 'top', + if sort not in ('best', 'controversial', 'hot', 'new', 'rising', 'top', 'gilded'): raise TypeError('Invalid sort parameter.') if period not in (None, 'all', 'day', 'hour', 'month', 'week', 'year'): @@ -861,7 +863,7 @@ def get_domain_listing(self, domain, sort='hot', period=None, *args, raise TypeError('Period cannot be set for that sort argument.') url = self.config['domain'].format(domain=domain) - if sort != 'hot': + if sort != 'best': url += sort if period: # Set or overwrite params 't' parameter kwargs.setdefault('params', {})['t'] = period @@ -985,6 +987,33 @@ def get_new(self, *args, **kwargs): """ return self.get_content(self.config['new'], *args, **kwargs) + @decorators.restrict_access(scope='read') + def get_hot(self, *args, **kwargs): + """Return a get_content generator for hot submissions. + + Corresponds to the submissions provided by + ``https://www.reddit.com/hot/`` for the session. + + The additional parameters are passed directly into + :meth:`.get_content`. Note: the `url` parameter cannot be altered. + + """ + return self.get_content(self.config['hot'], *args, **kwargs) + + @decorators.restrict_access(scope='read') + def get_best(self, *args, **kwargs): + """Return a get_content generator for hot submissions. + + Corresponds to the submissions provided by + ``https://www.reddit.com/hot/`` for the session. + + The additional parameters are passed directly into + :meth:`.get_content`. Note: the `url` parameter cannot be altered. + + """ + return self.get_content(self.config['best'], *args, **kwargs) + + def get_new_subreddits(self, *args, **kwargs): """Return a get_content generator for the newest subreddits. diff --git a/rtv/packages/praw/internal.py b/rtv/packages/praw/internal.py index 92c5e52e..83e99ac9 100644 --- a/rtv/packages/praw/internal.py +++ b/rtv/packages/praw/internal.py @@ -52,7 +52,7 @@ def _listing(self, sort='new', time='all', *args, **kwargs): """Return a get_content generator for some RedditContentObject type. :param sort: Specify the sort order of the results if applicable - (one of ``'hot'``, ``'new'``, ``'top'``, ``'controversial'``). + (one of ``'best'``, ``'hot'``, ``'new'``, ``'top'``, ``'controversial'``). :param time: Specify the time-period to return submissions if applicable (one of ``'hour'``, ``'day'``, ``'week'``, ``'month'``, ``'year'``, ``'all'``). diff --git a/rtv/packages/praw/objects.py b/rtv/packages/praw/objects.py index 86ae066c..6be358f1 100644 --- a/rtv/packages/praw/objects.py +++ b/rtv/packages/praw/objects.py @@ -1576,7 +1576,8 @@ class Subreddit(Messageable, Refreshable): # Generic listing selectors get_controversial = _get_sorter('controversial') - get_hot = _get_sorter('') + get_best = _get_sorter('') + get_hot = _get_sorter('hot') get_new = _get_sorter('new') get_top = _get_sorter('top') get_gilded = _get_sorter('gilded') @@ -1670,7 +1671,8 @@ class Multireddit(Refreshable): # Generic listing selectors get_controversial = _get_sorter('controversial') - get_hot = _get_sorter('') + get_best = _get_sorter('') + get_hot = _get_sorter('hot') get_new = _get_sorter('new') get_top = _get_sorter('top') diff --git a/rtv/submission_page.py b/rtv/submission_page.py index 0ec10ec6..d17c39d9 100644 --- a/rtv/submission_page.py +++ b/rtv/submission_page.py @@ -37,6 +37,10 @@ def __init__(self, reddit, term, config, oauth, url=None, submission=None): self.nav = Navigator(self.content.get, page_index=-1) self.selected_subreddit = None + @SubmissionController.register(Command('SORT_BEST')) + def sort_content_best(self): + self.refresh_content(order='best') + @SubmissionController.register(Command('SORT_HOT')) def sort_content_hot(self): self.refresh_content(order='hot') diff --git a/rtv/subreddit_page.py b/rtv/subreddit_page.py index 2426ed79..564fd8cf 100644 --- a/rtv/subreddit_page.py +++ b/rtv/subreddit_page.py @@ -60,6 +60,14 @@ def refresh_content(self, order=None, name=None): if not self.term.loader.exception: self.nav = Navigator(self.content.get) + @SubredditController.register(Command('SORT_BEST')) + def sort_content_best(self): + if self.content.query: + self.refresh_content(order='relevance') + else: + self.refresh_content(order='best') + + @SubredditController.register(Command('SORT_HOT')) def sort_content_hot(self): if self.content.query: diff --git a/rtv/templates/rtv.cfg b/rtv/templates/rtv.cfg index c85b152f..81d43d40 100644 --- a/rtv/templates/rtv.cfg +++ b/rtv/templates/rtv.cfg @@ -114,6 +114,7 @@ imgur_client_id = 93396265f59dec9 EXIT = q FORCE_EXIT = Q HELP = ? +SORT_BEST = 0 SORT_HOT = 1 SORT_TOP = 2 SORT_RISING = 3