diff options
Diffstat (limited to 'Reaktor/titlebot/commands')
-rwxr-xr-x | Reaktor/titlebot/commands/clear | 12 | ||||
-rwxr-xr-x | Reaktor/titlebot/commands/down | 2 | ||||
-rwxr-xr-x | Reaktor/titlebot/commands/help | 10 | ||||
-rwxr-xr-x | Reaktor/titlebot/commands/list | 27 | ||||
-rwxr-xr-x | Reaktor/titlebot/commands/new | 19 | ||||
-rw-r--r-- | Reaktor/titlebot/commands/poll.py | 23 | ||||
-rwxr-xr-x | Reaktor/titlebot/commands/undo | 27 | ||||
-rwxr-xr-x | Reaktor/titlebot/commands/up | 27 |
8 files changed, 147 insertions, 0 deletions
diff --git a/Reaktor/titlebot/commands/clear b/Reaktor/titlebot/commands/clear new file mode 100755 index 00000000..e3558194 --- /dev/null +++ b/Reaktor/titlebot/commands/clear @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +import json +from os import environ +import sys +import os +# krebs polling +import poll + +f = 'suggestions.json' +title=" ".join(sys.argv[1:]) +db = poll.save_db(f,[]) +print("cleared database") diff --git a/Reaktor/titlebot/commands/down b/Reaktor/titlebot/commands/down new file mode 100755 index 00000000..8964382d --- /dev/null +++ b/Reaktor/titlebot/commands/down @@ -0,0 +1,2 @@ +#!/bin/sh +echo "not implemented" diff --git a/Reaktor/titlebot/commands/help b/Reaktor/titlebot/commands/help new file mode 100755 index 00000000..475ea98e --- /dev/null +++ b/Reaktor/titlebot/commands/help @@ -0,0 +1,10 @@ +#!/bin/sh +cat <<EOF +BGT Title Poll Bot: + .new TITLE - suggest a new title + .list <(age|votes)> - list all suggestions + .up NUM (NUM ...) - upvote one or more suggestions from .list + .undo NUM (NUM ...) - undo an upvote + .clear - clear the poll (auth required) +EOF + diff --git a/Reaktor/titlebot/commands/list b/Reaktor/titlebot/commands/list new file mode 100755 index 00000000..3a29919a --- /dev/null +++ b/Reaktor/titlebot/commands/list @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +import json +from os import environ +import sys +import os +import poll + +f = 'suggestions.json' +title=" ".join(sys.argv[1:]) +db = poll.load_db(f) +if len(sys.argv) > 1 and ("-h" in sys.argv[1] or "usage" == sys.argv[1]): + print("""usage: list <(age|votes)> + sort by age or by votes (default: age) +""") + sys.exit(0) + +if len(sys.argv) > 1 and ("votes" in sys.argv[1]): + use = poll.sort_by_votes(db) +elif len(sys.argv) > 1 and ("age" in sys.argv[1]) or len(sys.argv) == 1: + pass +else: + print("unknown sorting method") + sys.exit(1) + +for entry in poll.sort_by_votes(db): + print("#%d %s (votes: %d)" % + (db.index(entry),entry['title'],sum(entry['votes'].values()))) diff --git a/Reaktor/titlebot/commands/new b/Reaktor/titlebot/commands/new new file mode 100755 index 00000000..7246a2b2 --- /dev/null +++ b/Reaktor/titlebot/commands/new @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +import json +from os import environ +import sys +import os +# krebs polling +import poll + +f = 'suggestions.json' +title=" ".join(sys.argv[1:]) +db = poll.load_db(f) + +suggester = environ['_from'] +if not poll.title_in_db(title,db): + db.append( { 'by': suggester, + 'votes':{},'title': title}) + print("Thank you for your suggestion '%s'!"%environ["_from"]) + print("To vote type '.up %d'"%(len(db)-1)) +poll.save_db(f,db) diff --git a/Reaktor/titlebot/commands/poll.py b/Reaktor/titlebot/commands/poll.py new file mode 100644 index 00000000..595ab269 --- /dev/null +++ b/Reaktor/titlebot/commands/poll.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +import json +def load_db(f): + try: + with open(f) as fl: + return json.load(fl) + except: + #default db is [] + return [] + +def title_in_db(t,d): + for index,entry in enumerate(d): + if t == entry['title']: + print("Title is already in list.") + print("To vote for this type '.up %d'" %index) + return True + return False +def save_db(f,db): + with open(f,"w") as x: + json.dump(db,x) + +def sort_by_votes(db): + return sorted(db,key=lambda entry:sum(entry['votes'].values()),reverse=True) diff --git a/Reaktor/titlebot/commands/undo b/Reaktor/titlebot/commands/undo new file mode 100755 index 00000000..bebd57a3 --- /dev/null +++ b/Reaktor/titlebot/commands/undo @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +import json +from os import environ +import sys +import os +# krebs polling +import poll + +f = 'suggestions.json' +db = poll.load_db(f) +votes = [] +try: + votes = sys.argv[1:] +except: + print("""usage: undo number (...) + undos vote of one or more entries based on .list""") + sys.exit(1) +voter = environ['_prefix'] +for vote in votes: + try: + vote = int(vote) + del(db[vote]['votes'][voter] ) + print("undid vote by %s for #%d" %(environ['_from'],vote)) + except: + print("undo voting for #%s failed" %vote) + +poll.save_db(f,db) diff --git a/Reaktor/titlebot/commands/up b/Reaktor/titlebot/commands/up new file mode 100755 index 00000000..c9de1223 --- /dev/null +++ b/Reaktor/titlebot/commands/up @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +import json +from os import environ +import sys +import os +# krebs polling +import poll + +f = 'suggestions.json' +db = poll.load_db(f) +votes = [] +try: + votes = sys.argv[1:] +except: + print("""usage: up number (...) + upvotes one or more entries based on .list""") + sys.exit(1) + +voter = environ['_prefix'] +for vote in votes: + try: + vote = int(vote) + db[vote]['votes'][voter] = 1 + except: + print("voting for #%s failed" %vote) +print("Thanks for your votes %s"%environ['_from']) +poll.save_db(f,db) |