From fcb639b8a37a8e83b2d0947a84094f87b90110f3 Mon Sep 17 00:00:00 2001 From: Joseph Gripenstraw Date: Tue, 4 Feb 2020 13:45:28 -0800 Subject: [PATCH] quoptes --- README.md | 2 ++ app.py | 3 ++- cohort_quotes.txt | 1 + modules/cohort_quotes.py | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 cohort_quotes.txt create mode 100644 modules/cohort_quotes.py diff --git a/README.md b/README.md index e49b8d0..caaf1bc 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ - !detain list - see detain history - !dierre - get a dierre picture and a quote :heart: - !door \ - ask to be let in +- !quote - get random cohort quote +- !quote add \ - add quote - !party \ \ \ - Let the group know there's a party at your place - !roseceremony - retrieve the list of current bachelor contestants - !train [TODO] \ \ - Lets you know when the next train/bus for at is coming diff --git a/app.py b/app.py index a13e006..18863ed 100755 --- a/app.py +++ b/app.py @@ -22,7 +22,7 @@ from modules.weather import weather_handler from modules.penny import penny_pic_handler from modules.tethics_dare import dare_handler - +from modules.cohort_quotes.py import cohort_quote_handler # Globs app = Flask(__name__) @@ -49,6 +49,7 @@ def call_handler(sender, message, bot_id, app_id): '!weather': weather_handler, '!penny': penny_pic_handler, '!dare': dare_handler + '!quote': cohort_quote_handler } # Get the function from handlers dictionary, add message as argument, return None on KeyError diff --git a/cohort_quotes.txt b/cohort_quotes.txt new file mode 100644 index 0000000..b8cc303 --- /dev/null +++ b/cohort_quotes.txt @@ -0,0 +1 @@ +"BALLS" - short stop Dierre \ No newline at end of file diff --git a/modules/cohort_quotes.py b/modules/cohort_quotes.py new file mode 100644 index 0000000..acc826b --- /dev/null +++ b/modules/cohort_quotes.py @@ -0,0 +1,34 @@ +import random + +def cohort_quote_handler(sender, message, bot_id, app_id): + message = message.strip().split() + if len(message) == 1: + return rand_quote() + elif len(message) > 2 and message[1] == 'add': + return add_quote(' '.join(message[3:])) + else: + return "Get random cohort quote.\n usage: !quote\n" + +def rand_quote(): + quotes = [] + with open('cohort_quotes.txt') as f: + for line in f: + quotes.append(line.strip()) + return quotes[random.randrange(len(quotes))] + +def add_quote(quote): + if '"' not in quote: + quote = '"' + quote + if '-' in quote: + quotes = quote.split('-') + quote = quotes[0]+'" - '+quotes[1] + else: + quote = quote+ '"' + + with open('cohort_quotes.txt','a') as f: + f.write('\n'+quote) + return quote + ' added' + +if __name__ == '__main__': + print(cohort_quote_handler('Dierre','!quote',' ',' ')) + print(cohort_quote_handler('Dierre','!quote add This is a new quote - Joe',' ',' '))