Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLite does not have case sensitive columns #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion healthkit_to_sqlite/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def find_all_tags(fp, tags, progress_callback=None):
def convert_xml_to_sqlite(fp, db, progress_callback=None, zipfile=None):
activity_summaries = []
records = []
metadata_keys_map = {}
for tag, el in find_all_tags(
fp, {"Record", "Workout", "ActivitySummary"}, progress_callback
):
Expand All @@ -35,7 +36,20 @@ def convert_xml_to_sqlite(fp, db, progress_callback=None, zipfile=None):
elif tag == "Record":
record = dict(el.attrib)
for child in el.findall("MetadataEntry"):
record["metadata_" + child.attrib["key"]] = child.attrib["value"]
# sqlite does not have case sensitive columns, have to
# distinguish between meta_start and meta_Start
# we will make meta_Start into meta_start_1
# that mapping is saved for other records to
# metadata_keys_map
meta_key = "metadata_" + child.attrib["key"]
candidates = list(filter(
lambda k: k[0].lower() == (meta_key).lower(),
metadata_keys_map.items()))
if not candidates:
metadata_keys_map[meta_key] = meta_key
elif meta_key not in metadata_keys_map.keys():
metadata_keys_map[meta_key] = meta_key + "_" + str(len(candidates))
record[metadata_keys_map[meta_key]] = child.attrib["value"]
records.append(record)
if len(records) >= 200:
write_records(records, db)
Expand Down
4 changes: 4 additions & 0 deletions tests/export.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<Record type="HKQuantityTypeIdentifierHeartRate" sourceName="Apple Watch" sourceVersion="4.3.1" device="&lt;&lt;HKDevice: 0x282a45810&gt;, name:Apple Watch, manufacturer:Apple, model:Watch, hardware:Watch2,4, software:4.3.1&gt;" unit="count/min" creationDate="2018-09-10 02:47:35 -0700" startDate="2018-09-10 02:28:55 -0700" endDate="2018-09-10 02:28:55 -0700" value="72">
<MetadataEntry key="HKMetadataKeyHeartRateMotionContext" value="0"/>
</Record>
<Record type="HKQuantityTypeIdentifierDietaryFatTotal" sourceName="MyFitnessPal" sourceVersion="20962" unit="g" creationDate="2017-09-30 13:57:16 +0100" startDate="2017-09-30 13:56:00 +0100" endDate="2017-09-30 13:56:00 +0100" value="21.37">
<MetadataEntry key="meal" value="Breakfast"/>
<MetadataEntry key="Meal" value="Breakfast"/>
</Record>
<Workout workoutActivityType="HKWorkoutActivityTypeRunning" duration="5.19412346680959" durationUnit="min" totalDistance="0.4971749504535062" totalDistanceUnit="mi" totalEnergyBurned="48.74499999999999" totalEnergyBurnedUnit="kcal" sourceName="Apple Watch" sourceVersion="3.1" creationDate="2016-11-14 07:33:49 -0700" startDate="2016-11-14 07:25:41 -0700" endDate="2016-11-14 07:30:52 -0700">
<MetadataEntry key="HKTimeZone" value="America/Los_Angeles"/>
<MetadataEntry key="HKWeatherTemperature" value="56 degF"/>
Expand Down