Btibert3
2/8/2015 - 9:07 PM

A super basic function to play around with the Prismatic Interest Graph

A super basic function to play around with the Prismatic Interest Graph

prizAPI = function(TOKEN=NA, URL=NA) {
  library(httr)
  ## set the endpoint
  EP = "http://interest-graph.getprismatic.com/url/topic"
  ## make sure the token exsists
  if (is.na(TOKEN)) {
    message("You must supply a valid token")
  }
  ## make sure URL exists
  if (is.na(URL)) {
    message("You must supply a valid URL in the form of http://....")
  }
  ## build the url
  PAGE = paste(EP,
               "?api-token=",
               TOKEN,
               "&url=",
               RCurl::curlEscape(URL),
               sep = "")
  ## get the data
  resp = httr::GET(PAGE)
  if (resp$status_code != 200) {
    df = data.frame()
  } else if (length(content(resp)[[1]]) == 0) {
    ## checks if no topics are returned by the API
    df = data.frame()
  } else {
    ## parse the data into a nested list
    dat = content(resp)
    ## parse into a dataframe
    df = do.call(rbind, lapply(dat$topics, function(x) data.frame(topic_id = x$id,
                                                                  topic = x$topic,
                                                                  score = x$score,
                                                                  stringsAsFactors=F)))
  }
  return(df)
}

Basic R Wrapper for Prismatic's Interest Graph API

By no means is this a perfect solution, in fact, it's nothing more than of a hacky function to just play around with the Prismatic Interest Graph API.

You can read about the introductory blog post here which describes the motivation and process of developing the backend.

The function

Like I said, very crude way of getting data. Right now, I make the assumption that you want to feed a URL to the function.

TOKEN = your API token

URL = the webpage you want to the API to tag

and to get data

URL = "https://medium.com/@garyvee/its-not-just-about-working-harder-you-also-need-to-work-faster-3389465794c1"
TOKEN = "YOUR_TOKEN_HERE"
dat = prizAPI(TOKEN, URL)

That's it. The function should return a dataframe, but as you can see, I am only doing some basic checks, by no means complete.

Development

Interestingly enough, I couldn't get httr or requests from python to get a valid response. Without a doubt, I am no doubt it was because of my lack of knowledge surrounding http requests, but I am not sure why the commands below didn't work.

EP = "http://interest-graph.getprismatic.com/url/topic"
TOKEN = "TOKEN"
URL = "https://medium.com/@garyvee/its-not-just-about-working-harder-you-also-need-to-work-faster-3389465794c1"
URL = RCurl::curlEscape(URL)
httr::POST(EP, add_headers("X-API-TOKEN" = TOKEN), body = list(url = URL), verbose())

Perhaps it's something extremely obvious, but less so for this guy. I looked at Chrome Developer's tools and a valid response to the basic form, and to the best of my knowledge, I was passing the ncessary data. That valid request was a POST with form values. Above, I passed the TOKEN in the header (I believe), and then passed a value of the encoded URL which I set to url.

On the other hand, my function uses GET, but I basically just manually build the URL and ask for the response. It works, but the approach is far from elegant and most certainly has bugs.

Usage

Here is a quick example that works for now

URL = "http://www.nhl.com/gamecenter/en/preview?id=2014020791&navid=nhl:topheads"
TOKEN = "TOKEN"
dat = prizAPI(TOKEN, URL)
str(dat)
dat

Which yields

> str(dat)
'data.frame':   7 obs. of  3 variables:
 $ topic_id: int  590 2915 3127 3166 2914 1950 3093
 $ topic   : Factor w/ 7 levels "Boston Bruins",..: 1 2 3 4 5 6 7
 $ score   : num  0.769 0.702 0.604 0.592 0.581 ...
> dat
  topic_id              topic     score
1      590      Boston Bruins 0.7689137
2     2915 Montreal Canadiens 0.7019416
3     3127 New York Islanders 0.6042980
4     3166                NHL 0.5916980
5     2914           Montreal 0.5805778
6     1950        Hockey, Ice 0.5467193
7     3093  New Jersey Devils 0.5463491

Considering the link above is an "overview" page for tonight's game between the Boston Bruins and the Montreal Canadiens, the topics make complete sense, as the Bruins played the Islanders in the previous game.