Skip to content Skip to sidebar Skip to footer

Check Whether A Website Provides Photo Or Video Based On A Pattern In Its Url

I am wondering how I can figure out if a website provide photo or video by checking its URL. I investigated the website that I am interested in and found that most of the links I h

Solution 1:

So I have a rather simple solution for this.

Inspecting the URLs provided by the OP (e.g., https://www.pixilink.com/93313) indicates that the #mode= default value is provided by the variable initial_mode = in an embedded javascript. So to establish whether a URL will default to "picture" (#mode=0) or video (#mode=tour) can be accomplished by investigating the value assigned to this variable.

#Function to get the value of initial_mode from the URL
urlmode <- function(x){
  mycontent <- readLines(x)
  mypos <- grep("initial_mode = ", mycontent)
  
  if(grepl("0", mycontent[mypos])){
    cat("\n", x, "has default initial_mode picture: #mode=0 \n")
    return("picture")
  } elseif(grepl("tour", mycontent[mypos])){
    cat("\n", x, "has default initial_mode video: #mode=tour \n")
    return("video")
  } else{
    cat("\n", x, "is an invalid URL. \n")
    return("invalid")
  }
}


#Example URLs to demonstrate functionality
myurl1 <- "https://www.pixilink.com/93313"
myurl2 <- "https://www.pixilink.com/69964"


urlmode(myurl1)
## https://www.pixilink.com/93313 has default initial_mode picture: #mode=0 #[1] "picture"#Warning message:#In readLines(x) :#  incomplete final line found on 'https://www.pixilink.com/93313'#

urlmode(myurl2)
## https://www.pixilink.com/69964 has default initial_mode video: #mode=tour #[1] "video"#Warning message:#In readLines(x) :#  incomplete final line found on 'https://www.pixilink.com/69964'

Needless to say this is an extremely simplistic function that will (most likely) fail all but the ideal (sub)set of cases. But it's a start.

Post a Comment for "Check Whether A Website Provides Photo Or Video Based On A Pattern In Its Url"