forked from zachwill/flask_heroku
/
Run.py
141 lines (119 loc) · 4.24 KB
/
Run.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
from config import *
from Goal import Goal
class Run :
def __init__(self, sessionToken, runId) :
distanceList = []
params = urllib.parse.urlencode({"where":json.dumps({
"objectId" : runId
})})
try :
connection.request('GET', '/1/classes/Run?%s' % params, '', {
"X-Parse-Application-Id": os.environ['RUNMATE_CONST_APPID'],
"X-Parse-REST-API-Key": os.environ['RUNMATE_CONST_APIKEY'],
"X-Parse-Session-Token" : str(sessionToken)
})
result = json.loads(connection.getresponse().read().decode('utf-8'))['results']
except http.client.RemoteDisconnected :
connection.request('GET', '/1/classes/Run?%s' % params, '', {
"X-Parse-Application-Id": os.environ['RUNMATE_CONST_APPID'],
"X-Parse-REST-API-Key": os.environ['RUNMATE_CONST_APIKEY'],
"X-Parse-Session-Token" : str(sessionToken)
})
result = json.loads(connection.getresponse().read().decode('utf-8'))['results']
except :
restartConnection()
connection.request('GET', '/1/classes/Run?%s' % params, '', {
"X-Parse-Application-Id": os.environ['RUNMATE_CONST_APPID'],
"X-Parse-REST-API-Key": os.environ['RUNMATE_CONST_APIKEY'],
"X-Parse-Session-Token" : str(sessionToken)
})
result = json.loads(connection.getresponse().read().decode('utf-8'))['results']
if result :
run = result[0]
else :
self.error = True
return jsonify(error="Run not found")
self.error = False
self.distance = run['distance']
self.userId = run['user']
self.runId = runId
self.runLocations = run['runlocations']
self.runData = self.pullRunLocations()
if self.runData :
for l in self.runData :
distanceList.append(l[0])
self.runDistances = distanceList
def pullRunLocations(self) :
locList = []
try :
params = urllib.parse.urlencode({"where":json.dumps({
"objectId": {
"$in" : self.runLocations
}
})})
connection.request('GET', '/1/classes/RunLocation?%s' % params, '', {
"X-Parse-Application-Id": os.environ['RUNMATE_CONST_APPID'],
"X-Parse-REST-API-Key": os.environ['RUNMATE_CONST_APIKEY']
})
results = json.loads(connection.getresponse().read().decode('utf-8'))['results']
except ValueError:
return jsonify(error="RunLocs missing for run")
for runLoc in results :
locList.append([runLoc['distance']*0.00062137, datetime.datetime.fromtimestamp(runLoc['timestamp'])])
locList = sorted(locList, key=itemgetter(0))
return locList
def getTrophies(self) :
goalsCompleted = []
if self.error == True :
return jsonify(error="Run not found")
# Retrieve all uncompleted user goals
params = urllib.parse.urlencode({"where":json.dumps({
"completed" : False,
"userObjectID" : self.userId
})})
try :
connection.request('GET', '/1/classes/TrophyInformation?%s' % params, '', {
"X-Parse-Application-Id": os.environ['RUNMATE_CONST_APPID'],
"X-Parse-REST-API-Key": os.environ['RUNMATE_CONST_APIKEY']
})
trophyList = json.loads(connection.getresponse().read().decode('utf-8'))['results']
except http.client.RemoteDisconnected :
return self.getTrophies()
# Filter goals that shouldn't be considered
for t in trophyList :
goal = Goal(str(t['objectId']))
if self.checkTrophy(goal) :
goalsCompleted.append(goal.setCompleted())
if goalsCompleted == [] :
return jsonify(error="No goals completed")
else :
return jsonify(goals=goalsCompleted)
def findNearest(self, array, value):
idx = (np.abs(array-value)).argmin()
return array[idx]
def checkTrophy(self, goal) :
locList = []
if goal.distance > self.distance :
return False
elif ((goal.distance <= self.distance) & (goal.time == 0)) :
return True
else :
if self.runData == None :
return False
else :
loops = goal.distance / self.distance
l = np.arange(0.0, loops, .1)
for i in l :
startMile = i
endMile = i + goal.distance
startInfo = self.findNearest(self.runDistances, startMile)
endInfo = self.findNearest(self.runDistances, endMile)
for d in self.runData :
if (d[0] == startInfo) :
startInfo = d
if (d[0] == endInfo) :
endInfo = d
diffTime = endInfo[1] - startInfo[1]
if diffTime.seconds <= goal.time :
return True
return False