-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreports.py
325 lines (260 loc) · 8.22 KB
/
reports.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import re
import time
import os.path
import json
import glob
import dateutil.parser
import dropbox
from dropbox.exceptions import HttpError
from tqdm import tqdm
from reporter_api.constants import ACCESS_TOKEN, APP_PATH
DEBUG = True
class Mode:
DROPBOX = 0
LOCAL = 1
def log(msg):
if DEBUG:
print("reporter_api :: {0}".format(msg))
class Series(object):
def __init__(self, mode=None):
if mode == None:
self.__mode = Mode.DROPBOX
elif mode == Mode.LOCAL:
self.__mode = Mode.LOCAL
elif mode == Mode.DROPBOX:
self.__mode = Mode.DROPBOX
self.__setup()
def __setup(self):
if self.__mode == 0:
self.__db = dropbox.Dropbox(ACCESS_TOKEN)
self.__listFolderObj = self.__db.files_list_folder("/{0}".format(APP_PATH))
def __fetchLocal(self):
reportObjs = []
home = os.path.expanduser("~")
dropboxPath = os.path.join(str(home), "Dropbox", str(APP_PATH))
files = glob.glob("{path}/*.json".format(path=dropboxPath))
files.sort()
if(DEBUG):
start = time.time()
for file in tqdm(files):
fh = open(file, 'r')
data = json.loads(fh.read())
reportObjs.append(Report(data, file))
end = time.time()
log("Query time :: " + str(end - start))
return reportObjs
def __fetchDropbox(self):
reporterObjs = []
if(DEBUG):
start = time.time()
for i in tqdm(self.__listFolderObj.entries):
if os.path.splitext(i.path_display)[1] == ".json":
try:
md, res = self.__db.files_download(i.path_display)
except HttpError as err:
print('*** HTTP error', err)
return None
data = res.content
reporterObj = Report(json.loads(data))
reporterObjs.append(reporterObj)
end = time.time()
log("Query time :: " + str(end - start))
return reporterObjs
@property
def reportObjs(self):
if self.__mode == 0:
reports = self.__fetchDropbox()
else:
reports = self.__fetchLocal()
pass
return reports
@property
def latestReport(self):
""" Returns latest Report object """
if self.__mode == 0:
md, res = self.__db.files_download(self.__listFolderObj.entries[-1].path_display)
data = res.content
return Report(json.loads(data))
class Report(object):
def __init__(self, data, filePath=None):
self.__data = data
self.__filePath = filePath
self.__setup()
def __setup(self):
self.__snapshots = []
for snapshot in self.__data["snapshots"]:
snapshotObj = Snapshot(snapshot, parent=self)
self.__snapshots.append(snapshotObj)
@property
def snapshots(self):
return self.__snapshots
@property
def questions(self):
return self.__data["questions"]
@property
def date(self):
if "snapshots" in self.__data:
if self.__data["snapshots"] != []:
matchObj = re.match("(.+)T", self.__data["snapshots"][0]['date'])
return matchObj.groups()[0]
else:
return None
def __str__(self):
if "snapshots" in self.__data:
if self.__data["snapshots"] != []:
return self.__data["snapshots"][0]["date"]
return str()
@property
def filePath(self):
return self.__filePath
class Snapshot(object):
def __init__(self, data, parent=None):
self.__data = data
self.__parent = parent
self.__setup()
def __setup(self):
self.__weight = None
@property
def battery(self):
"""
Gets recorded battery level
Returns: `int`
"""
return self.__data["battery"]
@property
def placemark(self):
if "location" in self.__data:
return self.__data["location"]["placemark"]
@property
def longitude(self):
""" Gets longitude
Returns: float
"""
if "location" in self.__data:
if "longitude" in self.__data['location']:
return self.__data['location']['longitude']
else:
return None
@property
def latitude(self):
""" Gets latitude
Returns: float
"""
if "location" in self.__data:
if "latitude" in self.__data['location']:
return self.__data['location']['latitude']
else:
return None
@property
def timestamp(self):
if "location" in self.__data:
return self.__data['location']['timestamp']
else:
return None
@property
def date(self):
if "date" in self.__data:
return dateutil.parser.parse(self.__data['date']).strftime("%Y-%m-%d %H:%M")
else:
return None
@property
def audio(self):
if "audio" in self.__data:
return self.__data["audio"]["avg"]
@property
def connection(self):
"""
Phone connection status
Returns: `int`
"""
if "connection" in self.__data:
return self.__data["connection"]
@property
def data(self):
return self.__data
@property
def cleanData(self):
clean = {}
clean["location"] = self.location
clean["date"] = self.date
clean["responses"] = self.responses
return clean
@property
def longlat(self):
return [self.longitude, self.latitude]
@property
def country(self):
"""
Gets country
Returns: `string`
"""
if "location" in self.__data:
if "country" in self.__data["location"]:
return self.__data["location"]["country"]
elif "placemark" in self.__data["location"]:
if "country" in self.__data["location"]["placemark"]:
return self.__data["location"]["placemark"]["country"]
return None
@property
def altitude(self):
"""
Gets altitude
Returns: `float`
"""
if "location" in self.__data:
if "altitude" in self.__data["location"]:
return self.__data["location"]["altitude"]
else:
return None
@property
def steps(self):
"""
Gets step count
Returns: `int`
"""
if "steps" in self.__data:
return self.__data["steps"]
else:
return None
@property
def humidity(self):
if "weather" in self.__data:
if "relativeHumidity" in self.__data["weather"]:
return self.__data["weather"]["relativeHumidity"]
@property
def tempC(self):
if "weather" in self.__data:
if "tempC" in self.__data["weather"]:
return self.__data["weather"]["tempC"]
@property
def weather(self):
if "weather" in self.__data:
if "weather" in self.__data["weather"]:
return self.__data["weather"]["weather"]
@property
def weight(self):
return self.__weight
@weight.setter
def weight(self, weightIn):
self.__weight = weightIn
@property
def responses(self):
answers = {}
if "responses" in self.__data:
for response in self.data["responses"]:
answers[response["questionPrompt"]] = []
if "numericResponse" in response:
answers[response["questionPrompt"]] = response["numericResponse"]
if "answeredOptions" in response:
answers[response["questionPrompt"]] = response["answeredOptions"]
if "textResponses" in response:
answers[response["questionPrompt"]] = response["textResponses"]
if "tokens" in response:
answers[response["questionPrompt"]] = response["tokens"]
return answers
@property
def report(self):
return self.__parent
if __name__ == "__main__":
series = Series(Mode.DROPBOX)
series.latestReport