--The one that saves the data stored on the fitbit server locally --The language has been confirmed to work with Python, 2.7.10. ――Please refer to the following articles for preparing various environments.
--Note the "USER_ID", "CLIENT_SECRET", "ACCESS_TOKEN", and "REFRESH_TOKEN" by referring to the URL in the overview, and replace them with the characters "each one".
# -*- coding: utf-8 -*-
import sys
import os
import fitbit
import gather_keys_oauth2 as Oauth2
#Branch by the number of command line arguments (end if other than 2)
if not len(sys.argv) == 2: sys.exit(1)
#Variable declaration and date, data frequency, directory, file name generation
DATE = sys.argv[1]
FREQS = ['1sec','1min','15min']
DIRS={}
FILES={}
for FREQ in FREQS:
DIRS[FREQ] = './%s' % FREQ
FILES[FREQ] = 'HR_%s_%s.csv' % ( DATE, FREQ )
#If the directory does not exist, create the directory
for FREQ in FREQS:
if not os.path.exists(DIRS[FREQ]):os.mkdir(FREQ)
#Unique information, connection to authentication server and authentication processing
USER_ID = 'Each one'
CLIENT_SECRET = 'Each one'
ACCESS_TOKEN = 'Each one'
REFRESH_TOKEN = 'Each one'
auth2_client = fitbit.Fitbit(USER_ID, CLIENT_SECRET, oauth2=True, access_token=ACCESS_TOKEN, refresh_token=REFRESH_TOKEN)
#Data acquisition
fitbit_stats = {}
stats = {}
for FREQ in FREQS:
fitbit_stats[FREQ] = auth2_client.intraday_time_series('activities/heart', DATE, detail_level=FREQ)
stats[FREQ] = fitbit_stats[FREQ]['activities-heart-intraday']['dataset']
#CSV file generation and file saving
for FREQ in FREQS:
os.chdir(DIRS[FREQ]) #Move to each directory
csv_file = open(FILES[FREQ],'w')
for var in range(0, len(stats[FREQ])):
csv_file.write(stats[FREQ][var]['time'])
csv_file.write(",")
csv_file.write(str(stats[FREQ][var]['value']))
csv_file.write("\n")
csv_file.close()
os.chdir('..') #Move to the next higher directory
――It will be edited later
--Copy "fitbit", "gather_keys_oauth2.py", and "gather_keys_oauth2.pyc" in the Fitbit API to the same directory as the source code. --Execute with python file name.py 2017-02-01 --Directory named "1sec", "1min", and "15min" are created in the execution directory, and the data of the specified date is saved in each directory.
――For the code to get the heart rate, refer to the following page.
This is the first post. If you have any advice or supplements, please point them out.
Recommended Posts