Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Add support for best/ being the new default sort #619

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions rtv/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,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:
Expand All @@ -715,7 +715,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:
Expand Down Expand Up @@ -774,7 +774,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`
Expand Down
4 changes: 2 additions & 2 deletions rtv/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@
"""

BANNER_SUBREDDIT = """
[1]hot [2]top [3]rising [4]new [5]controversial [6]gilded
[1]best [2]hot [3]top [4]rising [5]new [6]controversial [7]gilded
"""

BANNER_SUBMISSION = """
[1]hot [2]top [3]rising [4]new [5]controversial
[1]best [2]hot [3]top [4]rising [5]new [6]controversial
"""

BANNER_SEARCH = """
Expand Down
39 changes: 34 additions & 5 deletions rtv/packages/praw/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/',
Expand Down Expand Up @@ -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/',
Expand Down Expand Up @@ -833,16 +835,16 @@ 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.

Corresponds to the submissions provided by
``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'.
Expand All @@ -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'):
Expand All @@ -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
Expand Down Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion rtv/packages/praw/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'``).
Expand Down
6 changes: 4 additions & 2 deletions rtv/packages/praw/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')

Expand Down
12 changes: 8 additions & 4 deletions rtv/submission_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,26 @@ def refresh_content(self, order=None, name=None):
self.nav = Navigator(self.content.get, page_index=-1)

@SubmissionController.register(Command('SORT_1'))
def sort_content_best(self):
self.refresh_content(order='best')

@SubmissionController.register(Command('SORT_2'))
def sort_content_hot(self):
self.refresh_content(order='hot')

@SubmissionController.register(Command('SORT_2'))
@SubmissionController.register(Command('SORT_3'))
def sort_content_top(self):
self.refresh_content(order='top')

@SubmissionController.register(Command('SORT_3'))
@SubmissionController.register(Command('SORT_4'))
def sort_content_rising(self):
self.refresh_content(order='rising')

@SubmissionController.register(Command('SORT_4'))
@SubmissionController.register(Command('SORT_5'))
def sort_content_new(self):
self.refresh_content(order='new')

@SubmissionController.register(Command('SORT_5'))
@SubmissionController.register(Command('SORT_6'))
def sort_content_controversial(self):
self.refresh_content(order='controversial')

Expand Down
35 changes: 25 additions & 10 deletions rtv/subreddit_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,32 +77,47 @@ def sort_content_hot(self):
if self.content.query:
self.refresh_content(order='relevance')
else:
self.refresh_content(order='hot')
self.refresh_content(order='best')

@SubredditController.register(Command('SORT_2'))
def sort_content_top(self):
order = self._prompt_period('top')
if order is None:
self.term.show_notification('Invalid option')
def sort_content_hot(self):
if self.content.query:
order = self._prompt_period('top')
if order is None:
self.term.show_notification('Invalid option')
else:
self.refresh_content(order=order)
else:
self.refresh_content(order=order)
self.refresh_content(order='hot')


@SubredditController.register(Command('SORT_3'))
def sort_content_rising(self):
def sort_content_top(self):
if self.content.query:
order = self._prompt_period('comments')
if order is None:
self.term.show_notification('Invalid option')
else:
self.refresh_content(order=order)
else:
self.refresh_content(order='rising')
order = self._prompt_period('top')
if order is None:
self.term.show_notification('Invalid option')
else:
self.refresh_content(order=order)

@SubredditController.register(Command('SORT_4'))
def sort_content_rising(self):
if self.content.query:
self.refresh_content(order='new')
else:
self.refresh_content(order='rising')

@SubredditController.register(Command('SORT_5'))
def sort_content_new(self):
self.refresh_content(order='new')

@SubredditController.register(Command('SORT_5'))
@SubredditController.register(Command('SORT_6'))
def sort_content_controversial(self):
if self.content.query:
self.term.flash()
Expand All @@ -113,7 +128,7 @@ def sort_content_controversial(self):
else:
self.refresh_content(order=order)

@SubredditController.register(Command('SORT_6'))
@SubredditController.register(Command('SORT_7'))
def sort_content_gilded(self):
if self.content.query:
self.term.flash()
Expand Down