Skip to content

Instantly share code, notes, and snippets.

@sillasgonzaga
Created October 14, 2017 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sillasgonzaga/2e92f880811f460c7edd0a622563a17a to your computer and use it in GitHub Desktop.
Save sillasgonzaga/2e92f880811f460c7edd0a622563a17a to your computer and use it in GitHub Desktop.
# source: https://gist.github.com/corynissen/5389426
getLongURL.curl <- function(shorturl){
# uses curl statement to get expanded url from t.co links (or any link)
# loop through until there's no location attribute... that's the long link.
newurl <- shorturl
url <- ""
while(url != newurl){
data <- system(paste0("curl -I ", newurl), intern=T)
if(sum(grepl("location: ", tolower(data))) == 0){
url <- newurl
}else{
data <- subset(data, tolower(substring(data, 1, 9))=="location:")
stringurl <- substring(data[1], 11, nchar(data[1])-1)
# sometimes the location data isn't a url.
if(substring(stringurl, 1, 4)=="http"){
newurl <- stringurl
}else{
url <- newurl
}
}
}
return(newurl)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment