-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoing-duty-to-do.R
141 lines (105 loc) · 2.48 KB
/
doing-duty-to-do.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# script for extracting data from suunto watch for storage and use at hompage
# packages
library(XML)
library(data.table)
library(dplyr)
library(jsonlite)
library(stringr)
library(lubridate)
# functions
get_data <- function(xml){
tmp <- xpathApply(xml, "//*[local-name() = 'Header']", xmlToList)[[1]]
tmp <- as.data.frame(t(unlist(tmp)))
return(tmp)
}
# data extraction
files <- dir(path.expand("~/AppData/Roaming/Suunto/Moveslink2"), pattern="sml", full.names = TRUE)
# storing raw in data path
dir.create("data", showWarnings = FALSE)
file.copy(files, "./data")
# parsing data
XML <- lapply(files, xmlParse)
DATA <- lapply(XML, get_data)
data_df <- rbindlist(DATA, fill=TRUE)
# loading already stored data
if( file.exists("suunto_data.Rdata") ){
load("suunto_data.Rdata")
}else{
data_df_all <- data.frame()
}
# combining both data sources
data_df_all <- unique(rbind(data_df, data_df_all))
save(data_df_all, file="suunto_data.Rdata")
# write to json
writeLines(
toJSON(
x = list(run_data = data_df_all),
pretty = TRUE
),
"./data/suunto_data.json"
)
# analytics data
df <-
data_df_all %>%
select(
Activity,
ActivityType,
Distance,
Duration,
Energy,
DateTime
) %>%
filter(
ActivityType == 3
) %>%
mutate(
duration_min = round(as.numeric(as.character(Duration)) / 60, 2),
date = as.Date(substring(DateTime, 1,10)),
kj = round(as.numeric(as.character(Energy))/1000),
kcal = round(as.numeric(as.character(Energy))/4184),
distance = round(as.numeric(as.character(Distance))/1000, 2),
kmh = round(distance / (duration_min / 60), 2),
mkm =
paste0(
floor(duration_min / distance),
":",
str_pad(ceiling(((duration_min/distance) %% 1) * 60), width = 2, side = "left", pad = "0")
),
year = year(date),
week = week(date)
) %>%
select(
date,
distance,
duration_min,
kmh,
mkm,
kj,
kcal,
year,
week
) %>%
group_by(year, week) %>%
mutate(
dist_weekly = sum(distance),
kcal_weekly = sum(kcal)
) %>%
ungroup() %>%
as.data.frame()%>%
arrange(
-as.integer(date)
)
writeLines(
toJSON(
x = list(run_data = df),
pretty = TRUE
),
"./data/suunto_data_analytics.json"
)
# add everything to repo
try({
system("git add *")
system("git status")
system(paste0("git commit -m ", '"update ',as.character(Sys.time()),'"'))
system("git push")
})