[med-svn] [r-cran-fitbitscraper] 02/05: Imported Upstream version 0.1.6
Dylan Aïssi
bob.dybian-guest at moszumanska.debian.org
Wed Apr 27 06:09:21 UTC 2016
This is an automated email from the git hooks/post-receive script.
bob.dybian-guest pushed a commit to branch master
in repository r-cran-fitbitscraper.
commit 3ea99c152ac2ef7c4be8e51e9cbe530aa8432336
Author: Dylan Aïssi <bob.dybian at gmail.com>
Date: Tue Apr 26 22:48:12 2016 +0200
Imported Upstream version 0.1.6
---
DESCRIPTION | 18 ++++++++++
LICENSE | 2 ++
MD5 | 19 +++++++++++
NAMESPACE | 9 +++++
NEWS.md | 40 ++++++++++++++++++++++
R/get_activity_data.R | 65 +++++++++++++++++++++++++++++++++++
R/get_daily_data.R | 86 +++++++++++++++++++++++++++++++++++++++++++++++
R/get_intraday_data.R | 55 ++++++++++++++++++++++++++++++
R/get_premium_export.R | 84 +++++++++++++++++++++++++++++++++++++++++++++
R/get_sleep_data.R | 52 ++++++++++++++++++++++++++++
R/get_weight_data.R | 39 +++++++++++++++++++++
R/login.R | 47 ++++++++++++++++++++++++++
README.md | 32 ++++++++++++++++++
man/get_activity_data.Rd | 37 ++++++++++++++++++++
man/get_daily_data.Rd | 33 ++++++++++++++++++
man/get_intraday_data.Rd | 31 +++++++++++++++++
man/get_premium_export.Rd | 34 +++++++++++++++++++
man/get_sleep_data.Rd | 31 +++++++++++++++++
man/get_weight_data.Rd | 31 +++++++++++++++++
man/login.Rd | 29 ++++++++++++++++
20 files changed, 774 insertions(+)
diff --git a/DESCRIPTION b/DESCRIPTION
new file mode 100644
index 0000000..25a8dea
--- /dev/null
+++ b/DESCRIPTION
@@ -0,0 +1,18 @@
+Package: fitbitScraper
+Title: Scrapes Data from Fitbit
+Version: 0.1.6
+Author: Cory Nissen <corynissen at gmail.com> [aut, cre]
+Maintainer: Cory Nissen <corynissen at gmail.com>
+Description: Scrapes data from Fitbit <http://www.fitbit.com>. This does not use the official
+ API, but instead uses the API that the web dashboard uses to generate the graphs
+ displayed on the dashboard after login at <http://www.fitbit.com>.
+Depends: R (>= 3.0.0)
+License: MIT + file LICENSE
+LazyData: true
+Imports: httr, stringr, jsonlite, methods, utils
+URL: https://github.com/corynissen/fitbitScraper
+RoxygenNote: 5.0.1
+NeedsCompilation: no
+Packaged: 2016-04-11 15:13:52 UTC; 60018847
+Repository: CRAN
+Date/Publication: 2016-04-11 18:38:35
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..995931c
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,2 @@
+YEAR: 2014
+COPYRIGHT HOLDER: Cory Nissen
diff --git a/MD5 b/MD5
new file mode 100644
index 0000000..d27694e
--- /dev/null
+++ b/MD5
@@ -0,0 +1,19 @@
+0f2bb69c7fa616218574664c6aea3e36 *DESCRIPTION
+d499114c8fb7674b84d73553dd6e8d9a *LICENSE
+af05ec54f552800fae2fa87191cbef9c *NAMESPACE
+88390e2e5dd3e55a0c71f936eb4f60bc *NEWS.md
+ca12202aff2a6dd5b672b54b269170fe *R/get_activity_data.R
+6f22f81c632183d6bb0d26638f2741c5 *R/get_daily_data.R
+bbdfa193a0b307c8f634c89681ad87d1 *R/get_intraday_data.R
+c1b92b4d6f2af001a7c7227c927360f4 *R/get_premium_export.R
+a363b6cd2faa8a8ccfdb2052dfb42540 *R/get_sleep_data.R
+e541d7ca220d36967d1e210f94f6ce68 *R/get_weight_data.R
+102dcc2840fa122eea731fa2f5b4b6c8 *R/login.R
+9f7ac5485822d04895cd5ad0a64abf34 *README.md
+b4bd6aacbc1f19392f02d757e13ac0ee *man/get_activity_data.Rd
+5780a406e827724de4d3cc2b96913fd1 *man/get_daily_data.Rd
+b9312f0b0bace2795a3229bc19efeac3 *man/get_intraday_data.Rd
+0f12986e8d5ef889d56848ecdb09b4e3 *man/get_premium_export.Rd
+cf2f8c7c4abd423007bdf5db9b0abd15 *man/get_sleep_data.Rd
+dc0c871efcadc80badd75074df7629af *man/get_weight_data.Rd
+88fd5d742acc1558be0f54313b60c675 *man/login.Rd
diff --git a/NAMESPACE b/NAMESPACE
new file mode 100644
index 0000000..739f429
--- /dev/null
+++ b/NAMESPACE
@@ -0,0 +1,9 @@
+# Generated by roxygen2: do not edit by hand
+
+export(get_activity_data)
+export(get_daily_data)
+export(get_intraday_data)
+export(get_premium_export)
+export(get_sleep_data)
+export(get_weight_data)
+export(login)
diff --git a/NEWS.md b/NEWS.md
new file mode 100644
index 0000000..2a51958
--- /dev/null
+++ b/NEWS.md
@@ -0,0 +1,40 @@
+
+### fitbitScraper 0.1.6
+* switch from RJSONIO to jsonlite
+
+### fitbitScraper 0.1.5
+* use real URL in DESCRIPTION
+* added <> around URLs in DESCRIPTION
+* added methods and utils to imports in DESCRIPTION
+* added corresponding methods:: and utils:: in code
+* changed get_activity_data() function to have a working end_date.
+* pull request #7 calories burned vs intake
+* pull request #5 Add check.names=T for creating data.frames
+* pull request #4 slight changes to sleep variable names
+* pull request #3 Update login.R
+
+### fitbitScraper 0.1.4
+* added get_activity_data() function
+* added "getRestingHeartRateData" to get_daily_data() function
+* added rememberMe parameter to login function
+* merged pull request #2, a change to the login function
+
+### fitbitScraper 0.1.3
+* Changed the API calls to match changes on fitbit end of things.
+
+### fitbitScraper 0.1.2
+* Added get_sleep_data()
+* Added get_premium_export()
+* Changed output column of get_daily_data(), get_15_min_data(), and get_weight_data() to correspond to the data type requested... for example: "weight" instead of "data"", "steps" instead of "data"
+* Added heart-rate for get_15_min_data() and get_daily_data()
+* added get_intraday_data()
+* Deprecated get_15_min_data(), use get_intraday_data() instead
+
+### fitbitScraper 0.1.1
+* Basic checks included for arguments
+* tz added to the return dataframes as.POSIXct date field
+* documentation cleanup
+
+### fitbitScraper 0.1
+* No error checking
+* Three functions: login, get_daily_data, get_15_min_data
diff --git a/R/get_activity_data.R b/R/get_activity_data.R
new file mode 100644
index 0000000..055c821
--- /dev/null
+++ b/R/get_activity_data.R
@@ -0,0 +1,65 @@
+#' Get activity data from fitbit.com
+#'
+#' Get activity data from fitbit using cookie returned from login function
+#' @param cookie Cookie returned after login, specifically the "u" cookie
+#' @param end_date Date in YYYY-MM-DD format
+#' @keywords data
+#' @export
+#' @return A dataframe with thirteen columns:
+#' \item{id}{The fitbit ID of the activity}
+#' \item{name}{Name for the type of activity}
+#' \item{date}{Date of the activity}
+#' \item{start_time}{Start time for the activity}
+#' \item{distance}{Distance travelled during the activity}
+#' \item{duration}{Duration in hours:minutes:seconds of the activity}
+#' \item{calories}{Calories burned during the activity}
+#' \item{steps}{Steps taken the activity}
+#' \item{start_datetime}{A POSIXct encoded start time for the activity}
+#' \item{end_datetime}{A POSIXct encoded end time for the activity}
+#' @examples
+#' \dontrun{
+#' get_activity_data(cookie, end_date="2015-01-20")
+#' }
+#' get_activity_data
+get_activity_data <- function(cookie, end_date){
+ if(!is.character(cookie)){stop("cookie must be a character string")}
+ if(!is.character(end_date)){stop("end_date must be a character string")}
+ if(!grepl("[0-9]{4}-[0-9]{2}-[0-9]{2}", end_date)){stop('end_date must have format "YYYY-MM-DD"')}
+
+ url <- "https://www.fitbit.com/ajaxapi"
+
+ request <- paste0('{"serviceCalls":[{"id":"GET /api/2/user/activities/logs","name":"user","method":"getActivitiesLogs","args":{',
+ '"beforeDate":"',
+ end_date,
+ 'T00:00:00",',
+ '"period":"day","offset":0,"limit":100}},{"id":"GET /api/2/user/activities/logs/summary","name":"user","method":"getActivitiesLogsSummary","args":{"fromDate":"',
+ end_date,
+ '","toDate":"',
+ end_date,
+ '","period":"day","offset":0,"limit":10}}],"template":"activities/modules/models/ajax.response.json.jsp"}'
+ )
+
+ csrfToken <- stringr::str_extract(cookie,
+ "[A-Z0-9]{8}\\-[A-Z0-9]{4}\\-[A-Z0-9]{4}\\-[A-Z0-9]{4}\\-[0-9A-Z]{12}")
+ body <- list(request=request, csrfToken = csrfToken)
+ response <- httr::POST(url, body=body, httr::config(cookie=cookie))
+
+ dat_string <- methods::as(response, "character")
+ dat_list <- jsonlite::fromJSON(dat_string)
+
+ if("GET /api/2/user/activities/logs" %in% names(dat_list)){
+ df <- dat_list[["GET /api/2/user/activities/logs"]]["result"][[1]]
+ }else{
+ df <- NULL
+ print("unable to retrieve activities data")
+ }
+
+ tz <- Sys.timezone()
+ if(is.null(tz) | is.na(tz)){tz <- format(Sys.time(),"%Z")}
+ df$start_datetime <- as.POSIXct(paste0(df$date, " ", df$formattedStartTime),
+ format="%Y-%m-%d %H:%M", tz=tz)
+ df$end_datetime <- as.POSIXct(paste0(df$date, " ", df$formattedEndTime),
+ format="%Y-%m-%d %H:%M", tz=tz)
+
+ return(df)
+}
diff --git a/R/get_daily_data.R b/R/get_daily_data.R
new file mode 100644
index 0000000..964dada
--- /dev/null
+++ b/R/get_daily_data.R
@@ -0,0 +1,86 @@
+#' Get daily data from fitbit.com
+#'
+#' Get daily data from fitbit using cookie returned from login function
+#' @param cookie Cookie returned after login, specifically the "u" cookie
+#' @param what What data you wish to be returned. Options include "steps", "distance", "floors", "minutesVery", "caloriesBurnedVsIntake", "getTimeInHeartRateZonesPerDay", "getRestingHeartRateData"
+#' @param start_date Date in YYYY-MM-DD format
+#' @param end_date Date in YYYY-MM-DD format
+#' @keywords data
+#' @export
+#' @return A dataframe with two columns:
+#' \item{time}{A POSIXct time value}
+#' \item{data}{The data column corresponding to the choice of "what"}
+#' @examples
+#' \dontrun{
+#' get_daily_data(cookie, what="steps", start_date="2015-01-13", end_date="2015-01-20")
+#' }
+#' get_daily_data
+get_daily_data <- function(cookie, what="steps", start_date, end_date){
+ if(!is.character(cookie)){stop("cookie must be a character string")}
+ if(!is.character(what)){stop("what must be a character string")}
+ if(!is.character(start_date)){stop("start_date must be a character string")}
+ if(!is.character(end_date)){stop("end_date must be a character string")}
+ if(!grepl("[0-9]{4}-[0-9]{2}-[0-9]{2}", start_date)){stop('start_date must have format "YYYY-MM-DD"')}
+ if(!grepl("[0-9]{4}-[0-9]{2}-[0-9]{2}", end_date)){stop('end_date must have format "YYYY-MM-DD"')}
+ if(!what %in% c("steps", "distance", "floors", "minutesVery", "caloriesBurnedVsIntake",
+ "getTimeInHeartRateZonesPerDay", "getRestingHeartRateData")){
+ stop('what must be one of "steps", "distance", "floors", "minutesVery", "caloriesBurnedVsIntake", "getTimeInHeartRateZonesPerDay", "getRestingHeartRateData"')
+ }
+
+ if(what %in% c("getTimeInHeartRateZonesPerDay", "getRestingHeartRateData")){
+ url <- "https://www.fitbit.com/ajaxapi"
+ request <- paste0('{"template":"/ajaxTemplate.jsp","serviceCalls":[{"name":"activityTileData","args":{"startDate":"',
+ start_date,
+ '","endDate":"',
+ end_date,
+ '"},"method":"',
+ what,
+ '"}]}'
+ )
+ csrfToken <- stringr::str_extract(cookie,
+ "[A-Z0-9]{8}\\-[A-Z0-9]{4}\\-[A-Z0-9]{4}\\-[A-Z0-9]{4}\\-[0-9A-Z]{12}")
+ body <- list(request=request, csrfToken = csrfToken)
+ response <- httr::POST(url, body=body, httr::config(cookie=cookie))
+ }else{
+ url <- "https://www.fitbit.com/graph/getNewGraphData"
+ query <- list("type" = what,
+ "dateFrom" = start_date,
+ "dateTo" = end_date,
+ "granularity" = "DAILY",
+ "hidePrecreationData" = "false")
+
+ response <- httr::GET(url, query=query, httr::config(cookie=cookie))
+ }
+
+ dat_string <- methods::as(response, "character")
+ dat_list <- jsonlite::fromJSON(dat_string)
+
+ if(what=="getTimeInHeartRateZonesPerDay"){
+ df <- cbind(dat_list$dateTime, dat_list$value, stringsAsFactors=FALSE)
+ names(df)[1:3] <- c("time", "zone1", "zone2", "zone3")
+ }else if(what=="getRestingHeartRateData"){
+ df <- dat_list$dataPoints
+ df <- df[, c("date", "value")]
+ names(df)[1:2] <- c("time", "restingHeartRate")
+ }else if(what=="caloriesBurnedVsIntake"){
+ df_burn <- dat_list[["graph"]][["dataSets"]][["activity"]][["dataPoints"]]
+ df_int <- dat_list[["graph"]][["dataSets"]][["caloriesIntake"]][["dataPoints"]]
+ names(df_burn)[1:2] <- c("time", "caloriesBurned")
+ names(df_int)[1:2] <- c("time", "caloriesIntake")
+ df <- merge(df_burn, df_int, by="time")
+ }else{
+ df <- dat_list[["graph"]][["dataSets"]][["activity"]][["dataPoints"]]
+ names(df)[1:2] <- c("time", what)
+ }
+
+ if(what=="getRestingHeartRateData"){
+ tz <- Sys.timezone()
+ if(is.null(tz)){tz <- format(Sys.time(),"%Z")}
+ df$time <- as.POSIXct(df$time, "%Y-%m-%d", tz=tz)
+ }else{
+ tz <- Sys.timezone()
+ if(is.null(tz)){tz <- format(Sys.time(),"%Z")}
+ df$time <- as.POSIXct(df$time, "%Y-%m-%d %H:%M:%S", tz=tz)
+ }
+ return(df)
+}
diff --git a/R/get_intraday_data.R b/R/get_intraday_data.R
new file mode 100644
index 0000000..e77e596
--- /dev/null
+++ b/R/get_intraday_data.R
@@ -0,0 +1,55 @@
+#' Get intraday data from fitbit.com
+#'
+#' Get intraday data from fitbit using cookie returned from login function
+#' @param cookie Cookie returned after login, specifically the "u" cookie
+#' @param what What data you wish to be returned. Options include "steps", "distance", "floors", "active-minutes", "calories-burned", "heart-rate"
+#' @param date Date in YYYY-MM-DD format
+#' @keywords data
+#' @export
+#' @return A dataframe with two columns:
+#' \item{time}{A POSIXct time value}
+#' \item{data}{The data column corresponding to the choice of "what"}
+#' @examples
+#' \dontrun{
+#' get_intraday_data(cookie, what="steps", date="2015-01-20")
+#' }
+#' get_intraday_data
+get_intraday_data <- function(cookie, what="steps", date){
+ if(!is.character(cookie)){stop("cookie must be a character string")}
+ if(!is.character(what)){stop("what must be a character string")}
+ if(!is.character(date)){stop("date must be a character string")}
+ if(!grepl("[0-9]{4}-[0-9]{2}-[0-9]{2}", date)){stop('date must have format "YYYY-MM-DD"')}
+ if(!what %in% c("steps", "distance", "floors", "active-minutes", "calories-burned",
+ "heart-rate")){
+ stop('what must be one of "steps", "distance", "floors", "active-minutes", "calories-burned",
+ "heart-rate"')
+ }
+
+ url <- "https://www.fitbit.com/ajaxapi"
+ request <- paste0('{"template":"/ajaxTemplate.jsp","serviceCalls":[{"name":"activityTileData","args":{"date":"',
+ date,
+ '","dataTypes":"',
+ what,
+ '"},"method":"getIntradayData"}]}'
+ )
+ csrfToken <- stringr::str_extract(cookie,
+ "[A-Z0-9]{8}\\-[A-Z0-9]{4}\\-[A-Z0-9]{4}\\-[A-Z0-9]{4}\\-[0-9A-Z]{12}")
+ body <- list(request=request, csrfToken = csrfToken)
+ response <- httr::POST(url, body=body, httr::config(cookie=cookie))
+
+ dat_string <- methods::as(response, "character")
+ dat_list <- jsonlite::fromJSON(dat_string)
+
+ df <- dat_list[["dataSets"]][["activity"]][["dataPoints"]][[1]]
+ if(what=="heart-rate"){
+ tz <- Sys.timezone()
+ if(is.null(tz)){tz <- format(Sys.time(),"%Z")}
+ df$time <- as.POSIXct(df$dateTime, "%Y-%m-%d %H:%M:%S", tz=tz)
+ }else{
+ names(df)[1:2] <- c("time", what)
+ tz <- Sys.timezone()
+ if(is.null(tz)){tz <- format(Sys.time(),"%Z")}
+ df$time <- as.POSIXct(df$time, "%Y-%m-%d %H:%M:%S", tz=tz)
+ }
+ return(df)
+}
diff --git a/R/get_premium_export.R b/R/get_premium_export.R
new file mode 100644
index 0000000..e8ee26c
--- /dev/null
+++ b/R/get_premium_export.R
@@ -0,0 +1,84 @@
+#' Get official data export from fitbit.com premium
+#'
+#' Get official data export from fitbit premium using cookie returned from login function. This should be used over individual calls to get_daily_data(), etc. if you subscribe to premium and data export is allowed. I'm not subscribed to premium, but it works for me...
+#' @param cookie Cookie returned after login, specifically the "u" cookie
+#' @param what What data you wish to be returned. Options include "BODY", "FOODS", "ACTIVITIES", "SLEEP"
+#' @param start_date Date in YYYY-MM-DD format
+#' @param end_date Date in YYYY-MM-DD format
+#' @keywords data
+#' @export
+#' @return A list with two things
+#' \item{summary}{A list of sleep summary values}
+#' \item{df}{A data frame containing various sleep values over time}
+#' @examples
+#' \dontrun{
+#' get_premium_export(cookie, what="ACTIVITIES", start_date="2015-01-13", end_date="2015-01-20")
+#' }
+#' get_premium_export
+get_premium_export <- function(cookie, what="ACTIVITIES", start_date="2015-01-13", end_date="2015-01-20"){
+ if(!is.character(cookie)){stop("cookie must be a character string")}
+ if(!is.character(what)){stop("what must be a character string")}
+ if(!is.character(start_date)){stop("start_date must be a character string")}
+ if(!is.character(end_date)){stop("end_date must be a character string")}
+ if(!grepl("[0-9]{4}-[0-9]{2}-[0-9]{2}", start_date)){stop('start_date must have format "YYYY-MM-DD"')}
+ if(!grepl("[0-9]{4}-[0-9]{2}-[0-9]{2}", end_date)){stop('end_date must have format "YYYY-MM-DD"')}
+ if(!what %in% c("BODY", "FOODS", "ACTIVITIES", "SLEEP")){
+ stop('what must be one of "BODY", "FOODS", "ACTIVITIES", "SLEEP"')
+ }
+
+ # as of 5/2015-ish, the date format is not MM/DD/YYYY
+ start_date <- format(as.Date(start_date), "%m/%d/%Y")
+ end_date <- format(as.Date(end_date), "%m/%d/%Y")
+
+ url <- "https://www.fitbit.com/export/user/data"
+ header <- list("Content-Type"="application/x-www-form-urlencoded",
+ "u"=cookie,
+ "Host"="www.fitbit.com",
+ "Origin"="https://www.fitbit.com",
+ "Referer"="https://www.fitbit.com/export/user/data",
+ "User-Agent"="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36",
+ "X-Requested-With"="XMLHttpRequest")
+ body <- list("export"="true",
+ "dataPeriod.periodType"="CUSTOM",
+ "startDate"=start_date,
+ "endDate"=end_date,
+ "dataExportType"=what,
+ "fileFormat"="CSV")
+
+ response <- httr::POST(url, header=header, body=body,
+ httr::config(cookie=cookie))
+ if(response$status_code!=200){
+ stop("problem with request, this may be available only for premium subscribers")
+ }
+ file_id <- methods::as(response, "character")
+ file_id <- jsonlite::fromJSON(file_id)
+ file_id <- file_id["fileIdentifier"]
+
+ # see if file ready for download
+ get_file_status <- function(file_id){
+ is_ready <- httr::GET(paste0("https://www.fitbit.com/premium/export?isExportedFileReady=true&fileIdentifier=",
+ file_id))
+ if("fileIsReady" %in% names(httr::content(is_ready))){
+ is_ready <- httr::content(is_ready)$fileIsReady
+ }else{
+ stop("file_id not found while retrieving file status")
+ }
+ return(is_ready)
+ }
+
+ start <- Sys.time()
+ is_ready <- get_file_status(file_id)
+ while(!is_ready){
+ if(Sys.time() - start > 10){
+ stop("timeout waiting for file to generate")
+ }
+ Sys.sleep(.5)
+ is_ready <- get_file_status(file_id)
+ }
+
+ a <- httr::GET(paste0("https://www.fitbit.com/premium/export/download/",
+ file_id))
+ df <- utils::read.csv(text=methods::as(a, "character"), skip=1,
+ stringsAsFactors=F)
+ return(df)
+}
diff --git a/R/get_sleep_data.R b/R/get_sleep_data.R
new file mode 100644
index 0000000..8cd7019
--- /dev/null
+++ b/R/get_sleep_data.R
@@ -0,0 +1,52 @@
+#' Get sleep data from fitbit.com
+#'
+#' Get sleep data from fitbit using cookie returned from login function
+#' @param cookie Cookie returned after login, specifically the "u" cookie
+#' @param start_date Date in YYYY-MM-DD format
+#' @param end_date Date in YYYY-MM-DD format
+#' @keywords data
+#' @export
+#' @return A list with two things
+#' \item{summary}{A list of sleep summary values}
+#' \item{df}{A data frame containing various sleep values over time}
+#' @examples
+#' \dontrun{
+#' get_sleep_data(cookie, start_date="2015-01-13", end_date="2015-01-20")
+#' }
+#' get_sleep_data
+get_sleep_data <- function(cookie, start_date="2015-01-13", end_date="2015-01-20"){
+ if(!is.character(cookie)){stop("cookie must be a character string")}
+ if(!is.character(start_date)){stop("start_date must be a character string")}
+ if(!is.character(end_date)){stop("end_date must be a character string")}
+ if(!grepl("[0-9]{4}-[0-9]{2}-[0-9]{2}", start_date)){stop('start_date must have format "YYYY-MM-DD"')}
+ if(!grepl("[0-9]{4}-[0-9]{2}-[0-9]{2}", end_date)){stop('end_date must have format "YYYY-MM-DD"')}
+
+ url <- "https://www.fitbit.com/ajaxapi"
+ request <- paste0('{"template":"/ajaxTemplate.jsp","serviceCalls":[{"name":"activityTileData","args":{"dateFrom":"',
+ start_date,
+ '","dateTo":"',
+ end_date,
+ '"},"method":"getSleepTileData"}]}'
+ )
+
+ csrfToken <- stringr::str_extract(cookie,
+ "[A-Z0-9]{8}\\-[A-Z0-9]{4}\\-[A-Z0-9]{4}\\-[A-Z0-9]{4}\\-[0-9A-Z]{12}")
+ body <- list(request=request, csrfToken = csrfToken)
+ response <- httr::POST(url, body=body, httr::config(cookie=cookie))
+
+ dat_string <- methods::as(response, "character")
+ dat_list <- jsonlite::fromJSON(dat_string)
+
+ if("hasLoggedSleep" %in% names(dat_list)){
+ summary <- list(avgSleepDuration = dat_list$avgSleepDuration,
+ avgSleepTime = dat_list$avgSleepTime,
+ avgSleepScore = dat_list$avgSleepScore,
+ avgGraphicPercent = dat_list$avgGraphicPercent)
+ # get individual day data
+ df <- dat_list[["entries"]]
+ }else{
+ stop("No sleep data available")
+ }
+ return(list(summary=summary, df=df))
+}
+
diff --git a/R/get_weight_data.R b/R/get_weight_data.R
new file mode 100644
index 0000000..dcf2fc4
--- /dev/null
+++ b/R/get_weight_data.R
@@ -0,0 +1,39 @@
+#' Get weight data from fitbit.com
+#'
+#' Get weight data from fitbit using cookie returned from login function
+#' @param cookie Cookie returned after login, specifically the "u" cookie
+#' @param start_date Date in YYYY-MM-DD format
+#' @param end_date Date in YYYY-MM-DD format
+#' @keywords data
+#' @export
+#' @return A dataframe with two columns:
+#' \item{time}{A POSIXct time value}
+#' \item{weight}{The data column corresponding to weight}
+#' @examples
+#' \dontrun{
+#' get_weight_data(cookie, start_date="2015-01-13", end_date="2015-01-20")
+#' }
+#' get_weight_data
+get_weight_data <- function(cookie, start_date, end_date){
+ if(!is.character(cookie)){stop("cookie must be a character string")}
+ if(!is.character(start_date)){stop("start_date must be a character string")}
+ if(!is.character(end_date)){stop("end_date must be a character string")}
+ if(!grepl("[0-9]{4}-[0-9]{2}-[0-9]{2}", start_date)){stop('start_date must have format "YYYY-MM-DD"')}
+ if(!grepl("[0-9]{4}-[0-9]{2}-[0-9]{2}", end_date)){stop('end_date must have format "YYYY-MM-DD"')}
+
+ url <- "https://www.fitbit.com/graph/getNewGraphData"
+ query <- list("type" = "weight",
+ "dateFrom" = start_date,
+ "dateTo" = end_date)
+
+ response <- httr::GET(url, query=query, httr::config(cookie=cookie))
+
+ dat_string <- methods::as(response, "character")
+ dat_list <- jsonlite::fromJSON(dat_string)
+ df <- dat_list[["graph"]][["dataSets"]][["weight"]][["dataPoints"]]
+ names(df)[1:2] <- c("time", "weight")
+ tz <- Sys.timezone()
+ if(is.null(tz)){tz <- format(Sys.time(),"%Z")}
+ df$time <- as.POSIXct(df$time, "%Y-%m-%d %H:%M:%S", tz=tz)
+ return(df)
+}
diff --git a/R/login.R b/R/login.R
new file mode 100644
index 0000000..36e7d76
--- /dev/null
+++ b/R/login.R
@@ -0,0 +1,47 @@
+#' Login to fitbit.com and get cookie
+#'
+#' Get the login cookie after login at www.fitbit.com
+#' @param email Email address used to login to fitbit.com
+#' @param password Password used to login to fitbit.com
+#' @param rememberMe Value for rememberMe during login, default is FALSE, but changing to TRUE may help with login issues
+#' @keywords login
+#' @export
+#' @return A string containing the cookie that is returned after login at www.fitbit.com
+#' @examples
+#' \dontrun{
+#' cookie <- login(email="corynissen<at>gmail.com", password="mypasswordhere")
+#' }
+#' login
+login <- function(email, password, rememberMe=FALSE){
+ if(!is.character(email)){stop("email must be a character string")}
+ if(!is.character(password)){stop("password must be a character string")}
+
+ rememberMe <- ifelse(rememberMe, "true", "false")
+
+ url <- "https://www.fitbit.com/login"
+ headers <- list("Host" = "www.fitbit.com",
+ "Connection" = "keep-alive",
+ "Content-Length" = "278",
+ "Cache-Control" = "max-age=0",
+ "Accept" = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
+ "Origin" = "https://www.fitbit.com",
+ "User-Agent" = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36",
+ "Content-Type" = "application/x-www-form-urlencoded",
+ "Referer" = "https://www.fitbit.com/login",
+ "Accept-Encoding" = "gzip, deflate",
+ "Accept-Language" = "en-US,en;q=0.8")
+ body <- list("email"=email, "password"=password, "rememberMe"=rememberMe,
+ "login"="Log In")
+
+ a <- httr::POST(url, headers=headers, body=body)
+ cookie <- a$cookies$u
+ if(is.null(cookie)){
+ all_cookies <- a$cookies
+ cookie <- all_cookies[grep("^u$", all_cookies$name, ignore.case=F),"value"]
+ if(is.null(cookie)){
+ stop("login failed")
+ }
+ }
+
+ return(cookie)
+}
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..5ada605
--- /dev/null
+++ b/README.md
@@ -0,0 +1,32 @@
+
+### fitbitScraper 0.1.6
+
+This package scrapes data from fitbit.com
+It only works if you use email / password to login. Not sure about facebook or google login.
+
+Usage:
+
+```R
+install.packages("fitbitScraper")
+library("fitbitScraper")
+
+cookie <- login(email="corynissen at gmail.com", password="mypassword")
+# 15_min_data "what" options: "steps", "distance", "floors", "active-minutes", "calories-burned"
+df <- get_activity_data(cookie, what="steps", date="2015-01-21")
+library("ggplot2")
+ggplot(df) + geom_bar(aes(x=time, y=data, fill=data), stat="identity") +
+ xlab("") +ylab("steps") +
+ theme(axis.ticks.x=element_blank(),
+ panel.grid.major.x = element_blank(),
+ panel.grid.minor.x = element_blank(),
+ panel.grid.minor.y = element_blank(),
+ panel.background=element_blank(),
+ panel.grid.major.y=element_line(colour="gray", size=.1),
+ legend.position="none")
+
+# daily_data "what" options: "steps", "distance", "floors", "minutesVery", "caloriesBurnedVsIntake"
+df <- get_daily_data(cookie, what="steps", start_date="2015-01-13", end_date="2015-01-20")
+ggplot(df) + geom_point(aes(x=time, y=data))
+```
+### Changed function
+- get_activity_data() - end_date now works. Start_date removed.
diff --git a/man/get_activity_data.Rd b/man/get_activity_data.Rd
new file mode 100644
index 0000000..9e30cac
--- /dev/null
+++ b/man/get_activity_data.Rd
@@ -0,0 +1,37 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/get_activity_data.R
+\name{get_activity_data}
+\alias{get_activity_data}
+\title{Get activity data from fitbit.com}
+\usage{
+get_activity_data(cookie, end_date)
+}
+\arguments{
+\item{cookie}{Cookie returned after login, specifically the "u" cookie}
+
+\item{end_date}{Date in YYYY-MM-DD format}
+}
+\value{
+A dataframe with thirteen columns:
+ \item{id}{The fitbit ID of the activity}
+ \item{name}{Name for the type of activity}
+ \item{date}{Date of the activity}
+ \item{start_time}{Start time for the activity}
+ \item{distance}{Distance travelled during the activity}
+ \item{duration}{Duration in hours:minutes:seconds of the activity}
+ \item{calories}{Calories burned during the activity}
+ \item{steps}{Steps taken the activity}
+ \item{start_datetime}{A POSIXct encoded start time for the activity}
+ \item{end_datetime}{A POSIXct encoded end time for the activity}
+}
+\description{
+Get activity data from fitbit using cookie returned from login function
+}
+\examples{
+\dontrun{
+get_activity_data(cookie, end_date="2015-01-20")
+}
+get_activity_data
+}
+\keyword{data}
+
diff --git a/man/get_daily_data.Rd b/man/get_daily_data.Rd
new file mode 100644
index 0000000..bfcbbd5
--- /dev/null
+++ b/man/get_daily_data.Rd
@@ -0,0 +1,33 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/get_daily_data.R
+\name{get_daily_data}
+\alias{get_daily_data}
+\title{Get daily data from fitbit.com}
+\usage{
+get_daily_data(cookie, what = "steps", start_date, end_date)
+}
+\arguments{
+\item{cookie}{Cookie returned after login, specifically the "u" cookie}
+
+\item{what}{What data you wish to be returned. Options include "steps", "distance", "floors", "minutesVery", "caloriesBurnedVsIntake", "getTimeInHeartRateZonesPerDay", "getRestingHeartRateData"}
+
+\item{start_date}{Date in YYYY-MM-DD format}
+
+\item{end_date}{Date in YYYY-MM-DD format}
+}
+\value{
+A dataframe with two columns:
+ \item{time}{A POSIXct time value}
+ \item{data}{The data column corresponding to the choice of "what"}
+}
+\description{
+Get daily data from fitbit using cookie returned from login function
+}
+\examples{
+\dontrun{
+get_daily_data(cookie, what="steps", start_date="2015-01-13", end_date="2015-01-20")
+}
+get_daily_data
+}
+\keyword{data}
+
diff --git a/man/get_intraday_data.Rd b/man/get_intraday_data.Rd
new file mode 100644
index 0000000..a521104
--- /dev/null
+++ b/man/get_intraday_data.Rd
@@ -0,0 +1,31 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/get_intraday_data.R
+\name{get_intraday_data}
+\alias{get_intraday_data}
+\title{Get intraday data from fitbit.com}
+\usage{
+get_intraday_data(cookie, what = "steps", date)
+}
+\arguments{
+\item{cookie}{Cookie returned after login, specifically the "u" cookie}
+
+\item{what}{What data you wish to be returned. Options include "steps", "distance", "floors", "active-minutes", "calories-burned", "heart-rate"}
+
+\item{date}{Date in YYYY-MM-DD format}
+}
+\value{
+A dataframe with two columns:
+ \item{time}{A POSIXct time value}
+ \item{data}{The data column corresponding to the choice of "what"}
+}
+\description{
+Get intraday data from fitbit using cookie returned from login function
+}
+\examples{
+\dontrun{
+get_intraday_data(cookie, what="steps", date="2015-01-20")
+}
+get_intraday_data
+}
+\keyword{data}
+
diff --git a/man/get_premium_export.Rd b/man/get_premium_export.Rd
new file mode 100644
index 0000000..164d920
--- /dev/null
+++ b/man/get_premium_export.Rd
@@ -0,0 +1,34 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/get_premium_export.R
+\name{get_premium_export}
+\alias{get_premium_export}
+\title{Get official data export from fitbit.com premium}
+\usage{
+get_premium_export(cookie, what = "ACTIVITIES", start_date = "2015-01-13",
+ end_date = "2015-01-20")
+}
+\arguments{
+\item{cookie}{Cookie returned after login, specifically the "u" cookie}
+
+\item{what}{What data you wish to be returned. Options include "BODY", "FOODS", "ACTIVITIES", "SLEEP"}
+
+\item{start_date}{Date in YYYY-MM-DD format}
+
+\item{end_date}{Date in YYYY-MM-DD format}
+}
+\value{
+A list with two things
+ \item{summary}{A list of sleep summary values}
+ \item{df}{A data frame containing various sleep values over time}
+}
+\description{
+Get official data export from fitbit premium using cookie returned from login function. This should be used over individual calls to get_daily_data(), etc. if you subscribe to premium and data export is allowed. I'm not subscribed to premium, but it works for me...
+}
+\examples{
+\dontrun{
+get_premium_export(cookie, what="ACTIVITIES", start_date="2015-01-13", end_date="2015-01-20")
+}
+get_premium_export
+}
+\keyword{data}
+
diff --git a/man/get_sleep_data.Rd b/man/get_sleep_data.Rd
new file mode 100644
index 0000000..481d928
--- /dev/null
+++ b/man/get_sleep_data.Rd
@@ -0,0 +1,31 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/get_sleep_data.R
+\name{get_sleep_data}
+\alias{get_sleep_data}
+\title{Get sleep data from fitbit.com}
+\usage{
+get_sleep_data(cookie, start_date = "2015-01-13", end_date = "2015-01-20")
+}
+\arguments{
+\item{cookie}{Cookie returned after login, specifically the "u" cookie}
+
+\item{start_date}{Date in YYYY-MM-DD format}
+
+\item{end_date}{Date in YYYY-MM-DD format}
+}
+\value{
+A list with two things
+ \item{summary}{A list of sleep summary values}
+ \item{df}{A data frame containing various sleep values over time}
+}
+\description{
+Get sleep data from fitbit using cookie returned from login function
+}
+\examples{
+\dontrun{
+get_sleep_data(cookie, start_date="2015-01-13", end_date="2015-01-20")
+}
+get_sleep_data
+}
+\keyword{data}
+
diff --git a/man/get_weight_data.Rd b/man/get_weight_data.Rd
new file mode 100644
index 0000000..482948e
--- /dev/null
+++ b/man/get_weight_data.Rd
@@ -0,0 +1,31 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/get_weight_data.R
+\name{get_weight_data}
+\alias{get_weight_data}
+\title{Get weight data from fitbit.com}
+\usage{
+get_weight_data(cookie, start_date, end_date)
+}
+\arguments{
+\item{cookie}{Cookie returned after login, specifically the "u" cookie}
+
+\item{start_date}{Date in YYYY-MM-DD format}
+
+\item{end_date}{Date in YYYY-MM-DD format}
+}
+\value{
+A dataframe with two columns:
+ \item{time}{A POSIXct time value}
+ \item{weight}{The data column corresponding to weight}
+}
+\description{
+Get weight data from fitbit using cookie returned from login function
+}
+\examples{
+\dontrun{
+get_weight_data(cookie, start_date="2015-01-13", end_date="2015-01-20")
+}
+get_weight_data
+}
+\keyword{data}
+
diff --git a/man/login.Rd b/man/login.Rd
new file mode 100644
index 0000000..0278208
--- /dev/null
+++ b/man/login.Rd
@@ -0,0 +1,29 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/login.R
+\name{login}
+\alias{login}
+\title{Login to fitbit.com and get cookie}
+\usage{
+login(email, password, rememberMe = FALSE)
+}
+\arguments{
+\item{email}{Email address used to login to fitbit.com}
+
+\item{password}{Password used to login to fitbit.com}
+
+\item{rememberMe}{Value for rememberMe during login, default is FALSE, but changing to TRUE may help with login issues}
+}
+\value{
+A string containing the cookie that is returned after login at www.fitbit.com
+}
+\description{
+Get the login cookie after login at www.fitbit.com
+}
+\examples{
+\dontrun{
+cookie <- login(email="corynissen<at>gmail.com", password="mypasswordhere")
+}
+login
+}
+\keyword{login}
+
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/r-cran-fitbitscraper.git
More information about the debian-med-commit
mailing list