From 4c78f404e2a81d68f91452e51465c1ab5edf238b Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 28 Nov 2016 20:51:56 -0600 Subject: [PATCH 1/3] Allows users to whitelist and blacklist multireddits --- shreddit.yml.example | 4 ++++ shreddit/__init__.py | 2 ++ shreddit/shredder.py | 30 +++++++++++++++++++++++++----- shreddit/shreddit.yml.example | 4 ++++ 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/shreddit.yml.example b/shreddit.yml.example index 38088a8..498e98f 100644 --- a/shreddit.yml.example +++ b/shreddit.yml.example @@ -38,6 +38,10 @@ whitelist: [AskScience, TheCulture, redditdev, programming, charity, netsec] # If you want any specific posts to be whitelisted stick 'em in here whitelist_ids: [] +# If you want to whitelist or blacklist specific multireddits, add them here +multi_blacklist: [] +multi_whitelist: [] + # If you set this then no editing or deleting will be done # but the output from the program will be shown as an example trial_run: False diff --git a/shreddit/__init__.py b/shreddit/__init__.py index c5e676e..3ef0c40 100644 --- a/shreddit/__init__.py +++ b/shreddit/__init__.py @@ -4,6 +4,8 @@ default_config = {"username": None, "save_directory": "/tmp", "whitelist": [], "whitelist_ids": [], + "multi_blacklist": [], + "multi_whitelist": [], "item": "overview", "sort": "new", "whitelist_distinguished": True, diff --git a/shreddit/shredder.py b/shreddit/shredder.py index a59b001..4b7f91d 100644 --- a/shreddit/shredder.py +++ b/shreddit/shredder.py @@ -39,12 +39,29 @@ class Shredder(object): self._limit = None self._api_calls = [] + # Add any multireddit subreddits to the whitelist + self._whitelist = set([s.lower() for s in self._whitelist]) + for username, multiname in self._multi_whitelist: + multireddit = self._r.get_multireddit(username, multiname) + for subreddit in multireddit.subreddits: + self._whitelist.add(str(subreddit).lower()) + + # Add any multireddit subreddits to the blacklist + self._blacklist = set() + for username, multiname in self._multi_blacklist: + multireddit = self._r.get_multireddit(username, multiname) + for subreddit in multireddit.subreddits: + self._blacklist.add(str(subreddit).lower()) + + self._logger.info("Deleting ALL items before {}".format(self._nuke_cutoff)) self._logger.info("Deleting items not whitelisted until {}".format(self._recent_cutoff)) self._logger.info("Ignoring ALL items after {}".format(self._recent_cutoff)) self._logger.info("Targeting {} sorted by {}".format(self._item, self._sort)) + if self._blacklist: + self._logger.info("Deleting ALL items from subreddits {}".format(", ".join(list(self._blacklist)))) if self._whitelist: - self._logger.info("Keeping items from subreddits {}".format(", ".join(self._whitelist))) + self._logger.info("Keeping items from subreddits {}".format(", ".join(list(self._whitelist)))) if self._keep_a_copy and self._save_directory: self._logger.info("Saving deleted items to: {}".format(self._save_directory)) if self._trial_run: @@ -89,7 +106,7 @@ class Shredder(object): raise RateLimitExceeded("You're doing that too much.", e) self._logger.info("Logged in as {user}.".format(user=self._r.user)) - def _check_item(self, item): + def _check_whitelist(self, item): """Returns True if the item is whitelisted, False otherwise. """ if str(item.subreddit).lower() in self._whitelist or item.id in self._whitelist_ids: @@ -157,15 +174,18 @@ class Shredder(object): time.sleep(10) self._logger.debug("Examining item {}: {}".format(idx + 1, item)) created = arrow.get(item.created_utc) + if str(item.subreddit).lower() in self._blacklist: + self._logger.debug("Deleting due to blacklist") + self._remove(item) + elif self._check_whitelist(item): + self._logger.debug("Skipping due to: whitelisted") + continue if created <= self._nuke_cutoff: self._logger.debug("Item occurs prior to nuke cutoff") self._remove(item) elif created > self._recent_cutoff: self._logger.debug("Skipping due to: too recent") continue - elif self._check_item(item): - self._logger.debug("Skipping due to: whitelisted") - continue else: self._remove(item) return idx + 1 diff --git a/shreddit/shreddit.yml.example b/shreddit/shreddit.yml.example index 38088a8..498e98f 100644 --- a/shreddit/shreddit.yml.example +++ b/shreddit/shreddit.yml.example @@ -38,6 +38,10 @@ whitelist: [AskScience, TheCulture, redditdev, programming, charity, netsec] # If you want any specific posts to be whitelisted stick 'em in here whitelist_ids: [] +# If you want to whitelist or blacklist specific multireddits, add them here +multi_blacklist: [] +multi_whitelist: [] + # If you set this then no editing or deleting will be done # but the output from the program will be shown as an example trial_run: False From 6cb808c4032fd83ada87d0d38f733567f4aedb74 Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 28 Nov 2016 20:56:08 -0600 Subject: [PATCH 2/3] Improves config files self documentation --- shreddit.yml.example | 6 ++++++ shreddit/shreddit.yml.example | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/shreddit.yml.example b/shreddit.yml.example index 498e98f..3fe276b 100644 --- a/shreddit.yml.example +++ b/shreddit.yml.example @@ -1,6 +1,7 @@ --- # Login details for Reddit. Fill out if you don't wish # to be prompted for a login every time you run Shreddit. +# If your username or password has characters that could cause problems, surround them in quotes. ### NOTE: This may be deprecated as you can specify in praw.ini instead username: password: @@ -39,6 +40,11 @@ whitelist: [AskScience, TheCulture, redditdev, programming, charity, netsec] whitelist_ids: [] # If you want to whitelist or blacklist specific multireddits, add them here +# Each one must be a list of 2 elements: username, multireddit +# Example: +# multi_blacklist: +# - [myusername, mymulti] +# - [someotheruser, theirmulti] multi_blacklist: [] multi_whitelist: [] diff --git a/shreddit/shreddit.yml.example b/shreddit/shreddit.yml.example index 498e98f..3fe276b 100644 --- a/shreddit/shreddit.yml.example +++ b/shreddit/shreddit.yml.example @@ -1,6 +1,7 @@ --- # Login details for Reddit. Fill out if you don't wish # to be prompted for a login every time you run Shreddit. +# If your username or password has characters that could cause problems, surround them in quotes. ### NOTE: This may be deprecated as you can specify in praw.ini instead username: password: @@ -39,6 +40,11 @@ whitelist: [AskScience, TheCulture, redditdev, programming, charity, netsec] whitelist_ids: [] # If you want to whitelist or blacklist specific multireddits, add them here +# Each one must be a list of 2 elements: username, multireddit +# Example: +# multi_blacklist: +# - [myusername, mymulti] +# - [someotheruser, theirmulti] multi_blacklist: [] multi_whitelist: [] From ca83643526daff7e4a8fede4eeeb4d850b5ba5fa Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 28 Nov 2016 20:57:16 -0600 Subject: [PATCH 3/3] Fast forwards version number so that PyPI and PRAW agent agree --- setup.py | 2 +- shreddit/shredder.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 3b5497c..190179c 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import setup from codecs import open from os import path -VERSION = "3.0.0" +VERSION = "5.0.0" DESCRIPTION = " Remove your comment history on Reddit as deleting an account does not do so." here = path.abspath(path.dirname(__file__)) diff --git a/shreddit/shredder.py b/shreddit/shredder.py index 4b7f91d..80a9d23 100644 --- a/shreddit/shredder.py +++ b/shreddit/shredder.py @@ -79,7 +79,7 @@ class Shredder(object): self.shred() def _connect(self, praw_ini, username, password): - self._r = praw.Reddit(user_agent="shreddit/4.4") + self._r = praw.Reddit(user_agent="shreddit/5.0") if praw_ini: # PRAW won't panic if the file is invalid, so check first if not os.path.exists(praw_ini):