Skip to content

Commit

Permalink
Add .checkDatabase and .extract_token functions
Browse files Browse the repository at this point in the history
Reduces repetitive text in main functions and allows storage of data in database if no database is created during the authorization process or deleted for some reason.
  • Loading branch information
bhelsel committed May 9, 2024
1 parent 56e4835 commit d593488
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
30 changes: 14 additions & 16 deletions R/activity.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ get_fitbit_activities <- function(token.pathname, resource = "All Resources",
start.date = Sys.Date(), end.date = Sys.Date(),
returnData = TRUE, toSQL = FALSE){

directory <- dirname(dirname(token.pathname))
token <- readRDS(token.pathname)
token <- token[[2]]
user <- token$credentials$user_id
url_activity <- paste0("https://api.fitbit.com/1/", "user/", user, "/", "activities/")
tkn <- .extract_token(token.pathname)

url_activity <- paste0("https://api.fitbit.com/1/", "user/", tkn$user, "/", "activities/")

# Check to see if data is greater than 100 days
dateDiff <- difftime(end.date, start.date)
Expand All @@ -53,70 +51,70 @@ get_fitbit_activities <- function(token.pathname, resource = "All Resources",

if("activityCalories" %in% resource){
activityCalories.url <- paste0(url_activity, "activityCalories", sprintf("/date/%s/%s.json", start.date[i], end.date[i]))
activityCalories <- jsonlite::fromJSON(httr::content(httr::GET(activityCalories.url, token), as = "text"))[[1]][2]
activityCalories <- jsonlite::fromJSON(httr::content(httr::GET(activityCalories.url, tkn$token), as = "text"))[[1]][2]
colnames(activityCalories) <- "activityCalories"
temp_data <- cbind(temp_data, activityCalories)
}

if("calories" %in% resource){
calories.url <- paste0(url_activity, "calories", sprintf("/date/%s/%s.json", start.date[i], end.date[i]))
calories <- jsonlite::fromJSON(httr::content(httr::GET(calories.url, token), as = "text"))[[1]][2]
calories <- jsonlite::fromJSON(httr::content(httr::GET(calories.url, tkn$token), as = "text"))[[1]][2]
colnames(calories) <- "calories"
temp_data <- cbind(temp_data, calories)
}

if("distance" %in% resource){
distance.url <- paste0(url_activity, "distance", sprintf("/date/%s/%s.json", start.date[i], end.date[i]))
distance <- jsonlite::fromJSON(httr::content(httr::GET(distance.url, token), as = "text"))[[1]][2]
distance <- jsonlite::fromJSON(httr::content(httr::GET(distance.url, tkn$token), as = "text"))[[1]][2]
colnames(distance) <- "distance"
temp_data <- cbind(temp_data, distance)
}

if("elevation" %in% resource){
elevation.url <- paste0(url_activity, "elevation", sprintf("/date/%s/%s.json", start.date[i], end.date[i]))
elevation <- jsonlite::fromJSON(httr::content(httr::GET(elevation.url, token), as = "text"))[[1]][2]
elevation <- jsonlite::fromJSON(httr::content(httr::GET(elevation.url, tkn$token), as = "text"))[[1]][2]
colnames(elevation) <- "elevation"
temp_data <- cbind(temp_data, elevation)
}

if("floors" %in% resource){
floors.url <- paste0(url_activity, "floors", sprintf("/date/%s/%s.json", start.date[i], end.date[i]))
floors <- jsonlite::fromJSON(httr::content(httr::GET(floors.url, token), as = "text"))[[1]][2]
floors <- jsonlite::fromJSON(httr::content(httr::GET(floors.url, tkn$token), as = "text"))[[1]][2]
colnames(floors) <- "floors"
temp_data <- cbind(temp_data, floors)
}

if("minutesSedentary" %in% resource){
minutesSedentary.url <- paste0(url_activity, "minutesSedentary", sprintf("/date/%s/%s.json", start.date[i], end.date[i]))
minutesSedentary <- jsonlite::fromJSON(httr::content(httr::GET(minutesSedentary.url, token), as = "text"))[[1]][2]
minutesSedentary <- jsonlite::fromJSON(httr::content(httr::GET(minutesSedentary.url, tkn$token), as = "text"))[[1]][2]
colnames(minutesSedentary) <- "minutesSedentary"
temp_data <- cbind(temp_data, minutesSedentary)
}

if("minutesLightlyActive" %in% resource){
minutesLightlyActive.url <- paste0(url_activity, "minutesLightlyActive", sprintf("/date/%s/%s.json", start.date[i], end.date[i]))
minutesLightlyActive <- jsonlite::fromJSON(httr::content(httr::GET(minutesLightlyActive.url, token), as = "text"))[[1]][2]
minutesLightlyActive <- jsonlite::fromJSON(httr::content(httr::GET(minutesLightlyActive.url, tkn$token), as = "text"))[[1]][2]
colnames(minutesLightlyActive) <- "minutesLightlyActive"
temp_data <- cbind(temp_data, minutesLightlyActive)
}

if("minutesFairlyActive" %in% resource){
minutesFairlyActive.url <- paste0(url_activity, "minutesFairlyActive", sprintf("/date/%s/%s.json", start.date[i], end.date[i]))
minutesFairlyActive <- jsonlite::fromJSON(httr::content(httr::GET(minutesFairlyActive.url, token), as = "text"))[[1]][2]
minutesFairlyActive <- jsonlite::fromJSON(httr::content(httr::GET(minutesFairlyActive.url, tkn$token), as = "text"))[[1]][2]
colnames(minutesFairlyActive) <- "minutesFairlyActive"
temp_data <- cbind(temp_data, minutesFairlyActive)
}

if("minutesVeryActive" %in% resource){
minutesVeryActive.url <- paste0(url_activity, "minutesVeryActive", sprintf("/date/%s/%s.json", start.date[i], end.date[i]))
minutesVeryActive <- jsonlite::fromJSON(httr::content(httr::GET(minutesVeryActive.url, token), as = "text"))[[1]][2]
minutesVeryActive <- jsonlite::fromJSON(httr::content(httr::GET(minutesVeryActive.url, tkn$token), as = "text"))[[1]][2]
colnames(minutesVeryActive) <- "minutesVeryActive"
temp_data <- cbind(temp_data, minutesVeryActive)
}

if("steps" %in% resource){
steps.url <- paste0(url_activity, "steps", sprintf("/date/%s/%s.json", start.date[i], end.date[i]))
steps <- jsonlite::fromJSON(httr::content(httr::GET(steps.url, token), as = "text"))[[1]][2]
steps <- jsonlite::fromJSON(httr::content(httr::GET(steps.url, tkn$token), as = "text"))[[1]][2]
colnames(steps) <- "steps"
temp_data <- cbind(temp_data, steps)
}
Expand All @@ -125,7 +123,7 @@ get_fitbit_activities <- function(token.pathname, resource = "All Resources",
}

if(toSQL){
database <- grep(user, list.files(paste0(directory, "/data"), full.names = TRUE), value = TRUE)
database <- .checkDatabase(tkn$directory, tkn$user)
con <- DBI::dbConnect(RSQLite::SQLite(), database)
DBI::dbWriteTable(con, "activities", data, overwrite = TRUE)
DBI::dbDisconnect(con)
Expand Down
9 changes: 9 additions & 0 deletions R/database.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,12 @@
}


.checkDatabase <- function(directory, user){
if(!dir.exists(file.path(directory, "data"))) dir.create(file.path(directory, "data"))
user_database <- file.path(directory, "data", sprintf("%s.db", user))
if(!file.exists(user_database)) {
con <- DBI::dbConnect(RSQLite::SQLite(), user_database)
DBI::dbDisconnect(con)
}
return(user_database)
}
13 changes: 6 additions & 7 deletions R/device.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@

get_fitbit_device <- function(token.pathname, returnData = TRUE, toSQL = FALSE){

directory <- dirname(dirname(token.pathname))
tkn <- .extract_token(token.pathname)

devicesData <- data.frame(matrix(ncol = 5, nrow = 0))
colnames(devicesData) <- c("user", "deviceVersion", "lastSyncTime", "battery", "batteryLevel")
token <- readRDS(token.pathname)
token <- token[[2]]
user <- token$credentials$user_id
url_devices <- paste0("https://api.fitbit.com/1/", "user/", user, "/", "devices.json")
device <- jsonlite::fromJSON(httr::content(httr::GET(url_devices, token), as = "text"))
url_devices <- paste0("https://api.fitbit.com/1/", "user/", tkn$user, "/", "devices.json")
device <- jsonlite::fromJSON(httr::content(httr::GET(url_devices, tkn$token), as = "text"))
device <- device[device$deviceVersion != "MobileTrack", ]

if(length(device) != 0){
data <- data.frame(cbind(deviceVersion=device$deviceVersion, lastSyncTime=device$lastSyncTime, battery=device$battery, batteryLevel=device$batteryLevel, type=device$type))
Expand All @@ -35,7 +34,7 @@ get_fitbit_device <- function(token.pathname, returnData = TRUE, toSQL = FALSE){
}

if(toSQL){
database <- grep(user, list.files(paste0(directory, "/data"), full.names = TRUE), value = TRUE)
database <- .checkDatabase(tkn$directory, tkn$user)
con <- DBI::dbConnect(RSQLite::SQLite(), database)
DBI::dbWriteTable(con, "device", data, overwrite = TRUE)
DBI::dbDisconnect(con)
Expand Down
4 changes: 2 additions & 2 deletions R/heart.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ get_fitbit_heart_intraday <- function(token.pathname, start.date = Sys.Date(),
intensity_labels, heart_rate_method, rest.hr, verbose)

if(toSQL){
database <- grep(tkn$user, list.files(paste0(directory, "/data"), full.names = TRUE), value = TRUE)
database <- .checkDatabase(directory, tkn$user)
con <- DBI::dbConnect(RSQLite::SQLite(), database)
DBI::dbWriteTable(con, "heart", heart_data, overwrite = TRUE)
DBI::dbDisconnect(con)
Expand Down Expand Up @@ -228,7 +228,7 @@ calculate_heart_intensities <- function(heart_raw_data = NULL, sleep_data = NULL
heart_raw_data[which(heart_raw_data$nonwear == 1), "hrr.percent"] <- NA
heart_raw_data <- dplyr::bind_cols(cbind(dplyr::select(heart_raw_data, time:nonwear), sapply(intensity_labels, function(x) heart_raw_data[x] <- dplyr::if_else(heart_raw_data$hrr.percent == x, 1, 0, 0))))
cols <- names(heart_raw_data)[names(heart_raw_data) %in% c("sleep", "nonwear", intensity_labels)]
heart_data <- heart_raw_data %>% dplyr::group_by(date = as.Date(time)) %>% dplyr::summarise_at(vars(all_of(cols)), sum, na.rm = TRUE)
heart_data <- heart_raw_data %>% dplyr::group_by(date = as.character(as.Date(time))) %>% dplyr::summarise_at(vars(dplyr::all_of(cols)), sum, na.rm = TRUE)

# Calculate MVPA if not already provided in intensities
if(!any(grepl("mvpa", colnames(heart_data), ignore.case = TRUE))){
Expand Down

0 comments on commit d593488

Please sign in to comment.