Unknown error on Facebook API through R. - r

I'm trying to download all the posts from a facebook page through RFacebook, but when the page has an high number of posts (over 400 or so), the script stops, returning the error
"Error in callAPI(url = url, token = token) : An unknown error has occurred." at the line where I call the getPage.
library(Rfacebook)
library(stringr)
load("fb_oauth")
token=fb_oauth
page<-getPage("bicocca", token, n = 100000, since = NULL, until = NULL, feed = TRUE)
noSpaceMsg<-str_replace_all(page$message, "[\r\n]" , "")
output<-as.data.frame(cbind(page$from_name,page$id, noSpaceMsg, page$created_time, page$type, page$link, page$likes_count, page$comments_count, page$shares_count))
colnames(output)<-c("username","msgid", "message", "created_time", "type", "link", "likes", "comments", "shares")
write.csv(output, "bicocca.csv", row.names=FALSE)
Where is the problem? How can I fix it?

It seems to be a problem with the API, not with the R package. When I try to do the query in the Graph API Explorer here, I get an error too. No idea why.
One way around this is to query month by month, wrapping the getPage function in a try command:
page <- 'bicocca'
dates <- seq(as.Date("2010/10/01"), as.Date("2015/04/20"), by="month")
n <- length(dates)-1
df <- list()
for (i in 1:n){
cat(as.character(dates[i]), " ")
try(df[[i]] <- getPage(page, token, since=dates[i], until=dates[i+1]))
cat("\n")
}
df <- do.call(rbind, df)
This will not give you all the posts, but probably most of them.

Related

getting an error using tuber to extract all comments from a vector of youtube videos id map_df()

I'm trying to extract all comments from a list of youtube videos using the tuber package
I can extract comments from a single video ID using the following code:
library(tuber)
client_id <- [my_client_id]
client_secret <- [my_client_secret]
yt_oauth(app_id = client_id, app_secret = client_secret, token = "")
video_id <- "arjHXHHQkQs"
test_comments <- get_all_comments(video_id = video_id)
I have a vector of 29 youtube IDs and I'm trying to use the map_df() function to iterate over every ID in the vector and apply the function get_all_comments() but I keep getting an error:
id <- c("C5OLDKq_CfI", "Y26MWDh8u3Y", "0HQyjY8I830", "AGBX-AHKDfk" ,"YuA59DKabVs")
comments_getter <- function(id) {
tuber::get_all_comments(video_id = id)}
comments_raw <- purrr::map(.x = id, .f = comments_getter)
Error: HTTP failure: 401
Called from: tuber_check(req)
And from the debug viewer I get this
function (req)
{
if (req$status_code < 400)
return(invisible())
stop("HTTP failure: ", req$status_code, "\n", call. = FALSE)
}
Is something related to a limit for the API or is there an error in my code?

using GET in a loop

I am using the following code. I create a list of first names and then generate links to an API for each name and then try to capture the data from each link.
mydata$NameGenderURL2 <- paste ("https://gender-api.com/get?name=",mydata$firstname, "&key=suZrzhrNJRvrkWFXAG", sep="")
mynamegenderfunction <- function(x){
GET(url= mydata$NameGenderURL2[x])
this.raw.content <- genderdata$content
this.raw.content <- rawToChar(genderdata$content)
this.content <- fromJSON(this.raw.content)
name1[x] <- this.content$name
gender1[x] <- this.content$gender}
namelist <- mydata$firstname[1:100]
genderdata <- lapply(namelist, mynamegenderfunction)
Oddly enough I receive the following message:
>Error in curl::curl_fetch_memory(url, handle = handle) :
>Could not resolve host: NA`
I tried another API and got the same issue. Any suggestions?
Here is a data sample:
namesurl
https://api.genderize.io/?name=kaan
https://api.genderize.io/?name=Joan
https://api.genderize.io/?name=homeblitz
https://api.genderize.io/?name=Flatmax
https://api.genderize.io/?name=BRYAN
https://api.genderize.io/?name=James
https://api.genderize.io/?name=Dion
https://api.genderize.io/?name=Flintu
https://api.genderize.io/?name=Adriana
The output that I need is the gender for each link, which would be :Male/Female, Null

Not Found (HTTP 404) twitteR loop error

I am aiming to build a twitter network by constructing an igraph from twitter relationships.
After 10 iterations R gives me this error
Error in twInterfaceObj$doAPICall(paste("users", "show", sep = "/"), params = params, :
Not Found (HTTP 404).
# Grab latest tweets
tweets_galway <- searchTwitter('#galway', n=100)
# make into df
df <- do.call("rbind", lapply(tweets_galway, as.data.frame))
# extract users from galway hashtag tweets
galway_users = df$screenName
connectiondf=data.frame()
for( i in 1:100)
{
name<-getUser(galway_users[i])
following<-name$getFriends()
following <- twListToDF(following)
connect1=cbind(follower=galway_users[i],following=following$screenName)
follower<-name$getFollowers()
follower <- twListToDF(follower)
connect2=cbind(follower=follower$screenName,following=galway_users[i])
connection<-rbind(connect1,connect2)
connectiondf=rbind(connectiondf,connection)
print(i)
}
Why am I getting this error? Apologies if this is a silly query but I am new to R

Using R to Grab Multiple Quandl Future Curves

I am trying to grab settlement values from 12 different future curves on Quandl and then assign them into an xts object. I am currently getting the error
"Error: object of type 'closure' is not subsettable"
and only one column. Ideally I want to have twelve columns named CL1, CL2, etc.
for (i in 1:12) {
data1 = Quandl(paste("CHRIS/CME_CL", i, sep = ""), start_date= "2017-01-01", type = "xts")
if (i == 1){ CL <- cbind(data1$Date, data1$Settle) }
if(i > 1){CL = cbind(CL, data$Settle)}
}
data1 = data.frame(matrix(ncol = 12, nrow = 279))
for (i in 1:12){
data1[,i] = Quandl(paste("CHRIS/CME_CL",i, sep=""), start_date="2017-01-01")$Settle
}
Can you verify if this works? If it works, you should be able to convert the data.frame() object to a series one.
I got this error after working on it a bit.
Error: { "quandl_error": { "code": "QELx01", "message": "You have
exceeded the anonymous user limit of 50 calls per day. To make more
calls today, please register for a free Quandl account and then
include your API key with your requests." } }
Hope it works.

Stop Rfacebook for loop outputting while still running code

The point in the code is to gather posts from a Facebook page and store them in my_page however i am unfamiliar with the code as it is for a Uni project. The problem i have is that it has to be used in a .rpres format created using Rstudio and as such i don't want the output but still need to run the code.
This is the output i don't want to be displayed:
```{r, echo = FALSE}
#install.packages("Rfacebook")
include(Rfacebook)
token <- "Facebook dev auth token goes here"
page_name <- "BuzzFeed"
my_page <- getPage(page_name, token, n = 2,reactions = TRUE,api = "v2.10")
number_required <- 50
dates <- seq(as.Date("2017/07/14"), Sys.Date(), by = "day")
#
n <- length(dates) - 1
df_daily <- list()
for (i in 1:n){
cat(as.character(dates[i]), " ")
try(df_daily[[i]] <- getPage(page_name, token,
n = number_required,reactions = TRUE,api = "v2.10",
since = dates[i],
until = dates[i+1]))
cat("\n")
}
```
Your problem is simply that Rfacebook::getPage prints to the console when it runs. That's because it calls cat(), which is the same thing as print(). Fortunately the package provides a switch to turn that off - all you need to do is add the verbose = FALSE argument to your call and it will stop printing:
getPage(...)
getPage(..., verbose = FALSE)
It's pretty bad practice for a package to call cat or print - they should use message and warning instead - so I have raised an issue with the package maintainer to ask for this to be changed, which you can watch here if you like:
https://github.com/pablobarbera/Rfacebook/issues/145

Resources