From 003eb2b958866849f96e2cc08b4838d51f70c001 Mon Sep 17 00:00:00 2001 From: woorst Date: Thu, 13 Jul 2017 11:45:07 -0500 Subject: [PATCH 1/4] praw fixes --- rtv/packages/praw/__init__.py | 4 ++-- rtv/packages/praw/objects.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rtv/packages/praw/__init__.py b/rtv/packages/praw/__init__.py index 0c12d9c4..71a8f1de 100644 --- a/rtv/packages/praw/__init__.py +++ b/rtv/packages/praw/__init__.py @@ -358,7 +358,7 @@ def _req_error(*_, **__): self.http.proxies['http'] = self.config.http_proxy if self.config.https_proxy: self.http.proxies['https'] = self.config.https_proxy - self.modhash = None + self.modhash = '' # Check for updates if permitted and this is the first Reddit instance # if not disable_update_check and not BaseReddit.update_checked \ @@ -631,7 +631,7 @@ def request_json(self, url, params=None, data=None, as_objects=True, # Update the modhash if isinstance(data, dict) and 'data' in data \ and 'modhash' in data['data']: - self.modhash = data['data']['modhash'] + self.modhash = data['data']['modhash'] or '' return data diff --git a/rtv/packages/praw/objects.py b/rtv/packages/praw/objects.py index a4f94f56..3be5e11e 100644 --- a/rtv/packages/praw/objects.py +++ b/rtv/packages/praw/objects.py @@ -1688,7 +1688,7 @@ def __init__(self, reddit_session, author=None, name=None, json_dict=None, fetch=False, **kwargs): """Construct an instance of the Multireddit object.""" author = six.text_type(author) if author \ - else json_dict['path'].split('/')[-3] + else json_dict['path'].split('/')[-4] if not name: name = json_dict['path'].split('/')[-1] From 41560f09703528637feb0546615ae2dc72fa358e Mon Sep 17 00:00:00 2001 From: woorst Date: Thu, 13 Jul 2017 12:26:42 -0500 Subject: [PATCH 2/4] subscription management --- rtv/content.py | 11 ++++- rtv/subscription_page.py | 91 +++++++++++++++++++++++++++++++++++----- 2 files changed, 89 insertions(+), 13 deletions(-) diff --git a/rtv/content.py b/rtv/content.py index 3c0537db..04f03ff1 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -264,8 +264,8 @@ def strip_praw_subscription(subscription): else: data['type'] = 'Subscription' data['name'] = "/r/" + subscription.display_name - data['title'] = subscription.title - + data['title'] = subscription.title if subscription._has_fetched \ + else '' return data @staticmethod @@ -694,6 +694,13 @@ def from_user(cls, reddit, loader, content_type='subreddit'): return cls(name, items, loader) + @classmethod + def from_multireddit(cls, reddit, loader, multi): + items = iter(multi.subreddits) + content = cls('My Multireddit: {}'.format(multi.path), items, loader) + content._multireddit = multi + return content + @property def range(self): return 0, len(self._subscription_data) - 1 diff --git a/rtv/subscription_page.py b/rtv/subscription_page.py index 024d8b5d..329a1476 100644 --- a/rtv/subscription_page.py +++ b/rtv/subscription_page.py @@ -7,6 +7,7 @@ from .page import Page, PageController from .content import SubscriptionContent, SubredditContent from .objects import Color, Navigator, Command +from .packages import praw class SubscriptionController(PageController): @@ -43,17 +44,49 @@ def refresh_content(self, order=None, name=None): self.nav = Navigator(self.content.get) @SubscriptionController.register(Command('PROMPT')) - def prompt_subreddit(self): - "Open a prompt to navigate to a different subreddit" - - name = self.term.prompt_input('Enter page: /') - if name is not None: - with self.term.loader('Loading page'): - content = SubredditContent.from_name( - self.reddit, name, self.term.loader) - if not self.term.loader.exception: - self.selected_subreddit = content - self.active = False + def add_subreddit(self): + "Open a prompt to add or remove multi/subreddit" + + context = self.content.name + obj = self.get_selected_item()['object'] + + if context == 'My Subreddits': + name = self.term.prompt_input('Subscribe to: /') + if name is not None: + with self.term.loader('Adding /r/{}'.format(name, context)): + for n in name.split('+'): + try: + self.reddit.get_subreddit(n).subscribe() + except: + pass + elif context == 'My Multireddits': + name = self.term.prompt_input('Initialize multireddit ' + '(multi/sub+sub): ') + if name is not None: + try: + multi, subreddits = name.split('/') + subreddits = subreddits.split('+') + except: + self.term.show_notification('Initialization Format: ' + 'multireddit/subreddit+subreddit+...') + return None + else: + with self.term.loader('Creating {} with {}'.format(multi, + subreddits)): + self.reddit.create_multireddit(multi, + subreddits=subreddits) + elif context.startswith('My Multireddit:'): + name = self.term.prompt_input('Add subreddits(s): /') + if name is not None: + with self.term.loader('Adding /r/{}'.format(name)): + for n in name.split('+'): + try: + self.content._multireddit.add_subreddit(n) + except: + pass + else: + return None + self.refresh_content() @SubscriptionController.register(Command('SUBSCRIPTION_SELECT')) def select_subreddit(self): @@ -73,6 +106,42 @@ def close_subscriptions(self): self.active = False + @SubscriptionController.register(Command('DELETE')) + def delete_reddit(self): + "Delete the selected reddit" + + context = self.content.name + data = self.get_selected_item() + listing = data['object'] + name = data['name'] + + if isinstance(listing, praw.objects.Multireddit) and \ + self.term.prompt_y_or_n('Delete {}? (y/n): '.format(name)): + with self.term.loader('Deleting {} from {}'.format(name, context)): + self.reddit.delete_multireddit(listing.name) + self.refresh_content() + else: + with self.term.loader('Deleting {} from {}'.format(name, context)): + if hasattr(self.content, '_multireddit'): + self.content._multireddit.add_subreddit( + listing.display_name, _delete=True) + elif isinstance(listing, praw.objects.Subreddit): + listing.unsubscribe() + self.refresh_content() + + @SubscriptionController.register(Command('SUBMISSION_TOGGLE_COMMENT')) + def open_multireddit(self): + "View content of multireddit" + + context = self.content.name + multi = self.get_selected_item()['object'] + if context == 'My Multireddits': + with self.term.loader(): + self.content = SubscriptionContent.from_multireddit( + self.reddit, self.term.loader, multi) + if not self.term.loader.exception: + self.nav = Navigator(self.content.get) + def _draw_banner(self): # Subscriptions can't be sorted, so disable showing the order menu pass From 1069f7c278a39d3a81a329b83344a8076849b70b Mon Sep 17 00:00:00 2001 From: woorst Date: Thu, 13 Jul 2017 12:41:49 -0500 Subject: [PATCH 3/4] controls for subcription management --- CONTROLS.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/CONTROLS.rst b/CONTROLS.rst index e8ad399b..eefd7240 100644 --- a/CONTROLS.rst +++ b/CONTROLS.rst @@ -73,3 +73,24 @@ In submission mode you can view the self text for a submission and browse commen :``o`` or ``ENTER``: Open the comment permalink with your web browser :``SPACE``: Fold the selected comment, or load additional comments :``b``: Display URLs with urlview + +----------------- +Subscription Mode +----------------- + +In subscription mode you can view either the subreddits you are subscribed to +or the multireddits you've curated. + +:``SPACE``: View contents of multireddit +:``d``: Unsubscribe from subreddit or delete multireddit + +The ``/`` prompt allows you to subscribe to subreddits or create multireddits. + +When viewing your subscriptions you may add new subscriptions: + +* ``python`` +* ``python+commandline+linux`` + +When viewing a list of your multireddits you may initialize a new one: + +``programming/python+ruby+haskell`` From b5193daa4fca7cc8e19c29cdaf810f11b2ed028b Mon Sep 17 00:00:00 2001 From: woorst Date: Thu, 13 Jul 2017 22:18:04 -0500 Subject: [PATCH 4/4] additional control documentation --- CONTROLS.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTROLS.rst b/CONTROLS.rst index eefd7240..e6c7e236 100644 --- a/CONTROLS.rst +++ b/CONTROLS.rst @@ -81,6 +81,7 @@ Subscription Mode In subscription mode you can view either the subreddits you are subscribed to or the multireddits you've curated. +:``l`` or ``►``: Open the selected subreddit in a new window :``SPACE``: View contents of multireddit :``d``: Unsubscribe from subreddit or delete multireddit