On Github ywchiu / SocialNetworkWithR
David Chiu @ 中興大學
http://libeltyseo.com/wp-content/uploads/2013/03/social-networking.png
http://cdn.macado.com/assets/2010/03/peeping-tom.gif
Open standard for authorization. OAuth provides a method for clients to access server resources on behalf of a resource owner
Open standard that allows users to be authenticated by certain co-operating sites
https://developers.facebook.com/
https://developers.facebook.com/tools/explorer/
Let's Hack
install.packages(c('rjson','RCurl','XML','igraph','bitops'),dependencies=TRUE) library(rjson) library(RCurl) library(XML) library(igraph) library(bitops)
fb_connect <- function( path = "me", access_token = token, options){ if( !missing(options) ){ options <- sprintf( "?%s", paste( names(options), "=", unlist(options), collapse = "&", sep = "" ) ) options <- gsub(",", "%2C", options) url <- sprintf( "https://graph.facebook.com/%s%s&access_token=%s", path, options, access_token ) } else { url <- sprintf( "https://graph.facebook.com/%s?access_token=%s", path, access_token ) } data <- getURL( url, ssl.verifypeer = FALSE ) fromJSON( data ) }
> fb_connect("me",access_token) $id [1] "778224889" $name [1] "David Chiu" $first_name [1] "David" $last_name [1] "Chiu" $link [1] "https://www.facebook.com/ChiuYW" $username [1] "ChiuYW"
> friends <- fb_connect(path="me/friends", access_token=access_token) > friends
friendgroups <- fb_connect( path="me/friendlists", access_token=access_token) friendgroups.id <- sapply(friendgroups$data, function(x) x$id) friendgroups.name <- sapply(friendgroups$data, function(x) x$name)
# Get Friends Information From FB me <- fb_connect(path="me", access_token=access_token, options=list("fields"="id,name,location,hometown,gender,friends.fields(location,hometown,name,gender)")) myname <- me$name friends <- me$friends # Extract Facebook IDs friends.id <- sapply(friends$data, function(x) x$id) # Extract FB Meta Information friends.name <- unlist(sapply(friends$data, function(x) iconv(x$name,to='UTF8',sub=""))) friends.gender <- sapply(friends$data, function(x) unlist(if(is.null(x$gender)) "none" else x$gender)) friends.location.id <- unlist(sapply(friends$data, function(x) x$location$id)) friends.location.name <- unlist(sapply(friends$data, function(x) {if (is.null(x$location$id)) "none" else iconv(x$location$name,to='UTF8',sub="")})) friends.hometown.name <- unlist(sapply(friends$data, function(x) {if (is.null(x$hometown$id)) "none" else iconv(x$hometown$name,to='UTF8',sub="")}))
friendships <- function() { print("Generating friendship matrix") N <- length(friends.id) friendship.matrix <- matrix(0,N,N) for (i in 1:N) { print(paste(i,friends.name[i])) tmp <- facebook( path=paste("me/mutualfriends", friends.id[i], sep="/") , access_token=access_token) mutualfriends <- sapply(tmp$data, function(x) x$id) friendship.matrix[i,friends.id %in% mutualfriends] <- 1 } # Create connections with my friends friends_con <- c(1:N) friends_con[] <-1 # Add this vector as a column to the friendship matrix friendship.matrix <- cbind(friendship.matrix,friends_con) # Append my friendship with myself friendship.matrix <- rbind(friendship.matrix,append(friends_con,0)) rownames(friendship.matrix) <- append(friends.name,myname) colnames(friendship.matrix) <- append(friends.name,myname) return (friendship.matrix) }
friendgraph <- function(friendship.matrix) { friendship.graph <- graph.adjacency(friendship.matrix, mode=c("undirected"), weighted=NULL) V(friendship.graph)$gender <- append(friends.gender, myself$gender) V(friendship.graph)$location <- append(friends.location.name, myself$location$name) V(friendship.graph)$hometown <- append(friends.hometown.name, myself$hometown$name) V(friendship.graph)$Label <- V(friendship.graph)$name return(friendship.graph) }
f_matrix <- friendships() f_graph <- friendgraph(f_matrix) write.graph(f_graph, "friends.graphml",format="graphml")
Using Gephi
Gephi, an open source graph visualization and manipulation software
https://www.udemy.com/gephi/
require(plyr) posts <- fb_connect(path="me/posts", access_token=access_token, options=list("fields"="likes.fields(name)")) post.likes <- sapply(posts$data, function(x) x$likes) post.likes.data <- sapply(post.likes, function(x) x$data) li = list() for (i in 1:length(post.likes.data)){ for (j in 1:(length(post.likes.data[[i]])-1)){ li <- append(li, post.likes.data[[i]][[j]]$name) } } df = do.call(rbind.data.frame, li) colnames(df) <- c("name") ddply(df, .(name), summarize, NumSubs = length(name))