Fix KeyError bug of parsing steps data

pull/112/head
Meng Li 2021-01-20 11:26:28 -05:00
parent 3a62f09101
commit 5f60aac5c8
1 changed files with 23 additions and 22 deletions

View File

@ -17,33 +17,34 @@ def parseStepsData(steps_data, fitbit_data_type):
# Parse JSON into individual records
for record in steps_data.fitbit_data:
record = json.loads(record) # Parse text into JSON
curr_date = datetime.strptime(record["activities-steps"][0]["dateTime"], "%Y-%m-%d")
# Parse summary data
if fitbit_data_type == "summary":
if "activities-steps" in record.keys():
curr_date = datetime.strptime(record["activities-steps"][0]["dateTime"], "%Y-%m-%d")
row_summary = (device_id,
record["activities-steps"][0]["value"],
curr_date,
0)
records.append(row_summary)
# Parse summary data
if fitbit_data_type == "summary":
# Parse intraday data
if fitbit_data_type == "intraday":
dataset = record["activities-steps-intraday"]["dataset"]
for data in dataset:
d_time = datetime.strptime(data["time"], '%H:%M:%S').time()
d_datetime = datetime.combine(curr_date, d_time)
row_intraday = (device_id,
data["value"],
d_datetime,
row_summary = (device_id,
record["activities-steps"][0]["value"],
curr_date,
0)
records.append(row_intraday)
records.append(row_summary)
# Parse intraday data
if (fitbit_data_type == "intraday") and ("activities-steps-intraday" in record.keys()):
dataset = record["activities-steps-intraday"]["dataset"]
for data in dataset:
d_time = datetime.strptime(data["time"], '%H:%M:%S').time()
d_datetime = datetime.combine(curr_date, d_time)
row_intraday = (device_id,
data["value"],
d_datetime,
0)
records.append(row_intraday)
parsed_data = pd.DataFrame(data=records, columns=STEPS_COLUMNS)
parsed_data = pd.DataFrame(data=records, columns=STEPS_COLUMNS)
return parsed_data