-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
157 lines (140 loc) · 5.29 KB
/
main.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import urllib
import config
import quapi
import parsers
import helpers
import filemanager
import os
from datetime import datetime, timedelta
#load config.
config.init()
curdir = os.path.dirname(__file__)
#read old validation file, exit if it's bad
valpath = os.path.join(curdir, 'validation.txt')
validation = filemanager.readcsv(valpath)
if validation[0][0] != 'GOOD':
print(validation[0][0])
print('Something went wrong at' + str(datetime.now()) + ', Stopping')
exit()
validation[0][0] = 'BAD'
filemanager.writecsv(validation, valpath)
#read old csv to get data.
csvpath = os.path.join(curdir, config.filename)
arr = filemanager.readcsv(csvpath)
users = filemanager.arrayToUsers(arr)
precount = 0
for i in range(0,len(users)):
if users[i].state == 'c':
precount = precount +1
#query qualtrics for responses
xmlresp1 = quapi.makeRequest(quapi.getLegacyResponseData(config.survey1id, config.survey1questions))
xmlresp2 = quapi.makeRequest(quapi.getLegacyResponseData(config.survey2id,config.survey2questions))
arr1 = parsers.parseForEmails(xmlresp1,config.survey1refs)
arr2 = parsers.parseForEmails(xmlresp2,config.survey2refs)
#integratge new responses with old data, set repeats and invalids to D, set completed surveys to C, send them thank yous
for i in range(0,len(arr1)):
for j in range(0,len(users)):
if arr1[i][0] == users[j].email:
users[j].state = 'c'
for k in range(0,len(users[j].childrenID)):
users[j].childrenID[k] = arr1[i][k+1]
users[j].childrenID[k] = users[j].childrenID[k].replace(' ', '')
#I know this is dumb, but it's late at night, and I'm confusing passing users by value or reference in my head, so this is what I'm doing.
for l in range(0,len(arr2)):
for m in range(0,len(users)):
if arr2[l][0] == users[m].email:
users[m].state = 'c'
for n in range(0,len(users[m].childrenID)):
users[m].childrenID[n] = arr2[l][n+1]
users[m].childrenID[n] = users[m].childrenID[n].replace(' ', '')
postcount = 0
for i in range(len(users)):
if users[i].state == 'c':
postcount = postcount + 1
credits = (postcount - precount) * 3
#kill all nonvalid surveys
if config.allowOnlySuffixes == 1:
for i in range(0,len(users)):
if config.suffix not in users[i].email:
users[i].state == 'd'
for i in range(0,len(users)):
invalid = '~`!#$%^&*()_-+={}[]:>;\',</?*-+'
for j in users[i].email:
for k in invalid:
if j ==k:
users[i].state = 'd'
#kill expired surveys
for i in range(len(users)):
if (users[i].sendTime + config.expiry) < datetime.now() and users[i].state == 's':
print(users[i].sendTime + config.expiry)
users[i].state = 'd'
credits = credits + 1
for i in range(0, len(users)):
if users[i].state == 'c':
for j in range(0, len(users[i].childrenID)):
found = 0
for k in range(0, len(users)):
if users[i].childrenID[j] == users[k].email:
found = 1
if found == 0:
if not not users[i].childrenID[j]:
new = filemanager.User()
new.email = users[i].childrenID[j]
new.email = new.email.replace(' ', '')
new.parentID = users[i].email
new.state = 'n'
users.append(new)
#check for Qs more than 1 day old, send surveys, set state to S
for i in range(0,len(users)):
if users[i].state == 'q' and datetime.now() > (users[i].selectTime + config.delay):
surv = helpers.chooseSurvey()
subj = ''
#change this depending on what zoe wants to do
defsubject = 1
if users[i].parentID == '' or defsubject == 1:
subj = config.subject
else:
subj = config.subject2 + users[i].parentID
quapi.sendSurveySubjectExpiry(users[i].email, surv,subj)
users[i].state = 's'
users[i].sendTime = datetime.now()
users[i].survey = surv
#calculate ave distance of each N to Qs and Ss and Cs
listofListOfParents = [None] *len(users)
dists = [0] * len(users)
for i in range(0,len(users)):
listofListOfParents[i] = helpers.getParentList(users,i)
for i in range(0,len(users)):
if users[i].state == 'n':
if not users[i].parentID:
dists[i] = 10000
else:
for j in range(0,len(users)):
if users[j].state == 's'or users[j].state == 'c' or users[j].state == 'q':
dists[i] = dists[i] + helpers.calcDist(listofListOfParents[i],listofListOfParents[j])
running = 0
for i in range(0,len(users)):
if users[i].state == 's' or users[i].state == 'c' or users[i].state == 'q':
running = running +1
#set Ns to Qs until either credits, total coupons are exceeded.
while credits > 0 and running < config.total:
index = dists.index(max(dists))
if users[index].state == 'n':
users[index].state = 'q'
users[index].selectTime = datetime.now()
credits = credits-1
running = running + 1
if running == config.total:
print('ran out of coupons at ' + str(datetime.now()))
exit()
dists[index] = 0
found = 0
for i in range(0,len(users)):
if users[i].state == 'n':
found = 1
if not found:
credits = 0
#write to csv
filemanager.writecsv(filemanager.usersToArray(users), csvpath)
validation[0][0] = 'GOOD'
filemanager.writecsv(validation,valpath)