Tapping the FourSquare Trending Venues API with R

March 4, 2013
By

(This article was first published on NERD PROJECT » R project posts, and kindly contributed to R-bloggers)

I came up with the following function to tap into the FourSquare trending venues API:

library("RCurl", "RJSONIO")
 
foursquare<-function(x,y,z){
    w<-paste("https://api.foursquare.com/v2/venues/trending?ll=",x,"&radius=2000&oauth_token=",y,"&v=",z,sep="")
    u<-getURL(w)
    test<-fromJSON(u)
    locationname=""
    lat=""
    long=""
    zip=""
    herenowcount=""
    likes=""
    for(n in 1:length(test$response$venues)) {
        locationname[n] = test$response$venues[[n]]$name
        lat[n] = test$response$venues[[n]]$location$lat
        long[n] = test$response$venues[[n]]$location$lng
        zip[n] = test$response$venues[[n]]$location$postalCode
        herenowcount[n]<-test$response$venues[[n]]$hereNow$count
        likes[n]<-test$response$venues[[n]]$likes$count
        xb<-as.data.frame(cbind(locationname, lat, long, zip, herenowcount, likes))
    }
    xb$pulled=date()
    return(xb)
}

where x=”lat,long”, y=oAuth_token, and z=date. You can find out your oAuth_token by signing into FourSquare and going to https://developer.foursquare.com/docs/venues/trending, click on the “try it out” button, then copy and the code that would be where the deleted box is.Screen Shot 2013-03-04 at 8.44.41 PM

an example:

philly<-foursquare("39.9572,-75.1691","XXXXDSAFAEWRFAEFRAAFDASDFASFD","20130304")

or you can scrape by running in a repeat function.

QED


To leave a comment for the author, please follow the link and comment on his blog: NERD PROJECT » R project posts.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.