Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decode tinyurl in R to get full url path?

Tags:

url

r

Is there a way to decode tinyURL links in R so that I can see which web pages they actually refer to?

like image 518
Grey Peak Avatar asked Mar 23 '10 14:03

Grey Peak


1 Answers

Below is a quick and dirty solution, but should get the job done:

library(RCurl)

decode.short.url <- function(u) {
  x <- try( getURL(u, header = TRUE, nobody = TRUE, followlocation = FALSE) )
  if(class(x) == 'try-error') {
    return(u)
  } else {
    x <- strsplit(x, "Location: ")[[1]][2]
    return(strsplit(x, "\r")[[1]][1])
  }
}

The variable 'u' below contains one shortend url, and one regular url.

u <- c("http://tinyurl.com/adcd", "http://www.google.com") 

You can then get the expanded results by doing the following.

 sapply(u, decode.short.url) 

The above should work for most services which shorten the URL, not just tinyURL. I think.

HTH

Tony Breyal

like image 75
Tony Breyal Avatar answered Sep 22 '22 12:09

Tony Breyal