forked from balloon-studios/quotebot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
127 lines (105 loc) · 3.2 KB
/
__init__.py
File metadata and controls
127 lines (105 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from flask import Flask
from flask import jsonify
from flask import abort
from flask import make_response
from flask import request
import csv
import random
import os
import re
import urllib
app = Flask(__name__)
app.debug = True
class WebFactionMiddleware(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
environ['SCRIPT_NAME'] = '/webhooks'
return self.app(environ, start_response)
app.wsgi_app = WebFactionMiddleware(app.wsgi_app)
quotes = []
quoters = {
'fluffyemily' : 'ET',
'geek_manager': 'MW',
'pixeldiva': 'AC',
'jcm': 'JM',
'cackhanded': 'MNF',
'rnalexander': 'RA',
'nick': 'NS',
'jabley': 'JA',
'fatbusinessman': 'DT',
'elly': 'EW',
'pkqk': 'AJ'
}
#@app.errorhandler(404)
#def not_found(error):
# return make_response(jsonify({'error': 'Not found'}), 404)
@app.route('/quote', methods = ["POST"])
def get_quote():
quoter = request.form['text'].replace("quote", "")
print "|"+quoter+"|"
if len(quoter) > 0:
quoter = str(quoter).strip()
if quoter == "me":
quoter = quoters[request.form['user_name']]
elif quoters.get(quoter) is not None:
quoter = quoters[quoter]
print quoter
if len(quoter) == 0:
filtered_quotes = quotes
else:
filtered_quotes = filter(lambda t: t['by'] == quoter, quotes)
print filtered_quotes
quote = random.choice(filtered_quotes)
quote_string = "*"+ quote['by'] + "*: "+ quote['quote']
print "returning quote: "+ quote_string
return jsonify({'text': quote_string})
@app.route('/quotes', methods = ["POST"])
def create_quote():
if len(request.form) > 0:
quote_text = request.form['text'] #.encode('utf-8', 'replace')
elif request.json is not None:
quote_text = request.json['text']
else:
abort(400)
# print quote_text
matches=re.findall(r'\"(.+?)\"',quote_text)
quote = ""
for match in matches:
quote_text = quote_text.replace(match, "")
if len(quote) > 0:
quote += "\n"
quote += match
quote_text.replace("'", "")
parts = quote_text.split(" ")
#print parts
quote_by = parts[-1]
print quote
print quote_by
quote = {
'quote': quote,
'by': quote_by
}
#add quote to list
quotes.append(quote)
#write quote to csv file for future usage
with open('webapps/slack_webhooks/slack_webhooks/quoteboard.csv', 'ab') as csvfile:
quote_writer = csv.writer(csvfile, delimiter=',',
quotechar='"', quoting=csv.QUOTE_NONNUMERIC)
quote_writer.writerow([quote['by'], quote['quote']])
return jsonify({"status": "OK"})
def init_db():
#initialise the db by reading in all of the existing quotes
print "initing db"
print os.getcwd()
with open('webapps/slack_webhooks/slack_webhooks/quoteboard.csv') as csvfile:
quote_reader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in quote_reader:
quote = {
'quote': row[1],
'by': row[0]
}
quotes.append(quote)
init_db()
if __name__ == "__main__":
app.run(debug = True)