Site icon R-bloggers

Tapping the FourSquare Trending Venues API with R

[This article was first published on NERD PROJECT » R project posts, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

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 their blog: NERD PROJECT » R project posts.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.