Last active
December 23, 2023 04:01
-
-
Save LanHao0/8332a9b018282c343ef30ae132f7e286 to your computer and use it in GitHub Desktop.
generate a google fit year report from google fit json data with javascript 通过google fit年度数据,生成你的年度步数信息
This file contains 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
/* | |
Author: LanHao | |
Time: 12/31/2021 19:31:27 | |
*/ | |
const steps = require('./stepdata'); | |
let arr_month = [] | |
let arr_day = [] | |
let arr_hour = [] | |
let hour_count = []; | |
function setMonthsStepData() { | |
//取得每月走多少步,生成一个数组 | |
for (let el of steps.steps.data) { | |
const currentMonth = parseInt((el.s).substring(5, 7)) - 1; | |
if (arr_month[currentMonth]) { | |
arr_month[currentMonth] += parseInt(el.v); | |
} else { | |
arr_month[currentMonth] = parseInt(el.v); | |
} | |
} | |
// console.log(arr_month); | |
getMaxMonth(); | |
} | |
function getMaxMonth() { | |
//取得最大月数 | |
const maxStepsInMonth = Math.max(...arr_month); | |
console.log("Which Month I walked most: " + (arr_month.indexOf(maxStepsInMonth) + 1)); | |
console.log("I walked " + maxStepsInMonth + " steps in this month.\n"); | |
} | |
function setDayData() { | |
for (let el of steps.steps.data) { | |
const currentMonth = parseInt((el.s).substring(5, 7)) - 1; | |
const currentDay = parseInt((el.s).substring(8, 10) - 1); | |
if (arr_day[currentMonth]) { | |
if (arr_day[currentMonth][currentDay]) { | |
arr_day[currentMonth][currentDay] += parseInt(el.v) | |
} else { | |
arr_day[currentMonth][currentDay] = parseInt(el.v) | |
} | |
} else { | |
arr_day[currentMonth] = []; | |
} | |
} | |
// console.log(arr_day) | |
getMaxDay() | |
} | |
function getMaxDay() { | |
let maxSteps = 0; | |
let month = 0; | |
let day = 0; | |
for (let i = 0; i < arr_day.length; i++) { | |
for (let j = 0; j < arr_day[i].length; j++) { | |
if (arr_day[i][j] > maxSteps) { | |
maxSteps = arr_day[i][j]; | |
month = i + 1; | |
day = j + 1; | |
} | |
} | |
} | |
console.log("the month is " + month + ", the day is " + day + "."); | |
console.log("In this day I walked a lot, I walked: " + maxSteps + " steps"); | |
} | |
function setHourData() { | |
//get hour array for steps, which hour i walk most | |
for (let el of steps.steps.data) { | |
const currentHour = parseInt((el.s).substring(11, 13)); | |
if (arr_hour[currentHour]) { | |
arr_hour[currentHour] += 'a'; | |
} else { | |
arr_hour[currentHour] = 'a'; | |
} | |
} | |
// console.log(arr_hour) | |
getMaxHour() | |
} | |
function getMaxHour() { | |
let tmp = 0; | |
let hour = 0; | |
for (let i = 0; i < arr_hour.length; i++) { | |
if (arr_hour[i].length > tmp) { | |
tmp = arr_hour[i].length; | |
hour = i | |
} | |
hour_count[i] = arr_hour[i].length; | |
} | |
console.log("\nI always start my walk at: " + hour + " o'clock"); | |
} | |
function getYearTotal() { | |
let res = 0; | |
for (let el of arr_month) { | |
res += el; | |
} | |
console.log("\nIn total, I walked " + res + " steps in year " + (steps.steps.data[0].s).substring(0, 4)); | |
} | |
setMonthsStepData(); | |
setDayData(); | |
setHourData(); | |
getYearTotal(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment