knio / myfitbit Goto Github PK
View Code? Open in Web Editor NEWFitbit data export - Because your data belongs to you!
License: GNU General Public License v3.0
Fitbit data export - Because your data belongs to you!
License: GNU General Public License v3.0
Since I stripped parts of the authorization process and made it use my personal constants, I decided not to make a PR, but for others' reference, here's how I added other parameters like steps, distance, and elevation (crappily; for best practices, just abstractize the heartrate intraday). You can pretty much follow the pattern to add others like calories, floors etc. (https://dev.fitbit.com/build/reference/web-api/activity/#get-activity-time-series)
Add to __init__.py
in class Fitbit(object):
def get_steps_intraday(self, date):
r = self.session.get('https://api.fitbit.com/1/user/-/activities/steps/date/{}/{}/1min.json'
.format(
str(date),
str(date)
)
)
r.raise_for_status()
return json.loads(r.text)['activities-steps-intraday']['dataset']
def get_distance_intraday(self, date):
r = self.session.get('https://api.fitbit.com/1/user/-/activities/distance/date/{}/{}/1min.json'
.format(
str(date),
str(date)
)
)
r.raise_for_status()
return json.loads(r.text)['activities-distance-intraday']['dataset']
def get_elevation_intraday(self, date):
r = self.session.get('https://api.fitbit.com/1/user/-/activities/elevation/date/{}/{}/1min.json'
.format(
str(date),
str(date)
)
)
r.raise_for_status()
return json.loads(r.text)['activities-elevation-intraday']['dataset']
Add to export.py
in class FitbitExport(object):
def steps_intraday_filenames(self):
start = date(2017, 1, 1)
days = 0
while 1:
d = start + timedelta(days=days)
days += 1
if d == date.today():
return
filename = self.filename(
'steps_intraday',
'{:04d}'.format(d.year),
'steps_intraday.{:04d}.{:02d}.{:02d}.json'.format(
d.year,
d.month,
d.day
))
yield d, filename
def get_steps_intraday(self):
def compress(data):
minutes = [None] * 24 * 60
for o in data:
h, m, s = map(int, o['time'].split(':'))
i = h * 60 + m
minutes[i] = o['value']
return minutes
steps = []
for d, filename in self.steps_intraday_filenames():
if not os.path.isfile(filename):
continue
data = json.load(open(filename))
if not data:
continue
steps.append({
'date': d.isoformat(),
'minutes': compress(data),
})
return steps
def sync_steps_intraday(self):
for d, filename in self.steps_intraday_filenames():
if os.path.isfile(filename):
log.info('Cached: %s', filename)
continue
log.info('Downloading: %s', filename)
hr = self.client.get_steps_intraday(d)
self.write(filename, hr)
def distance_intraday_filenames(self):
start = date(2017, 1, 1)
days = 0
while 1:
d = start + timedelta(days=days)
days += 1
if d == date.today():
return
filename = self.filename(
'distance_intraday',
'{:04d}'.format(d.year),
'distance_intraday.{:04d}.{:02d}.{:02d}.json'.format(
d.year,
d.month,
d.day
))
yield d, filename
def get_distance_intraday(self):
def compress(data):
minutes = [None] * 24 * 60
for o in data:
h, m, s = map(int, o['time'].split(':'))
i = h * 60 + m
minutes[i] = o['value']
return minutes
distance = []
for d, filename in self.distance_intraday_filenames():
if not os.path.isfile(filename):
continue
data = json.load(open(filename))
if not data:
continue
distance.append({
'date': d.isoformat(),
'minutes': compress(data),
})
return distance
def sync_distance_intraday(self):
for d, filename in self.distance_intraday_filenames():
if os.path.isfile(filename):
log.info('Cached: %s', filename)
continue
log.info('Downloading: %s', filename)
hr = self.client.get_distance_intraday(d)
self.write(filename, hr)
def elevation_intraday_filenames(self):
start = date(2017, 1, 1)
days = 0
while 1:
d = start + timedelta(days=days)
days += 1
if d == date.today():
return
filename = self.filename(
'elevation_intraday',
'{:04d}'.format(d.year),
'elevation_intraday.{:04d}.{:02d}.{:02d}.json'.format(
d.year,
d.month,
d.day
))
yield d, filename
def get_elevation_intraday(self):
def compress(data):
minutes = [None] * 24 * 60
for o in data:
h, m, s = map(int, o['time'].split(':'))
i = h * 60 + m
minutes[i] = o['value']
return minutes
elevation = []
for d, filename in self.elevation_intraday_filenames():
if not os.path.isfile(filename):
continue
data = json.load(open(filename))
if not data:
continue
elevation.append({
'date': d.isoformat(),
'minutes': compress(data),
})
return elevation
def sync_elevation_intraday(self):
for d, filename in self.elevation_intraday_filenames():
if os.path.isfile(filename):
log.info('Cached: %s', filename)
continue
log.info('Downloading: %s', filename)
hr = self.client.get_elevation_intraday(d)
self.write(filename, hr)
Add to __main__.py
in def main():
export.sync_steps_intraday()
export.sync_distance_intraday()
export.sync_elevation_intraday()
Thanks Knio for this wonderful package!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.