Created
January 10, 2019 04:23
-
-
Save Ithildir/5a821645bbf289b1568ff81520e4528e to your computer and use it in GitHub Desktop.
Convert MS Health weights JSON into CSV for Withings Healtmate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const fs = require('fs'); | |
const path = require('path'); | |
const CHUNK_SIZE = 250; | |
const pad = n => n < 10 ? `0${n}` : n; | |
const formatDate = d => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`; | |
function createWithingsWeightFiles(healthPath) { | |
const destDir = path.dirname(healthPath); | |
const health = JSON.parse(fs.readFileSync(healthPath)); | |
const csvEntries = health.all.entries.map((entry) => { | |
const ts = parseInt(entry.EntryDateUtc.substring(6, entry.EntryDateUtc.length - 2)); | |
return `"${formatDate(new Date(ts))}",${entry.Value}`; | |
}); | |
let csvHeader = 'Date,'; | |
if (health.useMetricWeight) { | |
csvHeader += 'Weight'; | |
} else { | |
csvHeader += '"Weight (lb)"'; | |
} | |
for (let chunkNum = 0;; chunkNum++) { | |
const chunk = csvEntries.slice(chunkNum * CHUNK_SIZE, (chunkNum + 1) * CHUNK_SIZE); | |
if (chunk.length === 0) { | |
break; | |
} | |
const csv = `${csvHeader}\n${chunk.join('\n')}`; | |
fs.writeFileSync(path.join(destDir, `withings_weight_${chunkNum + 1}.csv`), csv); | |
} | |
} | |
if (process.argv.length < 3) { | |
throw new Error('Please specify MS Health JSON file') | |
} | |
createWithingsWeightFiles(process.argv[2]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment