Created
January 14, 2025 00:57
-
-
Save timcappalli/dae48666573a8281f5909a20248ffc03 to your computer and use it in GitHub Desktop.
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
const fs = require('fs'); | |
const axios = require('axios'); | |
const csv = require('csv-parser'); | |
const apiUrl = 'https://api.fitbit.com/1/user/-/body/log/weight.json'; | |
const bearerToken = ''; | |
async function delay(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
}; | |
async function processRow(row) { | |
try { | |
await delay(500); // half-second delay | |
const response = await axios.post(apiUrl, null, { | |
params: row, | |
headers: { | |
'Authorization': `Bearer ${bearerToken}` | |
} | |
}); | |
console.log(row) | |
return response.data; | |
} catch (error) { | |
console.error(`Error processing row: ${JSON.stringify(row)}`, error); | |
throw error; | |
} | |
}; | |
async function processCsv(filePath) { | |
try { | |
const rows = []; | |
fs.createReadStream(filePath) | |
.pipe(csv()) | |
.on('data', (row) => rows.push(row)) | |
.on('end', async () => { | |
console.log('CSV reading completed.'); | |
const results = []; | |
for (const row of rows) { | |
try { | |
const result = await processRow(row); | |
results.push(result); | |
} catch (error) { | |
console.error('Error processing row:', error); | |
process.exit(1); | |
} | |
} | |
console.log('CSV processing completed.'); | |
console.log('Results:', results); | |
}); | |
} catch (error) { | |
console.error('Error reading CSV file:', error); | |
} | |
} | |
// Example usage: | |
// CSV format: weight,date,time (weight in KG, date in ISO8601, time 01:23:45) | |
const filePath = 'weight.csv'; | |
processCsv(filePath); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment