Skip to content

Instantly share code, notes, and snippets.

@MoralCode
Last active May 28, 2022 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MoralCode/565987c78d3bfef20e453b2700a24795 to your computer and use it in GitHub Desktop.
Save MoralCode/565987c78d3bfef20e453b2700a24795 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Usage: first run your apple health export through the scripts as documented in https://github.com/markwk/qs_ledger/tree/master/apple_health
# then run this script using the BodyMass.csv from this process to get an OpenScale csv
import argparse
import csv
import datetime
from dateutil.parser import parse as parsedate
OPENSCALE_HEADER = '"biceps","bone","caliper1","caliper2","caliper3","calories","chest","comment","dateTime","fat","hip","lbm","muscle","neck","thigh","visceralFat","waist","water","weight"'
OPENSCALE_HEADER = OPENSCALE_HEADER.replace('"', '')
_APPLE_QUANTIFIEDSELF_BODYMASS_HEADER = 'sourceName,sourceVersion,device,type,unit,creationDate,startDate,endDate,value'
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('apple_bodymass_csv_file_path')
parser.add_argument('output_path')
args = parser.parse_args()
with open(args.apple_bodymass_csv_file_path, 'r') as inp:
reader = csv.DictReader(inp)
with open(args.output_path, 'w') as outp:
writer = csv.DictWriter(outp, OPENSCALE_HEADER.split(','))
# outp.write(f'{OPENSCALE_HEADER}\n')
writer.writeheader()
for line in reader:
creationDate = line['creationDate']
output_date = parsedate(creationDate).strftime('%Y-%m-%d %H:%M')
weight = float(line['value'])
if line['unit'] == 'lb':
# convert to KG
weight = weight / 2.2
comment = "Imported from Apple Health export. Data originally from "
appname = line["sourceName"]
# make the apple health app name a little more obvious. by default its just "health"
if appname == "Health":
appname = "Apple Health"
comment += appname + " app"
appversion = line["sourceVersion"]
if appversion != "":
comment += " version " + appversion
writer.writerow({
'dateTime': output_date,
'weight': "{:.2f}".format(weight),
'comment': comment
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment