From 45b28547a98a7dca5fa9f9fc974e4559ad5151dd Mon Sep 17 00:00:00 2001 From: johnguy94 Date: Wed, 28 Sep 2016 20:37:08 +0100 Subject: [PATCH 1/7] Added quote plugin Added basic functionality, requires unit test --- csbot/plugins/quote.py | 94 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 csbot/plugins/quote.py diff --git a/csbot/plugins/quote.py b/csbot/plugins/quote.py new file mode 100644 index 00000000..98cfa3b5 --- /dev/null +++ b/csbot/plugins/quote.py @@ -0,0 +1,94 @@ +import json +from ..plugin import Plugin + +class Quote(Plugin): + """Quote people out of context for fun and for profit. + Add quotes, recall quotes + """ + class QUOTEERROR(Exception): + pass + + def _quote(self,in_quote_num=None): + quote = {} + if in_quote_num == None: + raise self.QUOTEERROR("No quote number passed.") + #check format of quote_numb + quote["ID"] = 0 + try: + quote["ID"] = int(in_quote_num) + except: + raise self.QUOTEERROR("Quote number passed not integer.") + + #open the quotes file, and read the quote + quotes = {} + try: + with open("quotes.json","r") as q_file: + try: + quotes = json.loads(q_file.read()) + except: + raise self.QUOTEERROR("Quotes JSON file format error.") + except: + raise self.QUOTEERROR("Quote file doesn't exist yo.") + + #get quote + quote["text"] = "" + try: + quote["text"] = quotes[str(quote_num)] + except: + return (quote["ID"],"NOT FOUND") + + return (quote["ID"],quote["text"]) + + def _addquote(self,in_quote_text=""): + if in_quote_text == "": + raise self.QUOTEERROR("Empty quote!") + quote = {} #output + + #open up quotes file + quotes = {} + try: + with open("quotes.json","r") as q_file: + quotes = json.loads(q_file.read()) + except: + #quote file doesn't exist + quotes = {"quote_count": 1, '0': "Test quote please ignore."} + + #read the last key used, , this becomes the quote id, add one to it + num_of_quotes = 0 + try: + num_of_quotes = quotes["quote_count"] + except: + raise self.QUOTEERROR("Can't find quote count in quote dictionary.") + quotes[num_of_quotes] = in_quote_text + quote["ID"] = num_of_quotes + quotes["quote_count"] = quotes[num_of_quotes] + 1 + + #write quote file back + try: + with open("quotes.json","w+") as q_file: + q_file.write(json.dumps(quotes)) + except: + raise self.QUOTEERROR("Unable to write quote file back") + + quote["text"] = in_quote_text + + return(quote["text"],quote["ID"]) + + + @Plugin.command('quote',help="quote ") + def quote(self, e): + """quote somebody + """ + try: + e.reply("#{}: {}".format(*self._quote(e["data"]))) + except self.QUOTEERROR as ex: + e.reply(str(ex)) + + @Plugin.command('addquote',help="addquote ") + def addquote(self, e): + """add a quote to somebody + """ + try: + e.reply("\"{}\" added as quote #{}.".format(*self._addquote(e["data"]))) + except self.QUOTEERROR as ex: + e.reply(str(ex)) \ No newline at end of file From 3c40dd3fab15f713782cdd0d688758182709bbed Mon Sep 17 00:00:00 2001 From: johnguy94 Date: Thu, 29 Sep 2016 00:56:58 +0100 Subject: [PATCH 2/7] Working quote set and retrieval --- csbot/plugins/quote.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/csbot/plugins/quote.py b/csbot/plugins/quote.py index 98cfa3b5..912135c4 100644 --- a/csbot/plugins/quote.py +++ b/csbot/plugins/quote.py @@ -33,7 +33,7 @@ def _quote(self,in_quote_num=None): #get quote quote["text"] = "" try: - quote["text"] = quotes[str(quote_num)] + quote["text"] = quotes[str(in_quote_num)] except: return (quote["ID"],"NOT FOUND") @@ -61,7 +61,7 @@ def _addquote(self,in_quote_text=""): raise self.QUOTEERROR("Can't find quote count in quote dictionary.") quotes[num_of_quotes] = in_quote_text quote["ID"] = num_of_quotes - quotes["quote_count"] = quotes[num_of_quotes] + 1 + quotes["quote_count"] = num_of_quotes + 1 #write quote file back try: @@ -91,4 +91,4 @@ def addquote(self, e): try: e.reply("\"{}\" added as quote #{}.".format(*self._addquote(e["data"]))) except self.QUOTEERROR as ex: - e.reply(str(ex)) \ No newline at end of file + e.reply(str(ex)) From 31a23fcc158ef6e61f68edc9adfff29d93bbd6d0 Mon Sep 17 00:00:00 2001 From: johnguy94 Date: Fri, 30 Sep 2016 14:43:24 +0100 Subject: [PATCH 3/7] Fixed wording on comment --- csbot/plugins/quote.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/csbot/plugins/quote.py b/csbot/plugins/quote.py index 912135c4..a1217aaf 100644 --- a/csbot/plugins/quote.py +++ b/csbot/plugins/quote.py @@ -86,7 +86,7 @@ def quote(self, e): @Plugin.command('addquote',help="addquote ") def addquote(self, e): - """add a quote to somebody + """add a quote """ try: e.reply("\"{}\" added as quote #{}.".format(*self._addquote(e["data"]))) From d6132479e19dcb0a5bd2caa51b248029062f881a Mon Sep 17 00:00:00 2001 From: johnguy94 Date: Sat, 1 Oct 2016 21:27:27 +0100 Subject: [PATCH 4/7] Fixed pep8 issues and CamelCase --- csbot/plugins/quote.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/csbot/plugins/quote.py b/csbot/plugins/quote.py index a1217aaf..c630334f 100644 --- a/csbot/plugins/quote.py +++ b/csbot/plugins/quote.py @@ -1,23 +1,24 @@ import json +import last from ..plugin import Plugin class Quote(Plugin): """Quote people out of context for fun and for profit. Add quotes, recall quotes """ - class QUOTEERROR(Exception): + class QuoteError(Exception): pass - def _quote(self,in_quote_num=None): + def _quote(self, in_quote_num=None): quote = {} if in_quote_num == None: - raise self.QUOTEERROR("No quote number passed.") + raise self.QuoteError("No quote number passed.") #check format of quote_numb quote["ID"] = 0 try: quote["ID"] = int(in_quote_num) except: - raise self.QUOTEERROR("Quote number passed not integer.") + raise self.QuoteError("Quote number passed not integer.") #open the quotes file, and read the quote quotes = {} @@ -26,22 +27,22 @@ def _quote(self,in_quote_num=None): try: quotes = json.loads(q_file.read()) except: - raise self.QUOTEERROR("Quotes JSON file format error.") + raise self.QuoteError("Quotes JSON file format error.") except: - raise self.QUOTEERROR("Quote file doesn't exist yo.") + raise self.QuoteError("Quote file doesn't exist yo.") #get quote quote["text"] = "" try: quote["text"] = quotes[str(in_quote_num)] except: - return (quote["ID"],"NOT FOUND") + return (quote["ID"], "NOT FOUND") - return (quote["ID"],quote["text"]) + return (quote["ID"], quote["text"]) - def _addquote(self,in_quote_text=""): + def _addquote(self, in_quote_text=""): if in_quote_text == "": - raise self.QUOTEERROR("Empty quote!") + raise self.QuoteError("Empty quote!") quote = {} #output #open up quotes file @@ -58,7 +59,7 @@ def _addquote(self,in_quote_text=""): try: num_of_quotes = quotes["quote_count"] except: - raise self.QUOTEERROR("Can't find quote count in quote dictionary.") + raise self.QuoteError("Can't find quote count in quote dictionary.") quotes[num_of_quotes] = in_quote_text quote["ID"] = num_of_quotes quotes["quote_count"] = num_of_quotes + 1 @@ -68,27 +69,27 @@ def _addquote(self,in_quote_text=""): with open("quotes.json","w+") as q_file: q_file.write(json.dumps(quotes)) except: - raise self.QUOTEERROR("Unable to write quote file back") + raise self.QuoteError("Unable to write quote file back") quote["text"] = in_quote_text - return(quote["text"],quote["ID"]) + return(quote["text"], quote["ID"]) - @Plugin.command('quote',help="quote ") + @Plugin.command('quote', help="quote ") def quote(self, e): """quote somebody """ try: e.reply("#{}: {}".format(*self._quote(e["data"]))) - except self.QUOTEERROR as ex: + except self.QuoteError as ex: e.reply(str(ex)) - @Plugin.command('addquote',help="addquote ") + @Plugin.command('addquote', help="addquote ") def addquote(self, e): """add a quote """ try: e.reply("\"{}\" added as quote #{}.".format(*self._addquote(e["data"]))) - except self.QUOTEERROR as ex: + except self.QuoteError as ex: e.reply(str(ex)) From 2d706b49b7c7da828d79343d2d6fa0de8917851e Mon Sep 17 00:00:00 2001 From: johnguy94 Date: Sat, 1 Oct 2016 23:38:41 +0100 Subject: [PATCH 5/7] Moved to MongoDB add quote now uses last message by person, get quote now uses nick --- csbot/plugins/quote.py | 110 ++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 63 deletions(-) diff --git a/csbot/plugins/quote.py b/csbot/plugins/quote.py index c630334f..6ea63a0c 100644 --- a/csbot/plugins/quote.py +++ b/csbot/plugins/quote.py @@ -1,95 +1,79 @@ -import json import last +import random from ..plugin import Plugin class Quote(Plugin): """Quote people out of context for fun and for profit. Add quotes, recall quotes """ + db = Plugin.use('mongodb', collection='quote') + class QuoteError(Exception): pass - def _quote(self, in_quote_num=None): - quote = {} - if in_quote_num == None: - raise self.QuoteError("No quote number passed.") - #check format of quote_numb - quote["ID"] = 0 - try: - quote["ID"] = int(in_quote_num) - except: - raise self.QuoteError("Quote number passed not integer.") + def _quote(self, nick): + """Find a quote by person""" + if not nick: + self.QuoteError("No nick!") - #open the quotes file, and read the quote - quotes = {} - try: - with open("quotes.json","r") as q_file: - try: - quotes = json.loads(q_file.read()) - except: - raise self.QuoteError("Quotes JSON file format error.") - except: - raise self.QuoteError("Quote file doesn't exist yo.") - - #get quote - quote["text"] = "" - try: - quote["text"] = quotes[str(in_quote_num)] - except: - return (quote["ID"], "NOT FOUND") + #find quote by nick + quotes = self.db.find({'nick': nick}) + quote_list = list(quotes) + + if len(quote_list) == 0: + self.QuoteError("No quotes by", nick) + + #Randomly pick quote from returned quotes + quote_list = list(quotes) + quote = random.choice(quote_list) + + return (quote["text"], quote["nick"]) - return (quote["ID"], quote["text"]) + def _quoteExists(self, quote_post): + """Checks to see if a nick + quote already exists to stop repeats.""" + if self.db.find(quote_post).count > 0: + return True + else: + return False + + def _addquote(self, nick=""): + """Add quote from what person last said""" + + if not nick: + raise self.QuoteError("No nick!") - def _addquote(self, in_quote_text=""): - if in_quote_text == "": - raise self.QuoteError("Empty quote!") quote = {} #output + quote["quote"] = last.last_message(nick)[2] - #open up quotes file - quotes = {} - try: - with open("quotes.json","r") as q_file: - quotes = json.loads(q_file.read()) - except: - #quote file doesn't exist - quotes = {"quote_count": 1, '0': "Test quote please ignore."} - - #read the last key used, , this becomes the quote id, add one to it - num_of_quotes = 0 - try: - num_of_quotes = quotes["quote_count"] - except: - raise self.QuoteError("Can't find quote count in quote dictionary.") - quotes[num_of_quotes] = in_quote_text - quote["ID"] = num_of_quotes - quotes["quote_count"] = num_of_quotes + 1 - - #write quote file back - try: - with open("quotes.json","w+") as q_file: - q_file.write(json.dumps(quotes)) - except: - raise self.QuoteError("Unable to write quote file back") + if not quote["quote"]: + raise self.QuoteError("No Last message from nick, or nick not found.") + + #Create post object + post_quote = {'nick': nick, 'quote': quote["quote"]} - quote["text"] = in_quote_text + #check if quote already in database + if not _quoteExists(post_quote): + self.db.insert_one(post_quote) + else: + self.QuoteError("Quote already in Database.") - return(quote["text"], quote["ID"]) + return(quote["quote"], nick) - @Plugin.command('quote', help="quote ") + @Plugin.command('quote', help="quote [nick]") def quote(self, e): """quote somebody """ try: - e.reply("#{}: {}".format(*self._quote(e["data"]))) + e.reply("\"{}\" - {}".format(*self._quote(e["data"]))) except self.QuoteError as ex: e.reply(str(ex)) - @Plugin.command('addquote', help="addquote ") + @Plugin.command('addquote', help="addquote ") def addquote(self, e): """add a quote """ try: - e.reply("\"{}\" added as quote #{}.".format(*self._addquote(e["data"]))) + e.reply("\"{}\" - {} added as quote.".format(*self._addquote(e["data"]))) except self.QuoteError as ex: e.reply(str(ex)) From 5751473b9c2904cbefeab74bbf83e49ded51a0c0 Mon Sep 17 00:00:00 2001 From: johnguy94 Date: Sat, 1 Oct 2016 23:42:47 +0100 Subject: [PATCH 6/7] Fixed return error --- csbot/plugins/quote.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/csbot/plugins/quote.py b/csbot/plugins/quote.py index 6ea63a0c..a79d1dcc 100644 --- a/csbot/plugins/quote.py +++ b/csbot/plugins/quote.py @@ -23,11 +23,11 @@ def _quote(self, nick): if len(quote_list) == 0: self.QuoteError("No quotes by", nick) - #Randomly pick quote from returned quotes + #Find returns cursor, convert cursor to list, randomly pick quote from returned quotes quote_list = list(quotes) quote = random.choice(quote_list) - return (quote["text"], quote["nick"]) + return (quote["quote"], quote["nick"]) def _quoteExists(self, quote_post): """Checks to see if a nick + quote already exists to stop repeats.""" From 07be5d13e6bd8724a801e93f8efd090da9785eeb Mon Sep 17 00:00:00 2001 From: johnguy94 Date: Sat, 1 Oct 2016 23:44:29 +0100 Subject: [PATCH 7/7] Fixed reuse of list convert --- csbot/plugins/quote.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/csbot/plugins/quote.py b/csbot/plugins/quote.py index a79d1dcc..982dc033 100644 --- a/csbot/plugins/quote.py +++ b/csbot/plugins/quote.py @@ -18,13 +18,14 @@ def _quote(self, nick): #find quote by nick quotes = self.db.find({'nick': nick}) + + #Find returns cursor, convert cursor iterator to list, quote_list = list(quotes) if len(quote_list) == 0: self.QuoteError("No quotes by", nick) - #Find returns cursor, convert cursor to list, randomly pick quote from returned quotes - quote_list = list(quotes) + #Randomly pick quote from returned quotes quote = random.choice(quote_list) return (quote["quote"], quote["nick"])