Initial commit of counter
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
/**
|
||||
* Apple Health export parser.
|
||||
*
|
||||
* Reads `export.xml` in chunks so a several-hundred-megabyte export can be
|
||||
* processed without leaving the phone or loading the whole file into memory.
|
||||
* Only step records and workouts are kept.
|
||||
*/
|
||||
(function (root, factory) {
|
||||
if (typeof module === 'object' && module.exports) module.exports = factory();
|
||||
else root.HealthImport = factory();
|
||||
}(typeof self !== 'undefined' ? self : this, function () {
|
||||
'use strict';
|
||||
|
||||
const STEP_TYPE = 'HKQuantityTypeIdentifierStepCount';
|
||||
|
||||
const WORKOUT_TYPES = {
|
||||
Running: { name: 'Running', met: 9.0 },
|
||||
Walking: { name: 'Walking', met: 4.3 },
|
||||
Cycling: { name: 'Cycling', met: 7.5 },
|
||||
Swimming: { name: 'Swimming', met: 6.0 },
|
||||
Hiking: { name: 'Hiking', met: 6.0 },
|
||||
Yoga: { name: 'Yoga', met: 2.5 },
|
||||
Rowing: { name: 'Rowing', met: 7.0 },
|
||||
Elliptical: { name: 'Elliptical', met: 5.0 },
|
||||
Dance: { name: 'Dance', met: 5.5 },
|
||||
CardioDance: { name: 'Dance', met: 5.5 },
|
||||
HighIntensityIntervalTraining: { name: 'HIIT', met: 8.0 },
|
||||
FunctionalStrengthTraining: { name: 'Strength training', met: 5.0 },
|
||||
TraditionalStrengthTraining: { name: 'Strength training', met: 5.0 },
|
||||
CoreTraining: { name: 'Core training', met: 3.5 },
|
||||
Flexibility: { name: 'Stretching', met: 2.3 },
|
||||
MindAndBody: { name: 'Yoga', met: 2.5 },
|
||||
Pilates: { name: 'Pilates', met: 3.0 },
|
||||
Soccer: { name: 'Soccer', met: 7.0 },
|
||||
Basketball: { name: 'Basketball', met: 6.5 },
|
||||
Tennis: { name: 'Tennis', met: 7.3 },
|
||||
Golf: { name: 'Golf', met: 4.8 },
|
||||
Skiing: { name: 'Skiing', met: 7.0 },
|
||||
Snowboarding: { name: 'Snowboarding', met: 5.3 },
|
||||
Surfing: { name: 'Surfing', met: 5.0 },
|
||||
Climbing: { name: 'Climbing', met: 8.0 },
|
||||
StairClimbing: { name: 'Stair climbing', met: 8.8 },
|
||||
JumpRope: { name: 'Jump rope', met: 12.3 },
|
||||
Boxing: { name: 'Boxing', met: 7.8 },
|
||||
MartialArts: { name: 'Martial arts', met: 10.3 },
|
||||
TeamSports: { name: 'Team sport', met: 7.0 },
|
||||
Other: { name: 'Workout', met: 6.0 },
|
||||
};
|
||||
|
||||
function parseAttrs(source) {
|
||||
const attrs = {};
|
||||
const re = /([A-Za-z_:][-A-Za-z0-9_:.]*)="([^"]*)"/g;
|
||||
let match;
|
||||
while ((match = re.exec(source))) attrs[match[1]] = match[2];
|
||||
return attrs;
|
||||
}
|
||||
|
||||
function localDay(value) {
|
||||
if (typeof value !== 'string') return null;
|
||||
const match = /^(\d{4}-\d{2}-\d{2})/.exec(value);
|
||||
return match ? match[1] : null;
|
||||
}
|
||||
|
||||
function appleDate(value) {
|
||||
if (typeof value !== 'string') return NaN;
|
||||
// Apple uses "2026-11-06 07:12:00 -0700".
|
||||
const iso = value.trim().replace(' ', 'T').replace(/ ([+-]\d{2})(\d{2})$/, '$1:$2');
|
||||
return Date.parse(iso);
|
||||
}
|
||||
|
||||
function workoutMeta(type) {
|
||||
const key = String(type || '').replace(/^HKWorkoutActivityType/, '');
|
||||
if (WORKOUT_TYPES[key]) return WORKOUT_TYPES[key];
|
||||
const name = key ? key.replace(/([a-z0-9])([A-Z])/g, '$1 $2').toLowerCase() : 'workout';
|
||||
return { name: name.charAt(0).toUpperCase() + name.slice(1), met: 6 };
|
||||
}
|
||||
|
||||
function durationMinutes(attrs) {
|
||||
const duration = Number(attrs.duration);
|
||||
if (Number.isFinite(duration) && duration > 0) return duration;
|
||||
|
||||
const start = appleDate(attrs.startDate);
|
||||
const end = appleDate(attrs.endDate);
|
||||
if (Number.isFinite(start) && Number.isFinite(end) && end > start) {
|
||||
return (end - start) / 60000;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
function createParser({ since = '' } = {}) {
|
||||
let tail = '';
|
||||
const stepDays = new Map();
|
||||
const workouts = new Map();
|
||||
|
||||
function consume(text) {
|
||||
const re = /<(Record|Workout)\b([^>]*)>/g;
|
||||
let match;
|
||||
while ((match = re.exec(text))) {
|
||||
const attrs = parseAttrs(match[2]);
|
||||
const day = localDay(attrs.startDate);
|
||||
if (!day || (since && day < since)) continue;
|
||||
|
||||
if (match[1] === 'Record') {
|
||||
if (attrs.type !== STEP_TYPE) continue;
|
||||
const value = Number(attrs.value);
|
||||
if (!Number.isFinite(value) || value <= 0) continue;
|
||||
stepDays.set(day, (stepDays.get(day) || 0) + value);
|
||||
continue;
|
||||
}
|
||||
|
||||
const meta = workoutMeta(attrs.workoutActivityType);
|
||||
const minutes = durationMinutes(attrs);
|
||||
if (!(minutes > 0)) continue;
|
||||
|
||||
const externalId = `apple:${attrs.workoutActivityType || 'workout'}:${attrs.startDate}`;
|
||||
if (workouts.has(externalId)) continue;
|
||||
|
||||
const energy = Number(attrs.totalEnergyBurned);
|
||||
workouts.set(externalId, {
|
||||
externalId,
|
||||
date: day,
|
||||
name: meta.name,
|
||||
minutes: Math.round(minutes * 10) / 10,
|
||||
kcal: Number.isFinite(energy) && energy > 0 ? Math.round(energy) : 0,
|
||||
met: meta.met,
|
||||
});
|
||||
}
|
||||
|
||||
const lastOpen = text.lastIndexOf('<');
|
||||
return lastOpen !== -1 && text.indexOf('>', lastOpen) === -1 ? text.slice(lastOpen) : '';
|
||||
}
|
||||
|
||||
return {
|
||||
push(text) {
|
||||
tail = consume(tail + text);
|
||||
},
|
||||
finish() {
|
||||
return {
|
||||
days: Array.from(stepDays, ([date, steps]) => ({ date, steps: Math.round(steps) })),
|
||||
workouts: Array.from(workouts.values()),
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function parseFile(file, options = {}) {
|
||||
const parser = createParser(options);
|
||||
const size = file.size || 0;
|
||||
const chunkSize = 4 * 1024 * 1024;
|
||||
let offset = 0;
|
||||
|
||||
while (offset < size) {
|
||||
const text = await file.slice(offset, offset + chunkSize).text();
|
||||
parser.push(text);
|
||||
offset += chunkSize;
|
||||
if (options.onProgress) {
|
||||
options.onProgress(Math.min(1, offset / size));
|
||||
}
|
||||
}
|
||||
if (options.onProgress) options.onProgress(1);
|
||||
return parser.finish();
|
||||
}
|
||||
|
||||
return { createParser, parseFile, STEP_TYPE, WORKOUT_TYPES };
|
||||
}));
|
||||
Reference in New Issue
Block a user