diff --git a/.gitignore b/.gitignore index 8dff601..dcd91ea 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ htmlcov # Editors .idea + +config.py +*.txt diff --git a/gather_keys_oauth2.py b/gather_keys_oauth2.py index 39a19f8..bf54c45 100755 --- a/gather_keys_oauth2.py +++ b/gather_keys_oauth2.py @@ -78,6 +78,7 @@ def _shutdown_cherrypy(self): """ Shutdown cherrypy in one second, if it's running """ if cherrypy.engine.state == cherrypy.engine.states.STARTED: threading.Timer(1, cherrypy.engine.exit).start() + cherrypy.engine.exit() if __name__ == '__main__': diff --git a/heartrate.py b/heartrate.py new file mode 100755 index 0000000..b4bc136 --- /dev/null +++ b/heartrate.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python +"""Script for fetching my min and max heart rate over the past 15 minutes.""" +import time + +from config import * +from gather_keys_oauth2 import OAuth2Server + +if __name__ == '__main__': + server = OAuth2Server(client_id, client_secret) + server.browser_authorize() + + fb = server.fitbit + detail_level = '1min' + intraday = f'https://api.fitbit.com/1/user/-/activities/heart/date/today/1d/{detail_level}.json' + + while True: + resp = fb.make_request(intraday) + dataset = resp['activities-heart-intraday']['dataset'] + # Get only data from the last 15 minutes. + latest = [d['value'] for d in dataset[-16:]] + + with open('maxs-hr.txt', 'w') as f: + output = '{}–{}'.format(min(latest), max(latest)) + print(output) + f.write(output) + + time.sleep(60)