Tapping the FourSquare Trending Venues API with R

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

About these ads

6 thoughts on “Tapping the FourSquare Trending Venues API with R

  1. I don’t know what goes wrong. I get an error with the connection when I call the function:

    Error in function (type, msg, asError = TRUE) :
    SSL certificate problem, verify that the CA cert is OK. Details:
    error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

  2. Ok. It appears to be a problem with RCurl package over Windows. I patched the function with this lines:

    download.file(url=”http://curl.haxx.se/ca/cacert.pem”, destfile=”cacert.pem”)
    u<-getURL(w, cainfo = "cacert.pem")

    One warning: it seems that there are geographic limitations to the API call. I couldn't manage to retrieve venues from a european small city.

  3. I {{leave|drop|write} a comment|drop a comment|leave a response} whenever I appreciate a article on a {site|blog|site|website} or I have something to contribute to the conversation. Usually it’s a result of the fire communicated in the article I browsed. And after this post educational piece. I {was moved|was actually excited} enough to {drop|{leave|drop|create}|post} a {thought|{comment|commenta response}} :-) I do have {some questions|a couple of questions|2 questions} for you {if you don’t mind|if it’s okay}. Could it be just me or {do|does it look like} a few of the comments look as if they are left by brain dead visitors? :-P And, if you are writing on other places, I’d like to follow {you|everything new you have to post}. Could you list all of all your community pages like your twitter feed, Facebook page or linkedin profile?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s