diff --git a/DESCRIPTION b/DESCRIPTION index 3fe5c63..6e5e10e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -7,8 +7,10 @@ Authors@R: person("Brandon", "Greenwell", email = "greenwell.brandon@gmail.com", Description: An R wrapper to the overly complicated Bitmoji API. License: GPL (>= 2) Imports: + getPass, graphics, grDevices, + httr, jsonlite, magick Encoding: UTF-8 diff --git a/NAMESPACE b/NAMESPACE index 609b9eb..d75caec 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,4 +3,5 @@ export(get_comic) export(get_comic_id) export(get_comic_ids) +export(get_id) export(plot_comic) diff --git a/R/utils.R b/R/utils.R index ad9a40d..2896902 100644 --- a/R/utils.R +++ b/R/utils.R @@ -78,3 +78,94 @@ plot_comic <- function(id, tag) { par(mar = c(0, 0, 0, 0) + 0.1) plot(as.raster(img)) } + +#' Get a User Id +#' +#' Get an \code{id} for consumption by \code{\link{plot_comic}} and \code{\link{get_comic}} +#' +#' @param user_email Character string specifying the email associated with the Bitmoji account +#' for which you'd like to get/plot comics (e.g., \code{username@@gmail.com}). +#' +#' @export +#' +#' @examples +#' \dontrun{ +#' id <- get_id("username@@gmail.com") +#' plot_comic(paste0(id,"-v1"), tag = "edvard") +#' } +get_id <- function(user_email) { + # attempt to login -------------------------------------------------------- + login_url = 'https://api.bitmoji.com/user/login' + login_response <- httr::POST(url = login_url + , httr::add_headers( + Accept = "application/json" + , "Accept-Encoding" = "gzip, deflate, br" + , "Accept-Language" = "en-US,en;q=0.9" + , Connection = "keep-alive" + , "Content-Type" = "application/x-www-form-urlencoded" + , Host = "api.bitmoji.com" + , Origin = "https://www.bitmoji.com" + , Referer = "https://www.bitmoji.com/account_v2/" + ) + , body = list( + client_id = "imoji" + , username = user_email + , password = getPass::getPass(msg = "PASS") + , grant_type = "password" + , client_secret = "secret" + ) + , encode = "form" + ) + + # if not a successful login, stop with most descriptive message available + if(login_response$status_code != 200) { + if("message" %in% names(httr::content(login_response))) { + stop(httr::content(login_response)$message) + } else { + stop(httr::content(login_response)$error) + } + } + + # extract token if successful login + token <- httr::content(login_response)$access_token + + # fetch id ---------------------------------------------------------------------- + avatar_url <- "https://api.bitmoji.com/user/avatar" + avatar_response <- httr::GET(url = avatar_url + , httr::add_headers( + "Accept-Encoding" = "gzip, deflate, br" + , "Accept-Language" = "en-US,en;q=0.9" + , "bitmoji-token" = token + , Connection = "keep-alive" + , "Content-Type" = "application/x-www-form-urlencoded" + , Host = "api.bitmoji.com" + , Origin = "https://www.bitmoji.com" + , Referer = "https://www.bitmoji.com/account_v2/" + ) + ) + # stop if request fails + if(avatar_response$status_code != 200) { + stop(httr::content(login_response)$error) + } + # extract id if successful avatar_response + uuid <- httr::content(avatar_response)$avatar_version_uuid + + # attempt to logout ------------------------------------------------------- + logout_url <- "https://api.bitmoji.com/user/logout" + logout_response <- httr::POST(url = logout_url + , httr::add_headers( + Accept = "application/json" + , "Accept-Encoding" = "gzip, deflate, br" + , "Accept-Language" = "en-US,en;q=0.9" + , "bitmoji-token" = token + , Connection = "keep-alive" + , "Content-Type" = "application/x-www-form-urlencoded" + , Host = "api.bitmoji.com" + , Origin = "https://www.bitmoji.com" + , Referer = "https://www.bitmoji.com/account_v2/" + ) + ) + + # return response --------------------------------------------------------- + return(uuid) +} diff --git a/man/get_id.Rd b/man/get_id.Rd new file mode 100644 index 0000000..2babd70 --- /dev/null +++ b/man/get_id.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{get_id} +\alias{get_id} +\title{Get a User Id} +\usage{ +get_id(user_email) +} +\arguments{ +\item{user_email}{Character string specifying the email associated with the Bitmoji account +for which you'd like to get/plot comics (e.g., \code{username@gmail.com}).} +} +\description{ +Get an \code{id} for consumption by \code{\link{plot_comic}} and \code{\link{get_comic}} +} +\examples{ +\dontrun{ +id <- get_id("username@gmail.com") +plot_comic(paste0(id,"-v1"), tag = "edvard") +} +}