aboutsummaryrefslogtreecommitdiffstats
path: root/titlebot/commands
diff options
context:
space:
mode:
Diffstat (limited to 'titlebot/commands')
-rwxr-xr-xtitlebot/commands/clear12
-rwxr-xr-xtitlebot/commands/down2
-rwxr-xr-xtitlebot/commands/help10
-rwxr-xr-xtitlebot/commands/list27
-rwxr-xr-xtitlebot/commands/new19
-rw-r--r--titlebot/commands/poll.py23
-rwxr-xr-xtitlebot/commands/undo27
-rwxr-xr-xtitlebot/commands/up27
8 files changed, 147 insertions, 0 deletions
diff --git a/titlebot/commands/clear b/titlebot/commands/clear
new file mode 100755
index 0000000..e355819
--- /dev/null
+++ b/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/titlebot/commands/down b/titlebot/commands/down
new file mode 100755
index 0000000..8964382
--- /dev/null
+++ b/titlebot/commands/down
@@ -0,0 +1,2 @@
+#!/bin/sh
+echo "not implemented"
diff --git a/titlebot/commands/help b/titlebot/commands/help
new file mode 100755
index 0000000..475ea98
--- /dev/null
+++ b/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/titlebot/commands/list b/titlebot/commands/list
new file mode 100755
index 0000000..3a29919
--- /dev/null
+++ b/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/titlebot/commands/new b/titlebot/commands/new
new file mode 100755
index 0000000..7246a2b
--- /dev/null
+++ b/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/titlebot/commands/poll.py b/titlebot/commands/poll.py
new file mode 100644
index 0000000..595ab26
--- /dev/null
+++ b/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/titlebot/commands/undo b/titlebot/commands/undo
new file mode 100755
index 0000000..bebd57a
--- /dev/null
+++ b/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/titlebot/commands/up b/titlebot/commands/up
new file mode 100755
index 0000000..c9de122
--- /dev/null
+++ b/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)