-
Notifications
You must be signed in to change notification settings - Fork 1
/
diffbot.R
executable file
·46 lines (34 loc) · 1.26 KB
/
diffbot.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#' This function will return the API Call results from diffbot
#' See RunDemo for Usage Example
#'Input Variables:
#' @param url : Article URL to process (URL encoded)
#' @param token : Developer token
#' @param api : e.g. 'analyze'
#' @param version : 2
#' @paramfields : Used to control which fields are returned by the API see Options below example:
#' fields <- c('meta', 'html', 'tags')
#' @Author Simon Müller, [email protected]
#'Output Argument:
#' @return data : a R list
diffbot <- function(url, token, api, fields=NULL, version=2) {
# load R libraries
library(RCurl)
library(RJSONIO)
# frontpage has a different api url
if (api == "frontpage") {
api_url <- 'http://www.diffbot.com/api/'
} else {
api_url <- 'http://api.diffbot.com/'
}
# request urls dependent on fields
if (is.null(fields)) {
request <- paste0(api_url, paste0("v", version, "/"), api, '?token=', token, "&url=", url)
} else {
request <- paste0(api_url, paste0("v", version, "/"), api, '?token=', token, "&url=", url, "&fields=", paste(fields, collapse=","))
}
# get data
response <- getURL(request)
# json to R list
data <- fromJSON(response)
return(data)
}