From cf2e670ad19c69c906abef021f488204f3abbfcf Mon Sep 17 00:00:00 2001 From: Frank Date: Sat, 8 Oct 2016 10:05:52 -0700 Subject: [PATCH] Fixed wordlist. Fixed whitelist. --- shreddit.py | 27 +++++++++++++++++---------- shreddit.yml.example | 5 +++-- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/shreddit.py b/shreddit.py index 373acf1..4252070 100755 --- a/shreddit.py +++ b/shreddit.py @@ -8,6 +8,7 @@ import json import yaml import praw import random +import string from re import sub from datetime import datetime, timedelta @@ -73,10 +74,11 @@ log.debug("Deleting messages before {time}.".format( whitelist = config.get('whitelist', []) whitelist_ids = config.get('whitelist_ids', []) -if config.get('whitelist'): +if whitelist: log.debug("Keeping messages from subreddits {subs}".format( subs=', '.join(whitelist)) ) + whitelist = set([string.lower(subr) for subr in whitelist]) def get_sentence(): @@ -87,14 +89,19 @@ try: except ImportError: # Module unavailable, use the default phrase pass -os_wordlist = config.get('wordlist', '/usr/share/dict/words') -if os.name == 'posix' and os.path.isfile(os_wordlist): - # Generate a random string of words from our system's dictionary - fh = open(os_wordlist) - words = fh.read().splitlines() - fh.close() - def get_sentence(): - return ' '.join(random.sample(words, random.randint(50,75))) + +if config.get('replacement_format') == 'random': + wordlist = config.get('wordlist') + if not wordlist: + os_wordlist = '/usr/share/dict/words' + if os.name == 'posix' and os.path.isfile(os_wordlist): + # Generate a random string of words from our system's dictionary + with open(os_wordlist) as fh: + wordlist = fh.read().splitlines() + if wordlist: + def get_sentence(): + return ' '.join(random.sample(wordlist, min(len(wordlist), + random.randint(50,75)))) def get_things(after=None): @@ -130,7 +137,7 @@ def remove_things(things): if thing_time < end_time: continue - if str(thing.subreddit).lower() in config.get('whitelist', []) \ + if str(thing.subreddit).lower() in whitelist \ or thing.id in config.get('whitelist_ids', []): continue diff --git a/shreddit.yml.example b/shreddit.yml.example index 49e5f7a..216eb5e 100644 --- a/shreddit.yml.example +++ b/shreddit.yml.example @@ -72,7 +72,8 @@ replacement_format: random # See: https://docs.python.org/3/library/logging.html#logging-levels debug: DEBUG -# Define your own wordlist to use as substitution text -wordlist: False +# Define your own wordlist to use as substitution text when +# replacement_format == random +wordlist: [] # vim: syntax=yaml ts=2