Skip to content

Commit

Permalink
Merge pull request #5 from B3rry/release/2.0.0
Browse files Browse the repository at this point in the history
Release/2.0.0
  • Loading branch information
B3rry authored May 9, 2020
2 parents 2c75887 + d5796a4 commit aa518c2
Show file tree
Hide file tree
Showing 21 changed files with 101 additions and 67 deletions.
Binary file added .coverage
Binary file not shown.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Balboa was initially created for [/r/survivor](https://www.reddit.com/r/survivor
## Getting Started

### Prerequisites
* [Python 2.7.x](https://docs.python.org/2/ "documentation")
* [Python 3.7.x](https://docs.python.org/3/ "documentation")
* [pip](https://pip.pypa.io/en/stable/installing/ "installation instructions")


Expand Down
1 change: 1 addition & 0 deletions actions/action_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self, requestIdentifier, payload, reddit):
'BULK_UPDATE_FLAIRS': lambda: BulkUpdateFromCSV(payload, reddit).complete,
}

# TODO: Catch errors parsing permissions, as this results in a loop otherwise. "config/permissions on the wiki having `PING` instead of `PINGPONG` caused loop."
activePermissions = Permissions(reddit).currentPermissions
botPermissions = activePermissions['bot']
requestPermissions = activePermissions['actions'][requestIdentifier]
Expand Down
23 changes: 16 additions & 7 deletions actions/action_request.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import sys
from action_dispatch import ActionDispatch
from .action_dispatch import ActionDispatch

class ActionRequest:
# ActionRequest is triggered _any_ time a message is recieved. In short, init defines the list of possible actions, and known exceptions, and determines if the request will
Expand Down Expand Up @@ -28,29 +28,38 @@ def __init__(self, request, payload, reddit):
'comment reply'
]

print('Message: /u/' + str(payload.author) + " sent '" + str(request) + "': " + str(payload.body) )
# Decoding the initial request aftern utf-8 sanitization
decodedRequest = request.decode('utf-8')

print('Message: /u/' + str(payload.author) + " sent '" + str(decodedRequest) + "': " + str(payload.body) )
sys.stdout.flush()

# Try to see if requestIdentifier (the subject line of the dispatched message) is in the requestCommand dict
# TODO - JRB: Suppport the passing of args in the requestIdentifier line
try:
requestIdentifier = requestCommand[request]
requestIdentifier = requestCommand[decodedRequest]

# Except messages where requestIdentifier does not exist in the requestCommand dict
except KeyError:
exception = request
except KeyError as e:
exception = decodedRequest
# If this is a non-actionable message (such as notifications about replies to posts or comments, mark them as read and take no action.)
if exception in knownExceptions:
print(str(decodedRequest) + " is on the list of known exceptions. Taking no action, marking message as read. ")
sys.stdout.flush()
payload.mark_read()
# Else dispatch a message and mark as read if the provided requestIdentifier doesn't exist in the requestCommand or knownExceptions dicts
else:
print(str(decodedRequest) + " does not match a known action or known exception. Replying with error message...")
sys.stdout.flush()
# TODO - JRB: I think this message should be more generic(?)
reddit.redditor(str(payload.author)).message('An Error Occurred', 'An error occured. Please contact the subreddit moderators.')
reddit.redditor(str(payload.author)).message('An Error Occurred', 'An error occured. Please contact the subreddit moderators: (Key Error: ' + str(e) + ')')
payload.mark_read()
# Catch all other excpetions
except Exception as e:
reddit.redditor(str(payload.author)).message('An Error Occurred', 'An error occured. Please contact the subreddit moderators.')
reddit.redditor(str(payload.author)).message('An Error Occurred', 'An error occured. Please contact the subreddit moderators: (Exception: ' + str(e) + ')')
payload.mark_read()
# If all conditions are met, dispatch the action.
else:
print(str(decodedRequest) + " matches action " + str(requestIdentifier) + ". Dispatching Action...")
sys.stdout.flush()
ActionDispatch(requestIdentifier, payload, self.reddit)
10 changes: 5 additions & 5 deletions actions/reddit/configuration/get_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ def __init__(self, reddit):

def get_permissions(self):
try:
documentPath = os.environ.get("USERNAME") + '/config/permissions'
documentPath = os.getenv("USERNAME") + '/config/permissions'
print("* Updating permissions")
print(" * Looking for permissions at reddit.com/r/" + os.environ.get("SUBREDDIT") + '/wiki/' + documentPath + '...')
print(" * Looking for permissions at reddit.com/r/" + os.getenv("SUBREDDIT") + '/wiki/' + documentPath + '...')
sys.stdout.flush()
permissionsDefinition = self.reddit.subreddit(os.environ.get("SUBREDDIT")).wiki[documentPath].content_md
permissionsDefinition = self.reddit.subreddit(os.getenv("SUBREDDIT")).wiki[documentPath].content_md
except Exception as e:
print(" * No permissions found at reddit.com/r/" + os.environ.get("SUBREDDIT") + '/wiki/' + documentPath + "")
print(" * No permissions found at reddit.com/r/" + os.getenv("SUBREDDIT") + '/wiki/' + documentPath + "")
print("* Updating permissionss failed. Continuing with no active permissionss.")
sys.stdout.flush()
self.status = {
'statusCode': 400,
'subject': 'Error: Permissions Not Found',
'message': "Permissions definition cannot be found. Confirm your permissions definition is located at reddit.com/r/" + os.environ.get("SUBREDDIT") + '/wiki/' + path + " :" + str(e)
'message': "Permissions definition cannot be found. Confirm your permissions definition is located at reddit.com/r/" + os.getenv("SUBREDDIT") + '/wiki' + documentPath + " :" + str(e)
}
else:
self.parse_permissions(permissionsDefinition)
Expand Down
6 changes: 3 additions & 3 deletions actions/reddit/flair/bulk_update_from_csv.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import sys
import csv
import urllib
import urllib.request
import praw

class BulkUpdateFromCSV:
Expand All @@ -15,15 +15,15 @@ def __init__(self, payload, reddit):

remoteURL = str(payload.body)

target_sub = os.environ.get("SUBREDDIT")
target_sub = os.getenv("SUBREDDIT")
subreddit = reddit.subreddit(target_sub)

flair_dicts = []

keys = ['user', 'flair_text', 'flair_css_class']
print('starting bulk update')
sys.stdout.flush()
remoteFile = urllib.urlretrieve(remoteURL)
remoteFile = urllib.request.urlretrieve(remoteURL)
# fileData = remoteFile.read()

print(remoteFile[0])
Expand Down
10 changes: 5 additions & 5 deletions actions/reddit/flair/rule_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ def __init__(self, reddit):

def get_rules(self):
try:
path = os.environ.get("USERNAME") + '/rules'
path = os.getenv("USERNAME") + '/rules'
print("* Updating rulesets")
print(" * Looking for ruleset at reddit.com/r/" + os.environ.get("SUBREDDIT") + '/wiki/' + path + '...')
print(" * Looking for ruleset at reddit.com/r/" + os.getenv("SUBREDDIT") + '/wiki/' + path + '...')
sys.stdout.flush()
ruledef = self.reddit.subreddit(os.environ.get("SUBREDDIT")).wiki[path].content_md
ruledef = self.reddit.subreddit(os.getenv("SUBREDDIT")).wiki[path].content_md
except Exception as e:
print(" * No ruleset found at reddit.com/r/" + os.environ.get("SUBREDDIT") + '/wiki/' + path + "")
print(" * No ruleset found at reddit.com/r/" + os.getenv("SUBREDDIT") + '/wiki/' + path + "")
print("* Updating rulesets failed. Continuing with no active rulesets.")
sys.stdout.flush()
self.status = {
'statusCode': 400,
'subject': 'Error: Flair Rules Not Found',
'message': "Ruleset cannot be found. Confirm your ruleset is located at reddit.com/r/" + os.environ.get("SUBREDDIT") + '/wiki/' + path + " :" + str(e)
'message': "Ruleset cannot be found. Confirm your ruleset is located at reddit.com/r/" + os.getenv("SUBREDDIT") + '/wiki/' + path + " :" + str(e)
}
else:
self.parse_rules(ruledef)
Expand Down
4 changes: 2 additions & 2 deletions actions/reddit/flair/update_flair_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, payload, reddit):
author = str(payload.author)
content = str(payload.body)
current_class = None
target_sub = os.environ.get("SUBREDDIT")
target_sub = os.getenv("SUBREDDIT")
subreddit = reddit.subreddit(target_sub)

for user in subreddit.flair.__call__(redditor=author):
Expand All @@ -24,7 +24,7 @@ def __init__(self, payload, reddit):

self.status['statusCode'] = 200
self.status['subject'] = 'Flair Changed'
self.status['message'] = author + ', your flair on /r/' + os.environ.get("SUBREDDIT") + ' has been updated to: ' + content + '. This bot is in beta. Not seeing your flair update? Please contact your moderator.'
self.status['message'] = author + ', your flair on /r/' + os.getenv("SUBREDDIT") + ' has been updated to: ' + content + '. This bot is in beta. Not seeing your flair update? Please contact your moderator.'


@property
Expand Down
4 changes: 2 additions & 2 deletions actions/reddit/flair/update_flair_text_by_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, payload, reddit):
author = str(payload.author)
content = str(payload.body)
current_class = None
target_sub = os.environ.get("SUBREDDIT")
target_sub = os.getenv("SUBREDDIT")
subreddit = reddit.subreddit(target_sub)

for user in subreddit.flair.__call__(redditor=author):
Expand All @@ -24,7 +24,7 @@ def __init__(self, payload, reddit):

self.status['statusCode'] = 200
self.status['subject'] = 'Flair Changed'
self.status['message'] = author + ', your flair on /r/' + os.environ.get("SUBREDDIT") + ' has been updated to: ' + content + '. This bot is in beta. Not seeing your flair update? Please contact your moderator.'
self.status['message'] = author + ', your flair on /r/' + os.getenv("SUBREDDIT") + ' has been updated to: ' + content + '. This bot is in beta. Not seeing your flair update? Please contact your moderator.'


@property
Expand Down
42 changes: 31 additions & 11 deletions actions/reddit/flair/update_flair_with_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import re
import calendar
from time import gmtime, strftime
from rule_parse import Rules
from .rule_parse import Rules

class UpdateFlairWithRules:

def __init__(self, payload, reddit):
author = str(payload.author)
content = str(payload.body)
print("CONTENT IS:")
print(content)
print("CONTENT ********")
sys.stdout.flush()
# Set a default status to be overwritten
self.status = {
'statusCode': 0,
Expand All @@ -26,26 +30,38 @@ def __init__(self, payload, reddit):
}
current_class = ''
new_class = ''
target_sub = os.environ.get("SUBREDDIT")
target_sub = os.getenv("SUBREDDIT")
subreddit = reddit.subreddit(target_sub)

# retrieve the current CSS class for the user's flair, otherwise set thier current css class to an empty string.
for user in subreddit.flair.__call__(redditor=author):
current_class = user['flair_css_class']
if current_class == None:
current_class = ''
# todo: figure out why this is here.... it looks to set rules, but idk why it's in this "if" statement
ruleset = Rules(reddit).findRuleset

try:
ruleset = ruleset[content]
rulesetIndex = ruleset[content]
except KeyError:
ruleset = False
self.status['statusCode'] = 200
self.status['subject'] = 'Invalid Flair for /r/' + os.environ.get("SUBREDDIT")
self.status['message'] = '/u/' + author + ',\n\nThe flair you requested ("' + content + '") is not valid. Please try again.\n\nPlease go to the [list of available flairs](https://www.reddit.com/r/survivor/wiki/available_flairs) and follow the instructions provided.\n\nIf you continue to experience errors, please [message the /r/' + os.environ.get("SUBREDDIT") + ' moderators](https://www.reddit.com/message/compose/?to=/r/Survivor&subject=Help%20with%20flair) for help.'
self.status['subject'] = 'Invalid Flair for /r/' + os.getenv("SUBREDDIT")
self.status['message'] = '/u/' + author + ',\n\nThe flair you requested ("' + content + '") is not valid. Please try again.\n\nPlease go to the [list of available flairs](https://www.reddit.com/r/survivor/wiki/available_flairs) and follow the instructions provided.\n\nIf you continue to experience errors, please [message the /r/' + os.getenv("SUBREDDIT") + ' moderators](https://www.reddit.com/message/compose/?to=/r/Survivor&subject=Help%20with%20flair) for help.'
except Exception as e:
print('exception: ' + str(e))
sys.stdout.flush()
else:
print('Valid ruleset found for requested flair: ' + str(rulesetIndex))
sys.stdout.flush()
process_rules = Rules(reddit).currentRules
process_rules = process_rules[ruleset]
process_rules = process_rules[rulesetIndex]

print('process rules by ruleset: ' + str(process_rules))
sys.stdout.flush()

for index, rule in enumerate(process_rules['set']['class']):
# enumerate through each class element to conditionally apply logic to the construction of a new css class
will_apply_rule = True
# Check to see if time on rule is valid
if 'limit' in rule:
Expand All @@ -57,13 +73,17 @@ def __init__(self, payload, reddit):
if 'expires' in rule['limit']:
if calendar.timegm(gmtime()) >= rule['limit']['expires']:
will_apply_rule = False

# When rule is eligible, apply it via the method speified as the "type"
if will_apply_rule:
if rule['type'] == 'string':
new_class = new_class + str(rule['content'])
elif rule['type'] == 'regex':
print("applying regex rule")
sys.stdout.flush()
# TODO: seems to be failing somewhere in here, probably due to python 3 encdoing shit
regex = rule['content'].encode('ascii', 'ignore')
result = re.search(regex, current_class)
decodedRegex = regex.decode('ascii')
result = re.search(decodedRegex, current_class)
if result is None:
new_class = new_class
else:
Expand All @@ -73,11 +93,11 @@ def __init__(self, payload, reddit):
except:
self.status['statusCode'] = 200
self.status['subject'] = 'An Error Occurred'
self.status['message'] = author + ', your requested flair of "' + content + '" on /r/' + os.environ.get("SUBREDDIT") + ' is not valid. This bot is in beta. Is this an error? Please contact your moderator.'
self.status['message'] = author + ', your requested flair of "' + content + '" on /r/' + os.getenv("SUBREDDIT") + ' is not valid. This bot is in beta. Is this an error? Please contact your moderator.'
else:
self.status['statusCode'] = 200
self.status['subject'] = '/r/' + os.environ.get("SUBREDDIT") + ' Flair Updated'
self.status['message'] = '/u/' + author + ',\n\nYour flair has been updated to "' + content + '".\n\nIf your updated flair is incorrect, [message the /r/' + os.environ.get("SUBREDDIT") + ' moderators](https://www.reddit.com/message/compose/?to=/r/Survivor&subject=Help%20with%20flair) for help.'
self.status['subject'] = '/r/' + os.getenv("SUBREDDIT") + ' Flair Updated'
self.status['message'] = '/u/' + author + ',\n\nYour flair has been updated to "' + content + '".\n\nIf your updated flair is incorrect, [message the /r/' + os.getenv("SUBREDDIT") + ' moderators](https://www.reddit.com/message/compose/?to=/r/Survivor&subject=Help%20with%20flair) for help.'


@property
Expand Down
6 changes: 3 additions & 3 deletions actions/reddit/flair/update_flair_with_rules_by_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import re
import calendar
from time import gmtime, strftime
from rule_parse import Rules
from .rule_parse import Rules

class UpdateFlairWithRulesByUser:

Expand All @@ -19,7 +19,7 @@ def __init__(self, payload, reddit):
content = str(payload.body)
current_class = None
new_class = ''
target_sub = os.environ.get("SUBREDDIT")
target_sub = os.getenv("SUBREDDIT")
subreddit = reddit.subreddit(target_sub)

for user in subreddit.flair.__call__(redditor=author):
Expand Down Expand Up @@ -55,7 +55,7 @@ def __init__(self, payload, reddit):

self.status['statusCode'] = 200
self.status['subject'] = 'Flair Changed'
self.status['message'] = author + ', your flair on /r/' + os.environ.get("SUBREDDIT") + ' has been updated to: ' + content + '. This bot is in beta. Not seeing your flair update? Please contact your moderator.'
self.status['message'] = author + ', your flair on /r/' + os.getenv("SUBREDDIT") + ' has been updated to: ' + content + '. This bot is in beta. Not seeing your flair update? Please contact your moderator.'


@property
Expand Down
2 changes: 1 addition & 1 deletion actions/reddit/redditor/banned_relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, payload, reddit):
'message': str(userToCheck)
}

target_sub = os.environ.get("SUBREDDIT")
target_sub = os.getenv("SUBREDDIT")
subreddit = reddit.subreddit(target_sub)
callBannedList = subreddit.banned.__call__(redditor=userToCheck)
self.is_banned = userToCheck in callBannedList
Expand Down
2 changes: 1 addition & 1 deletion actions/reddit/redditor/moderator_relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, payload, reddit):
'message': str(userToCheck)
}

target_sub = os.environ.get("SUBREDDIT")
target_sub = os.getenv("SUBREDDIT")
subreddit = reddit.subreddit(target_sub)
callModeratorList = subreddit.moderator.__call__(redditor=userToCheck)
self.is_moderator = userToCheck in callModeratorList
Expand Down
Loading

0 comments on commit aa518c2

Please sign in to comment.