Subscriptions page foundations
This commit is contained in:
@@ -4,10 +4,10 @@ import praw
|
||||
import requests
|
||||
import re
|
||||
|
||||
from .exceptions import SubmissionError, SubredditError, AccountError
|
||||
from .exceptions import SubmissionError, SubredditError, SubscriptionError, AccountError
|
||||
from .helpers import humanize_timestamp, wrap_text, strip_subreddit_url
|
||||
|
||||
__all__ = ['SubredditContent', 'SubmissionContent']
|
||||
__all__ = ['SubredditContent', 'SubmissionContent', 'SubscriptionContent']
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -143,6 +143,20 @@ class BaseContent(object):
|
||||
|
||||
return data
|
||||
|
||||
@staticmethod
|
||||
def strip_praw_subscription(subscription):
|
||||
"""
|
||||
Parse through a subscription and return a dict with data ready to be
|
||||
displayed through the terminal.
|
||||
"""
|
||||
|
||||
data = {}
|
||||
data['object'] = subscription
|
||||
data['type'] = 'Subscription'
|
||||
data['name'] = subscription._case_name
|
||||
data['title'] = subscription.title
|
||||
|
||||
return data
|
||||
|
||||
class SubmissionContent(BaseContent):
|
||||
"""
|
||||
@@ -352,3 +366,47 @@ class SubredditContent(BaseContent):
|
||||
data['offset'] = 0
|
||||
|
||||
return data
|
||||
|
||||
class SubscriptionContent(BaseContent):
|
||||
def __init__(self, subscriptions, loader):
|
||||
self.name = "Subscriptions"
|
||||
self._loader = loader
|
||||
self._subscriptions = subscriptions
|
||||
self._subscription_data = []
|
||||
|
||||
@classmethod
|
||||
def get_list(cls, reddit, loader):
|
||||
try:
|
||||
with loader():
|
||||
subscriptions = reddit.get_my_subreddits(limit=None)
|
||||
except praw.errors.APIException:
|
||||
raise SubscriptionError()
|
||||
|
||||
return cls(subscriptions, loader)
|
||||
|
||||
def get(self, index, n_cols=70):
|
||||
"""
|
||||
Grab the `i`th subscription, with the title field formatted to fit inside
|
||||
of a window of width `n_cols`
|
||||
"""
|
||||
|
||||
if index < 0:
|
||||
raise IndexError
|
||||
|
||||
while index >= len(self._subscription_data):
|
||||
try:
|
||||
with self._loader():
|
||||
subscription = next(self._subscriptions)
|
||||
except StopIteration:
|
||||
raise IndexError
|
||||
else:
|
||||
data = self.strip_praw_subscription(subscription)
|
||||
self._subscription_data.append(data)
|
||||
|
||||
data = self._subscription_data[index]
|
||||
subreddit_info = "/r/" + data['name'] + " - " + data['title']
|
||||
data['split_title'] = wrap_text(subreddit_info, width=n_cols)
|
||||
data['n_rows'] = len(data['split_title']) + 3
|
||||
data['offset'] = 0
|
||||
|
||||
return data
|
||||
|
||||
Reference in New Issue
Block a user