-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFitBit.py
More file actions
189 lines (177 loc) · 5.54 KB
/
FitBit.py
File metadata and controls
189 lines (177 loc) · 5.54 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
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
#!/usr/bin/env python
import mysql.connector as mysql
import sys
import string
import re
class FitBit(object):
"""
Creates an object that can parse the CSV file output from fitbit.com
and insert the data into a MySQL/MariaDB database, per the provided
schema
"""
def __init__(self):
self._sections = {
"Activities":self._add_activities,
"Body":self._add_body,
"Sleep":self._add_sleep,
"Food Log":self._add_foodlog,
"Daily Totals":self._add_nutrition,
}
self._connection = None
self._cursor = None
self._log = {}
def load_file(self, fname):
"""
Loads the file and populates a data structure with entries
"""
meals = {'"Breakfast"':1, '"Lunch"':2, '"Dinner"':3, '"Morning Snack"':4, '"Afternoon Snack"':5, '"After Dinner"':7, '"Anytime"':8}
logdate = ""
mealid = 0
raw = open(fname,'r').read()
raw = raw.split('\n\n')
for section in raw:
# parse the Body section of the CSV and store
if re.search('Body',section) is not None:
self._log["Body"] = []
for line in section.split("\n")[2:]:
self._log["Body"].append(self._parse_line(line))
# parse the activities section of the CSV and store
elif re.search('Activities',section) is not None:
self._log["Activities"] = []
for line in section.split("\n")[2:]:
self._log["Activities"].append(self._parse_line(line))
# parse the food log entry
elif re.search('Food Log', section) is not None:
nut = [] # store the nutrition info temporarily
if not "Food Log" in self._log:
self._log["Food Log"] = {}
if not "Daily Totals" in self._log:
self._log["Daily Totals"] = {}
food, nutrition = section.split("Daily Totals")
# main food log
for line in food.split("\n"):
if "Log" in line:
date = line[-8:]
logdate = date[0:4] + "-" + date[4:6] + "-" + date[6:]
self._log["Food Log"][logdate] = []
self._log["Daily Totals"][logdate] = []
elif line in meals:
mealid = meals[line]
else:
self._parse_foodlog(logdate,mealid,line)
# daily nutrition stats
for line in nutrition.split("\n"):
val = line.split('","')[-1:][0][:-1]
val = re.sub('\,','',val)
if 'cup' in val:
val = val.split(' ')[0]
if len(val) > 0:
nut.append(val)
self._log["Daily Totals"][logdate] = nut
# sleep stats
elif re.search("Sleep", section) is not None:
self._log["Sleep"] = []
for line in section.split("\n")[2:]:
self._log["Sleep"].append(self._parse_line(line))
# we don't really need this. info is present in nutrition stats
elif re.search("Foods",section) is not None:
pass
def connectdb(self,u,p,h,d):
"""
Takes the necessary arguments to mysql.connect and creates a connection
context and cursor internally to the FitBit object
"""
self._connection = mysql.connect(user=u,password=p,host=h,database=d)
self._cursor = self._connection.cursor()
def insert_data(self):
"""
Inserts all the data from the datastructure into the database
"""
assert self._connection is not None, "No connection exists"
assert self._cursor is not None, "No cursor exists"
for key in self._log.keys():
self._sections[key](self._log[key])
def dbcommit(self):
"""
Commit the database transaction
"""
self._connection.commit()
def dbclose(self):
"""
Close the database connection and cursor
"""
self._cursor.close()
self._connection.close()
def _add_activities(self, entries):
"""
Build the query for activities and actually insert into fbact
"""
for ent in entries:
query = """INSERT INTO fbact
(days, calsburned, steps, distance, floors, ms, mla, mfa, mva, actcals)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""
self._cursor.execute(query,ent)
def _add_body(self, entries):
"""
Build the query for body and actually insert into body
"""
for ent in entries:
query = """INSERT INTO body (days, weight, bmi, fat) VALUES (%s, %s, %s, %s)"""
self._cursor.execute(query,ent)
def _add_foodlog(self, data):
"""
build the query for the food log and insert
"""
for date in data:
for ent in data[date]:
ent.insert(0,date)
query = """INSERT INTO foodlog (days, meal, food, calories) VALUES (%s, %s, %s, %s)"""
self._cursor.execute(query,ent)
def _add_nutrition(self, data):
"""
build the query for the nutrition stats and insert
"""
for date in data:
data[date].insert(0,date)
query = """INSERT INTO nutrition (days, calories, fat, fiber, carbs, sodium, protein, water) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"""
self._cursor.execute(query,data[date])
def _add_sleep(self, entries):
"""
build the query for the sleep stats and insert
"""
for ent in entries:
query = """INSERT INTO sleep
(starttime, endtime, minsleep, minwake, numwakes, minbed, minrem, minlight, mindeep)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"""
self._cursor.execute(query,ent)
def _parse_line(self, entry):
"""
Cleans up a line and returns a list of values
"""
fields = entry.split('","')
fields[0] = fields[0][1:]
fields[-1] = fields[-1][:-1]
clean = []
for field in fields:
if ',' in field:
field = field.translate(None,string.punctuation)
elif field == "N/A":
field = None
if field == "":
continue
clean.append(field)
return clean
def _parse_foodlog(self,date,mealid,entry):
"""
parse food log entries and store in the data structure
"""
if "Meal" in entry:
pass
else:
data = self._parse_line(entry)
if len(data) == 0:
return
else:
data.insert(0,mealid)
self._log["Food Log"][date].append(data)
# end FitBit