-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathadd_sub.cgi
More file actions
executable file
·59 lines (48 loc) · 1.08 KB
/
Copy pathadd_sub.cgi
File metadata and controls
executable file
·59 lines (48 loc) · 1.08 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
#!/usr/bin/python
import cgitb; cgitb.enable() # for debugging
import cgi # for getting query keys/values
from JSON import json
def get_subs():
f = open('subs.txt', 'r')
subs = f.read().lower().split('\n')
f.close()
while subs.count("") > 0:
subs.remove("")
return subs
def save_subs(subs):
f = open('subs.txt', 'w')
for sub in subs:
f.write(sub + '\n')
f.close()
def get_keys():
""" Retrieves key/value pairs from query, puts in dict """
form = cgi.FieldStorage()
keys = {}
for key in form.keys():
keys[key] = form[key].value
return keys
def main():
keys = get_keys()
if not 'subreddit' in keys:
err = {}
err['result'] = "no subreddit given"
print json.dumps(err)
return
sub = keys['subreddit'].lower()
while ' ' in sub: sub.replace(' ', '')
subs = get_subs()
if sub in subs:
err = {}
err['result'] = "subreddit already exists"
print json.dumps(err)
return
subs.append(sub)
save_subs(subs)
j = {}
j['result'] = "subreddit added"
print json.dumps(j)
if __name__ == '__main__':
print "Content-Type: application/json"
print ""
main()
print '\n\n'