unshorten url in R code example

Example 1: unshorten url in R

decode_short_url <- function(url, ...) {
  # PACKAGES #
  require(RCurl)
 
  # LOCAL FUNCTIONS #
  decode <- function(u) {
    Sys.sleep(0.5)
    x <- try( getURL(u, header = TRUE, nobody = TRUE, followlocation = FALSE, cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")) )
    if(inherits(x, 'try-error') | length(grep(".*Location: (\\S+).*", x))<1) {
      return(u)
    } else {
      return(gsub('.*Location: (\\S+).*', '\\1', x))
    }
  }
 
  # MAIN #
  gc()
  # return decoded URLs
  urls <- c(url, ...)
  l <- vector(mode = "list", length = length(urls))
  l <- lapply(urls, decode)
  names(l) <- urls
  return(l)
}

Example 2: unshorten url in R

library("knitr")
library(urlshorteneR)

Example 3: unshorten url in R

if (interactive()) {

bitly_retrieve_group(ui$default_group_guid)
bitly_retrieve_groups()
}

Example 4: unshorten url in R

if (interactive()) {
bitly_update_user(name = "John Malc", showRequestURL = TRUE)
}

Example 5: unshorten url in R

## In order to use bitly functions, you first need to authenticate. 
##                         For that execute 'bitly_auth()' in R console.

Example 6: unshorten url in R

if (interactive()) {
bitly_app_details()
}

Example 7: unshorten url in R

if(interactive()) {
  opts_knit$set(root.dir = "~/Documents/Documents2/R-package-urlshortener")
  bitly_token <- readRDS("tests/bitly_local_token.rds")

# You can register a new pair of keys yourself - but you can also use mine - an option which is also by default
# bitly_token <- bitly_auth(key = "be03aead58f23bc1aee6e1d7b7a1d99d62f0ede8", secret = "f9c6a3b18968e991e35f466e90c7d883cc176073")
# bitly_token <- bitly_auth()

  ui <- bitly_user_info(showRequestURL = TRUE)
  is_bitly_user_premium_holder()
}

Tags:

Misc Example