Skip to content

Instantly share code, notes, and snippets.

@mourjo
Created January 1, 2022 05:59
Show Gist options
  • Save mourjo/9393be43fc23b77034541b5300188da0 to your computer and use it in GitHub Desktop.
Save mourjo/9393be43fc23b77034541b5300188da0 to your computer and use it in GitHub Desktop.
Poor person's Apple Health stats for distance run in a year
import fs, { readFileSync } from 'fs';
import readline from 'readline';
const YEAR = '2021';
function infer(row) {
const ts = row.match(/startDate=".*?"/g)[0].match(/.?.?.?.?-.?.?-.?.? .?.?:.?.?:.?.? .?.?.?.?.?/)[0]
const dt = ts.substring(0, 10)
if (dt.startsWith(YEAR)) {
const unit = row.match(/unit=".*?"/)[0].match(/".?.?"/)[0].substring(1, 3)
const v = row.match(/value="(.*?)"/)[0].substring(7);
const val = parseFloat(v.substring(0, v.length - 1))
return { dt, val }
}
}
async function processLineByLine(filename) {
const fileStream = fs.createReadStream(filename);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
const rows = [];
for await (const line of rl) {
const inf = infer(line);
if (inf) {
rows.push(inf);
}
}
const perDate = rows.reduce((acc, { dt, val }) => {
if (acc.has(dt)) {
acc.set(dt, acc.get(dt) + val)
} else {
acc.set(dt, val)
}
return acc;
}, new Map());
for (const dt of perDate.keys()) {
console.log(`${dt},${perDate.get(dt)},km`)
}
}
// grep HKQuantityTypeIdentifierDistanceWalkingRunning /tmp/apple_health_export/export.xml | grep hardware:Watch | grep 2021 > /tmp/running2021.txt
processLineByLine('/tmp/running2021.txt');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment