Skip to content

Commit

Permalink
fixes #138 (getUsers now works w/recent APIs)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablobarbera committed Jul 11, 2017
1 parent 7c08ae4 commit 88697fe
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Rfacebook/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,5 @@
2017-06-01 - version 0.6.16
- documentation updates
- searchGroups now returns 25+ results
2017-07-11 - version 0.6.17
- getUsers now works with recent versions of API
4 changes: 2 additions & 2 deletions Rfacebook/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: Rfacebook
Title: Access to Facebook API via R
Description: Provides an interface to the Facebook API.
Version: 0.6.16
Date: 2017-06-01
Version: 0.6.17
Date: 2017-07-11
Author: Pablo Barbera <[email protected]>, Michael Piccirilli
<[email protected]>, Andrew Geisler, Wouter van Atteveldt
Maintainer: Pablo Barbera <[email protected]>
Expand Down
39 changes: 20 additions & 19 deletions Rfacebook/R/getUsers.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@
#' }
#'

getUsers <- function(users, token, private_info=FALSE)
getUsers <- function(users, token, private_info=FALSE, api="v2.9")
{

tkversion <- getTokenVersion(token)
api.max <- ifelse(tkversion=='v2', 50, 500)

if (length(users)==1 && users=='me'){
query <- paste0('https://graph.facebook.com/',
ifelse(tkversion=='v2', 'v2.0/', ''), 'me?')
ifelse(tkversion=='v2', api, ''), '/me?')
content <- callAPI(query, token)
df <- userDataToDF(list(content), private_info=private_info)
return(df)
Expand All @@ -57,27 +57,28 @@ getUsers <- function(users, token, private_info=FALSE)
users.query <- paste(users[1:first.n], collapse=",")
## first query: checking what users are actual users vs pages
query <- paste0('https://graph.facebook.com/',
ifelse(tkversion=='v2', 'v2.0/', ''), '?ids=', users.query)
ifelse(tkversion=='v2', api, ''), '?ids=', users.query, "&metadata=1")
## making query
content <- callAPI(query, token)
if (length(content$error_code)>0){
stop(content$error_msg, ". Querying too many users?")
}
actual.users <- which(unlist(lapply(content, function(x) is.null(x$category))))
pages <- which(unlist(lapply(content, function(x) !is.null(x$category))))
types <- unlist(lapply(content, function(x) x$metadata$type))
actual.users <- names(types)[types=="user"]
pages <- names(types)[types=="page"]
## getting data for users
if (length(actual.users)>0){
if (private_info==TRUE){
query <- paste('https://graph.facebook.com/',
ifelse(tkversion=='v2', 'v2.0/', ''), '?ids=',
paste(names(actual.users), collapse=","),
ifelse(tkversion=='v2', api, ''), '?ids=',
paste(actual.users, collapse=","),
"&fields=id,name,first_name,middle_name,last_name,gender,locale,birthday,",
"location,hometown,relationship_status,picture.type(large)", sep="")
}
if (private_info==FALSE){
query <- paste('https://graph.facebook.com/',
ifelse(tkversion=='v2', 'v2.0/', ''), '?ids=',
paste(names(actual.users), collapse=","),
ifelse(tkversion=='v2', api, ''), '?ids=',
paste(actual.users, collapse=","),
"&fields=id,name,first_name,middle_name,last_name,gender,locale,",
"picture.type(large)", sep="")
}
Expand All @@ -91,9 +92,9 @@ getUsers <- function(users, token, private_info=FALSE)
if (length(pages)>0){
## getting data for pages
query <- paste('https://graph.facebook.com/',
ifelse(tkversion=='v2', 'v2.0/', ''), '?ids=',
paste(names(pages), collapse=","),
"&fields=id,name,category,likes,picture.type(large)", sep="")
ifelse(tkversion=='v2', api, ''), '?ids=',
paste(pages, collapse=","),
"&fields=id,name,category,fan_count,picture.type(large)", sep="")
## making query
content <- callAPI(query, token)
df.pages <- userDataToDF(content, private_info=private_info)
Expand All @@ -114,7 +115,7 @@ getUsers <- function(users, token, private_info=FALSE)
users.query <- paste(users[first.n:last.n], collapse=",")
## first query: checking what users are actual users vs pages
query <- paste0('https://graph.facebook.com/',
ifelse(tkversion=='v2', 'v2.0/', ''), '?ids=', users.query)
ifelse(tkversion=='v2', api, ''), '?ids=', users.query)
## making query
content <- callAPI(query, token)
if (length(content$error_code)>0){
Expand All @@ -126,15 +127,15 @@ getUsers <- function(users, token, private_info=FALSE)
if (length(actual.users)>0){
if (private_info==TRUE){
query <- paste('https://graph.facebook.com/',
ifelse(tkversion=='v2', 'v2.0/', ''), '?ids=',
paste(names(actual.users), collapse=","),
ifelse(tkversion=='v2', api, ''), '?ids=',
paste(actual.users, collapse=","),
"&fields=id,name,first_name,middle_name,last_name,gender,locale,birthday,",
"location,hometown,relationship_status,picture.type(large)", sep="")
}
if (private_info==FALSE){
query <- paste('https://graph.facebook.com/',
ifelse(tkversion=='v2', 'v2.0/', ''), '?ids=',
paste(names(actual.users), collapse=","),
ifelse(tkversion=='v2', api, ''), '?ids=',
paste(actual.users, collapse=","),
"&fields=id,name,first_name,middle_name,last_name,gender,locale,",
"picture.type(large)", sep="")
}
Expand All @@ -148,8 +149,8 @@ getUsers <- function(users, token, private_info=FALSE)
if (length(pages)>0){
## getting data for pages
query <- paste('https://graph.facebook.com/',
ifelse(tkversion=='v2', 'v2.0/', ''), '?ids=',
paste(names(pages), collapse=","),
ifelse(tkversion=='v2', api, ''), '?ids=',
paste(pages, collapse=","),
"&fields=id,name,category,likes,picture.type(large)", sep="")
## making query
content <- callAPI(query, token)
Expand Down
2 changes: 1 addition & 1 deletion Rfacebook/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ userDataToDF <- function(user_data, private_info){
gender = unlistWithNA(user_data, 'gender'),
locale = unlistWithNA(user_data, 'locale'),
#category = unlistWithNA(user_data, 'category'),
likes = unlistWithNA(user_data, 'likes'),
likes = unlistWithNA(user_data, 'fan_count'),
picture = unlistWithNA(user_data, c('picture', 'data', 'url')),
stringsAsFactors=F)
if (private_info==TRUE){
Expand Down

0 comments on commit 88697fe

Please sign in to comment.