├── .gitignore ├── AccessToken.R ├── Demo.R ├── DemoGeorgeTakei.R ├── DemoTED.R ├── ExifData.R ├── Facebook.Rproj ├── FriendLocations.R ├── Friends.R ├── Functions.R ├── Individual.R ├── Network.R ├── Photos.R ├── Posts.R ├── ReadMe.txt ├── Requirements.R └── Setup.R /.gitignore: -------------------------------------------------------------------------------- 1 | .svn 2 | Downloads 3 | .DS_Store -------------------------------------------------------------------------------- /AccessToken.R: -------------------------------------------------------------------------------- 1 | # Get a Facebook Graph API Explorer Access Token 2 | # Go to 'https://developers.facebook.com/tools/explorer', login and click "Get access token" 3 | # Store your access token here: 4 | 5 | access.token <- "AAACEdEose0cBAJ2GSJ77pHERFPPmi6hR9ZAdNKw3MslNFPVnB9CLtxYeO02TqZCwx2vVWnt5Ist6RS9ZClXYD9aNVTeiTmoX3fEkGmunQZDZD" -------------------------------------------------------------------------------- /Demo.R: -------------------------------------------------------------------------------- 1 | # Facebook - hacking the social network 2 | 3 | # Step 1: Get a Facebook Graph API Explorer Access Token 4 | # Go to 'https://developers.facebook.com/tools/explorer', login and click "Get access token" 5 | # Store your access token here: 6 | cat("Step 1: Get a Facebook Graph API Explorer Access Token","\n") 7 | source("AccessToken.R") 8 | 9 | # Step 2: Add the userid of the individual whose social network you want to examine 10 | # By default this is "me" but otherwise it should be an individuals facebook id 11 | cat("Step 2: Add the userid of the individual whose social network you want to examine","\n") 12 | individual.id <- "me" 13 | file.seqno <- 1 14 | 15 | # Step 3: Fetch data about the selected individual 16 | cat("Step 3: Fetch data about the selected individual","\n") 17 | source("Individual.R") 18 | 19 | # Step 4: Fetch data about the individuals friends 20 | cat("Step 4: Fetch data about the individuals friends","\n") 21 | source("Friends.R") 22 | 23 | # Step 5: Fetch the individuals and the friends photos 24 | cat("Step 5: Fetch the individuals and the friends photos","\n") 25 | source("Photos.R") 26 | 27 | # Step 6: Generate Social Network Maps 28 | cat("Step 6: Generate Social Network Maps","\n") 29 | source("Network.R") 30 | 31 | # Step 7: Show data about posts 32 | cat("Step 7: Show data about posts","\n") 33 | source("Posts.R") 34 | 35 | # Step 8: Show Exif Data from individuals photos 36 | cat("Step 8: Show Exif Data from individuals photos","\n") 37 | source("ExifData.R") 38 | -------------------------------------------------------------------------------- /DemoGeorgeTakei.R: -------------------------------------------------------------------------------- 1 | # Facebook - hacking the social network 2 | 3 | # Step 1: Get a Facebook Graph API Explorer Access Token 4 | # Go to 'https://developers.facebook.com/tools/explorer', login and click "Get access token" 5 | # Store your access token here: 6 | cat("Step 1: Get a Facebook Graph API Explorer Access Token","\n") 7 | source("AccessToken.R") 8 | 9 | # Step 2: Add the userid of the individual whose social network you want to examine 10 | # By default this is "me" but otherwise it should be an individuals facebook id 11 | cat("Step 2: Add the userid of the individual whose social network you want to examine","\n") 12 | individual.id <- "205344452828349" 13 | 14 | # Step 3: Fetch data about the selected individual 15 | cat("Step 3: Fetch data about the selected individual","\n") 16 | source("Individual.R") 17 | 18 | # Step 4: Show data about posts 19 | cat("Step 4: Show data about posts","\n") 20 | source("Posts.R") -------------------------------------------------------------------------------- /DemoTED.R: -------------------------------------------------------------------------------- 1 | # Facebook - hacking the social network 2 | 3 | # Step 1: Get a Facebook Graph API Explorer Access Token 4 | # Go to 'https://developers.facebook.com/tools/explorer', login and click "Get access token" 5 | # Store your access token here: 6 | cat("Step 1: Get a Facebook Graph API Explorer Access Token","\n") 7 | source("AccessToken.R") 8 | 9 | # Step 2: Add the userid of the individual whose social network you want to examine 10 | # By default this is "me" but otherwise it should be an individuals facebook id 11 | cat("Step 2: Add the userid of the individual whose social network you want to examine","\n") 12 | individual.id <- "29092950651" 13 | 14 | # Step 3: Fetch data about the selected individual 15 | cat("Step 3: Fetch data about the selected individual","\n") 16 | source("Individual.R") 17 | 18 | # Step 4: Show data about posts 19 | cat("Step 4: Show data about posts","\n") 20 | source("Posts.R") -------------------------------------------------------------------------------- /ExifData.R: -------------------------------------------------------------------------------- 1 | # Extracting EXIF data from a photo 2 | # It should be possible to extract EXIF data with the following package, however I have failed 3 | # http://cran.r-project.org/web/packages/adimpro/adimpro.pdf 4 | 5 | # Instead using a hack (via a system call) to grab the output from ExifTool 6 | 7 | cat("Individuals Photo Files\n") 8 | for (i in 1:length(individual.photos.file)) { 9 | ExifData <- fromJSON(paste(system(paste("/usr/bin/exiftool -j ",individual.photos.file[i]),intern=T), collapse=""))[[1]] 10 | cat(ExifData$FileName," (", ExifData$FileSize,")\n",sep="") 11 | if (!is.null(ExifData$PrimaryPlatform)) { 12 | cat("Photo Primary Platform: ", ExifData$PrimaryPlatform, "\n", seq="") 13 | } 14 | } 15 | 16 | rm(i) 17 | 18 | # Some of the fields that may be found 19 | 20 | # BitsPerSample 21 | # BlueMatrixColumn 22 | # BlueTRC 23 | # ChromaticAdaptation 24 | # CMMFlags 25 | # ColorComponents 26 | # ColorSpaceData 27 | # Comment 28 | # ConnectionSpaceIlluminant 29 | # DeviceAttributes 30 | # DeviceManufacturer 31 | # DeviceModel 32 | # DeviceModelDesc 33 | # Directory 34 | # EncodingProcess 35 | # ExifToolVersion 36 | # FileModifyDate 37 | # FileName 38 | # FilePermissions 39 | # FileSize 40 | # FileType 41 | # GreenMatrixColumn 42 | # GreenTRC 43 | # ImageHeight 44 | # ImageSize 45 | # ImageWidth 46 | # JFIFVersion 47 | # Luminance 48 | # MeasurementBacking 49 | # MeasurementFlare 50 | # MeasurementGeometry 51 | # MeasurementIlluminant 52 | # MeasurementObserver 53 | # MediaBlackPoint 54 | # MediaWhitePoint 55 | # MIMEType 56 | # PrimaryPlatform 57 | # ProfileClass 58 | # ProfileCMMType 59 | # ProfileConnectionSpace 60 | # ProfileCopyright 61 | # ProfileCreator 62 | # ProfileDateTime 63 | # ProfileDescription 64 | # ProfileFileSignature 65 | # ProfileID 66 | # ProfileVersion 67 | # RedMatrixColumn 68 | # RedTRC 69 | # RenderingIntent 70 | # ResolutionUnit 71 | # SourceFile 72 | # Technology 73 | # ViewingCondDesc 74 | # XResolution 75 | # YCbCrSubSampling 76 | # YResolution 77 | -------------------------------------------------------------------------------- /Facebook.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | RootDocument: 15 | -------------------------------------------------------------------------------- /FriendLocations.R: -------------------------------------------------------------------------------- 1 | friends.location.id <- character(0) 2 | friends.location.name <- character(0) 3 | friends.location.longitude <- character(0) 4 | friends.location.latitude <- character(0) 5 | 6 | for (i in 1:length(friends.id)) { 7 | temp.friend.data <- FacebookFetch(access.path = paste(friends.id[i], 8 | sep = "/"), access.token = access.token) 9 | if (!is.null(temp.friend.data$location$id)) { 10 | friends.location.id[i] <- temp.friend.data$location$id 11 | if (!is.null(temp.friend.data$location$name)) { 12 | friends.location.name[i] <- temp.friend.data$location$name 13 | } 14 | temp.location.data <- FacebookFetch(access.path = paste(friends.location.id[i], 15 | sep = "/"), access.token = access.token) 16 | if (!is.null(temp.location.data$location$longitude) & 17 | !is.null(temp.location.data$location$latitude)) { 18 | friends.location.longitude[i] <- temp.location.data$location$longitude 19 | friends.location.latitude[i] <- temp.location.data$location$latitude 20 | } 21 | } 22 | } 23 | 24 | 25 | sink("output/FriendsLocation.kml") 26 | cat('\n', sep="") 27 | cat('\n', sep="") 28 | cat(' \n', sep="") 29 | cat(' Facebook Friends Locations\n', sep="") 30 | cat(' Map of how my Facebook Friends are geographically related\n', sep="") 31 | cat(' \n', sep="") 37 | 38 | cat(' \n', sep="") 39 | cat(' Individuals\n', sep="") 40 | for (i in 1:length(friends.id)) { 41 | if ( !is.na(friends.location.longitude[i]) ){ 42 | cat(' \n', sep="") 43 | cat(' ',friends.initial[i],'\n', sep="") 44 | cat(' ',friends.name[i],'\n', sep="") 45 | cat(' \n', sep="") 46 | cat(' ',friends.location.longitude[i],',',friends.location.latitude[i],',0','\n', sep="") 47 | cat(' \n', sep="") 48 | cat(' \n', sep="") 49 | } 50 | } 51 | cat(' \n', sep="") 52 | 53 | cat(' \n', sep="") 54 | cat(' Relationships between Individuals\n', sep="") 55 | for (i in 1:length(friends.id)) { 56 | for (j in 1:length(friends.id)) { 57 | if (friends.matrix[i,j] > 0) { 58 | if ( !is.na(friends.location.longitude[i]) & !is.na(friends.location.longitude[j]) ) { 59 | cat(' \n', sep="") 60 | cat(' ',friends.initial[i],'/',friends.initial[j],'\n', sep="") 61 | cat(' ',friends.name[i],'/',friends.name[j],'\n', sep="") 62 | cat(' #style1\n', sep="") 63 | cat(' \n', sep="") 64 | cat(' 1\n', sep="") 65 | cat(' clampToGround\n', sep="") 66 | cat(' \n', sep="") 67 | cat(' ',friends.location.longitude[i],',',friends.location.latitude[i],',0.0','\n', sep="") 68 | cat(' ',friends.location.longitude[j],',',friends.location.latitude[j],',0.0','\n', sep="") 69 | cat(' \n', sep="") 70 | cat(' \n', sep="") 71 | cat(' \n', sep="") 72 | } 73 | } 74 | } 75 | } 76 | cat(' \n', sep="") 77 | 78 | cat(' \n', sep="") 79 | cat('\n', sep="") 80 | 81 | sink() 82 | -------------------------------------------------------------------------------- /Friends.R: -------------------------------------------------------------------------------- 1 | # This module fetches the friend data 2 | 3 | # Get the list of friends 4 | friends <- FacebookFetch(access.path=paste(individual.id,"friends", sep="/"), access.token=access.token) 5 | # Pre-1986 Group 6 | friends <- FacebookFetch(access.path=paste("10150846364521714","members", sep="/"), access.token=access.token) 7 | # Sequent Group 8 | friends <- FacebookFetch(access.path=paste("10150846271306714","members", sep="/"), access.token=access.token) 9 | 10 | # Extract the friends Facebook IDs 11 | friends.id <- sapply(friends$data, function(x) x$id) 12 | 13 | # Extract the friends Facebook names 14 | friends.name <- sapply(friends$data, function(x) iconv(x$name,"UTF-8","ASCII//TRANSLIT")) 15 | 16 | # Create a list of friends initials 17 | friends.initial <- sapply(strsplit(friends.name, " "), GetInitials) 18 | 19 | # Count the number of friends 20 | friends.count <- length(friends.id) 21 | 22 | # Create a list of each friends profile photo url 23 | friends.photos.url <- paste("http://graph.facebook.com", friends.id, "picture", sep="/") 24 | 25 | # Create a list of each friends profile photo file after downloading 26 | friends.photos.file <- paste(paste("photos","friends",friends.id, sep="/"),"jpg", sep=".") 27 | 28 | # Generate the friendship matrix 29 | 30 | # This is square matrix of friends.count by friends.count 31 | 32 | # Create the initial matrix, populating each with a value of zero to represent not a mutual friend 33 | friends.matrix <- matrix(0,friends.count,friends.count) 34 | 35 | # Then for each friend 36 | for (i in 1:friends.count) { 37 | # Get the mutualfriends data 38 | mutualfriends <- FacebookFetch(access.path=paste(individual.id,"mutualfriends", friends.id[i], sep="/"), access.token=access.token) 39 | # Extract the mutualfriends id 40 | mutualfriends.id <- sapply(mutualfriends$data, function(x) x$id) 41 | # Update each intersection in the matrix of a friend/mutualfriend with a value of one to represent a mutual friend 42 | friends.matrix[i,friends.id %in% mutualfriends.id] <- 1 43 | } 44 | 45 | # Cleanup temporary variables 46 | rm(i) 47 | rm(mutualfriends) 48 | rm(mutualfriends.id) 49 | -------------------------------------------------------------------------------- /Functions.R: -------------------------------------------------------------------------------- 1 | # This creates the 'facebook' function as described at 2 | # http://romainfrancois.blog.free.fr/index.php?post/2012/01/15/Crawling-facebook-with-R 3 | # and functions to create initials, etc. 4 | 5 | # Define the facebook function 6 | 7 | FacebookFetch <- function(access.path = "me", access.token, access.options) { 8 | if (missing(access.options)) { 9 | access.options <- "" 10 | } 11 | else { 12 | access.options <- sprintf("?%s", paste(names(access.options), "=", 13 | unlist(access.options), collapse = "&", sep = "")) 14 | } 15 | data <- getURL(sprintf("https://graph.facebook.com/%s%s&access.token=%s", 16 | access.path, access.options, access.token)) 17 | fromJSON(data) 18 | } 19 | 20 | # Define a function to convert a space seperated list into a set of initials 21 | # e.g. "David Walker" will become "DW" 22 | 23 | GetInitials <- function(x) paste(substr(x, 1, 1), collapse = "") 24 | 25 | # Define a function to check a package is installed 26 | 27 | IsInstalled <- function(mypkg) is.element(mypkg, installed.packages()[,1]) 28 | 29 | # A set of functions to parse posts of an individual 30 | 31 | ParsePosts <- function(x, f) sapply(x$data, f) 32 | ParsePostMessages <- function(x) if(!is.null(x$message)) x$message else NA 33 | ParsePostLikes <- function(x) if(!is.null(x$likes$count)) x$likes$count else 0 34 | ParsePostComments <- function(x) if(!is.null(x$comments$count)) x$comments$count else 0 35 | ParsePostLinks <- function(x) if (x$type=="link" & x$from$id==individual.id) x$link else NA 36 | ParsePostLinkLikes <- function(x) if (x$type=="link" & x$from$id==individual.id) x$likes$count else NA 37 | -------------------------------------------------------------------------------- /Individual.R: -------------------------------------------------------------------------------- 1 | # Fetch data about the selected individual 2 | 3 | individual <- FacebookFetch(access.path=paste(individual.id),access.token=access.token ) 4 | if (length(individual$id)) { 5 | cat ("Working with individual: ",individual$name," (",individual$id,")\n", sep="") 6 | } else{ 7 | cat("Message: ", unlist(individual)[1], "\n") 8 | cat("Type: ", unlist(individual)[2], "\n") 9 | cat("Code: ", unlist(individual)[3], "\n") 10 | stop(" Cannot continue") 11 | } 12 | 13 | # Fetch an individuals photos 14 | individual.photos <- FacebookFetch(access.path=paste(individual.id,"photos", sep="/"),access.token=access.token ) 15 | 16 | # Fetch the url of the indiviiduals photos 17 | if (!is.null(individual.photos$data)) { 18 | individual.photos.url <- sapply( individual.photos$data, function(x){ 19 | url <- x$source 20 | url <- gsub("https","http",url) 21 | }) 22 | 23 | # Create the location to store the individuals photos locally 24 | individual.photos.file <- paste("photos",individual.id,basename(individual.photos.url), sep="/") 25 | } 26 | 27 | # Get the individuals posts 28 | individual.posts <- list() 29 | 30 | i <- 0 31 | next.path <- paste(individual.id,"posts", sep="/") 32 | while(length(next.path)!=0) { 33 | i<-i+1 34 | individual.posts[[i]] <- FacebookFetch(access.path=next.path , access.token=access.token) 35 | next.path <- sub("https://graph.facebook.com/", "", individual.posts[[i]]$paging$'next') 36 | } 37 | 38 | individual.posts[[i]] <- NULL 39 | 40 | # Parse the list, extract number of likes and the corresponding text (status) 41 | 42 | # Get the text of the message 43 | individual.posts.messages <- unlist(sapply(individual.posts, ParsePosts, f=ParsePostMessages)) 44 | 45 | # Get the count of the likes 46 | individual.posts.likes <- unlist(sapply(individual.posts, ParsePosts, f=ParsePostLikes)) 47 | 48 | # Get a count of the comments 49 | individual.posts.comments <- unlist(sapply(individual.posts, ParsePosts, f=ParsePostComments)) 50 | 51 | # Get the individuals posted links 52 | individual.links <- unlist(sapply(individual.posts, ParsePosts, f=ParsePostLinks)) 53 | 54 | # Get the number of likes for any given link 55 | individual.links.likes <- unlist(sapply(individual.posts, ParsePosts, f=ParsePostLinkLikes)) 56 | 57 | # Display the three most popular links 58 | cat("Displaying the three most popular links by this individual:\n") 59 | cat(individual.links[order(individual.links.likes,decreasing=TRUE)][1:3]) 60 | 61 | rm(i) 62 | rm(next.path) -------------------------------------------------------------------------------- /Network.R: -------------------------------------------------------------------------------- 1 | # Differenent representations of the social network 2 | 3 | # Type 1: Using the 'network' library 4 | 5 | # Friends (using their initials) 6 | friends.network <- as.network(friends.matrix) 7 | plot(friends.network, label=friends.initial, arrowhead.cex=0) 8 | 9 | # And writing to a PDF 10 | pdf(file=paste("output/Network_Initials_",file.seqno,".pdf", sep=""), width=25, height=25) 11 | plot(friends.network, label=friends.initial, arrowhead.cex=0) 12 | dev.off() 13 | file.seqno <- file.seqno + 1 14 | 15 | # Friends (using their full names) 16 | friends.network <- as.network(friends.matrix) 17 | plot(friends.network, label=friends.name, arrowhead.cex=0) 18 | 19 | # And writing to a PDF 20 | pdf(file=paste("output/Network_Names_",file.seqno,".pdf", sep=""), width=25, height=25) 21 | plot(friends.network, label=friends.name, arrowhead.cex=0) 22 | dev.off() 23 | file.seqno <- file.seqno + 1 24 | 25 | # Type 2: Using the 'Rgraphviz' library 26 | 27 | # Initial set up for the graph 28 | 29 | # Create the graph 30 | friends.graph <-graph.adjacency(friends.matrix,mode="undirected") 31 | # Create a fixed layout 32 | set.seed(1) 33 | friends.layout <- layout.fruchterman.reingold(friends.graph) 34 | # Rescale the layout to -1 to 1 35 | friends.layout[,1]=(friends.layout[,1]-min(friends.layout[,1]))/(max(friends.layout[,1])-min(friends.layout[,1]))*2-1 36 | friends.layout[,2]=(friends.layout[,2]-min(friends.layout[,2]))/(max(friends.layout[,2])-min(friends.layout[,2]))*2-1 37 | 38 | # Friends (using their initials) 39 | V(friends.graph)$label <- friends.initial 40 | plot(friends.graph,layout=friends.layout,vertex.size=0,vertex.frame.color="#00000000") 41 | 42 | # And writing to a PDF 43 | pdf(file=paste("output/Network_Initials_",file.seqno,".pdf", sep=""), width=25, height=25) 44 | plot(friends.graph,layout=friends.layout,vertex.size=0,vertex.frame.color="#00000000") 45 | dev.off() 46 | file.seqno <- file.seqno + 1 47 | 48 | # Friends (using their names) 49 | V(friends.graph)$label <- friends.name 50 | plot(friends.graph,layout=friends.layout,vertex.size=0,vertex.frame.color="#00000000") 51 | 52 | # And writing to a PDF 53 | pdf(file=paste("output/Network_Names_",file.seqno,".pdf", sep=""), width=25, height=25) 54 | plot(friends.graph,layout=friends.layout,vertex.size=0,vertex.frame.color="#00000000") 55 | dev.off() 56 | file.seqno <- file.seqno + 1 57 | 58 | # Currently commented out until I can improve the speed and output size 59 | # 60 | # # Friends (using their photos) 61 | # V(friends.graph)$label <- "" 62 | # plot(friends.graph,layout=friends.layout,vertex.size=0,vertex.frame.color="#00000000") 63 | # 64 | # # Additional code required to add image 65 | # friends.layout.tmp<-friends.layout 66 | # for(i in length(friends.id):1){ 67 | # myfriend.photo<-read.jpeg(paste("photos/friends/",friends.id[i],".jpg",sep="")) 68 | # if(i!=1){ 69 | # apply(friends.layout.tmp,1,function(x)rasterImage(myfriend.photo,x[1]-0.05,x[2]-0.05,x[1]+0.05,x[2]+0.05)) 70 | # friends.layout.tmp<-friends.layout.tmp[-i,] 71 | # }else{ 72 | # rasterImage(myfriend.photo,friends.layout.tmp[1]-0.05,friends.layout.tmp[2]-0.05,friends.layout.tmp[1]+0.05,friends.layout.tmp[2]+0.05) 73 | # } 74 | # } 75 | # 76 | # # And writing to a PDF 77 | # pdf(file=paste("output/Network_Photos_",file.seqno,".pdf", sep=""), width=25, height=25) 78 | # plot(friends.graph,layout=friends.layout,vertex.size=0,vertex.frame.color="#00000000") 79 | # friends.layout.tmp<-friends.layout 80 | # for(i in length(friends.id):1){ 81 | # myfriend.photo<-read.jpeg(paste("photos/friends/",friends.id[i],".jpg",sep="")) 82 | # if(i!=1){ 83 | # apply(friends.layout.tmp,1,function(x)rasterImage(myfriend.photo,x[1]-0.05,x[2]-0.05,x[1]+0.05,x[2]+0.05)) 84 | # friends.layout.tmp<-friends.layout.tmp[-i,] 85 | # }else{ 86 | # rasterImage(myfriend.photo,friends.layout.tmp[1]-0.05,friends.layout.tmp[2]-0.05,friends.layout.tmp[1]+0.05,friends.layout.tmp[2]+0.05) 87 | # } 88 | # } 89 | # dev.off() 90 | # file.seqno <- file.seqno + 1 91 | 92 | 93 | -------------------------------------------------------------------------------- /Photos.R: -------------------------------------------------------------------------------- 1 | # Create a "photos" directory (Warnings ignored so if it already exists it will safely continue) 2 | 3 | # Create a "photos" directory 4 | dir.create( "photos", showWarnings = FALSE ) 5 | 6 | # Create a directory for the individual's photos 7 | dir.create( paste("photos",individual.id, sep="/"), showWarnings = FALSE ) 8 | 9 | # Download each of the individuals photos 10 | for (i in 1:length(individual.photos.url)) { 11 | download.file(individual.photos.url[i], individual.photos.file[i]) 12 | } 13 | 14 | # Create a directory for the friends photos 15 | dir.create( paste("photos","friends", sep="/"), showWarnings = FALSE ) 16 | 17 | # Download each friends photos 18 | for (i in 1:length(friends.photos.url)) { 19 | download.file(friends.photos.url[i], friends.photos.file[i]) 20 | } 21 | 22 | # Nasty hack to call CentOS 'convert' command for when people have uploaded non-jpg files 23 | system('for i in `ls photos/*/*.jpg`; do j=${i%.*}; convert $j.jpg $j.jpg; done', wait=TRUE) 24 | 25 | # Cleanup temporary variables 26 | rm(i) 27 | -------------------------------------------------------------------------------- /Posts.R: -------------------------------------------------------------------------------- 1 | # Display the most liked and the most commented messages 2 | 3 | cat("Most Liked post: ", individual.posts.messages[which.max(individual.posts.likes)], 4 | " (", individual.posts.likes[which.max(individual.posts.likes)], 5 | ")\n", sep = "") 6 | cat("Most Commented post: ", individual.posts.messages[which.max(individual.posts.comments)], 7 | " (", individual.posts.comments[which.max(individual.posts.comments)], 8 | ")\n", sep = "") 9 | -------------------------------------------------------------------------------- /ReadMe.txt: -------------------------------------------------------------------------------- 1 | Facebook Data Mining in R 2 | 3 | Introduction 4 | 5 | This group of scripts was inspired by a series of posts on how to data mine Facebook data. The scripts allow you to connect to the Facebook graph API and download data associated with an individual, their posts, friends, etc. and then analyse and graaph these in various ways. 6 | 7 | About The Script 8 | 9 | The scripts are broken up into a number of groups as follows: 10 | 11 | * Setup.R - Sets up the environment required 12 | * Demo.R - A demonstration of personal data 13 | * DemoGeorgeTakei.R - A demonstration of data available from Star Trek actor and activist George Takei (Sulu) 14 | * DemoTED.R - A demonstration of data available from TED.com's video posts 15 | 16 | All the other scripts called from these four scripts 17 | 18 | Getting Started 19 | 20 | * First you need an access token. To do this you need to visit https://developers.facebook.com/tools/explorer', 21 | login and click "Get access token" and then paste this into the appropriate place in AccessToken.R 22 | 23 | * Next from within R (or RStudio - http://rstudio.org, my personal preferred method) run the following command: 24 | 25 | source("Setup.R") 26 | 27 | This will ensure that all the required packages from the demo and installed and loaded (by calling Requirements.R) 28 | and that all the shared functions that are required are created (by calling Functions.R) 29 | 30 | The Demos 31 | 32 | At the three demos listed above can be called with: 33 | 34 | source("Demo.R") 35 | source("DemoGeorgeTakei.R") 36 | source("DemoTED.R") 37 | 38 | Each of these will call some of the following functions: 39 | 40 | AccessToken.R - Gives access to the API - called by all scripts 41 | Individual.R - Gets data associated with the individual - called by all scripts 42 | Friends.R - Gets data associated with an individuals friends - called by Demo.R 43 | Photos.R - Gets photographs associated with an individual and their friends - called by Demo.R 44 | Network.R - Plots the social network of a group of friends in various formats/methods - called by Demo.R 45 | Posts.R - Gets various pieces of information about posts made by an individual - called by all scripts 46 | 47 | 48 | List of source URLs used in the development of these scripts 49 | 50 | http://romainfrancois.blog.free.fr/index.php?post/2012/01/15/Crawling-facebook-with-R 51 | http://blog.revolutionanalytics.com/2012/01/visualize-your-facebook-friends-network-with-r.html 52 | http://applyr.blogspot.co.uk/2012/01/mining-facebook-data-most-liked-status.html?spref=tw 53 | https://github.com/sciruela/facebookFriends 54 | http://google-styleguide.googlecode.com/svn/trunk/google-r-style.html -------------------------------------------------------------------------------- /Requirements.R: -------------------------------------------------------------------------------- 1 | # This installs (if required) and loads the mandatory libaries for this demo 2 | 3 | # Required CRAN packages 4 | 5 | package.list <- c("igraph", "network", "pixmap", "RCurl", "ReadImages", "rjson", "adimpro") 6 | 7 | for (i in 1:length(package.list)) { 8 | pkg <- package.list[i] 9 | if(!IsInstalled(pkg)) { 10 | install.packages(pkg) 11 | } 12 | require(pkg, character.only = TRUE) 13 | } 14 | 15 | # Required BiocLite packages 16 | 17 | source("http://www.bioconductor.org/biocLite.R") 18 | 19 | package.list <- c("Rgraphviz") 20 | 21 | # To add Rgraphviz on CentOS linux requires: 22 | # yum inmstall graphviz 23 | # yum inmstall graphviz-devel 24 | # To add Rgraphviz on Mac OS X requires: 25 | # http://www.graphviz.org/Download_macos.php 26 | # Note: Requires version 2.26.3 which is not available for all OS X platforms 27 | 28 | for (i in 1:length(package.list)) { 29 | pkg <- package.list[i] 30 | if(!IsInstalled(pkg)) { 31 | biocLite(pkg) 32 | } 33 | require(pkg, character.only = TRUE) 34 | } 35 | 36 | rm(i) 37 | rm(package.list) 38 | rm(pkg) -------------------------------------------------------------------------------- /Setup.R: -------------------------------------------------------------------------------- 1 | # Step 1: Create functions used by the demo 2 | # This creates the 'facebook' function as described at 3 | # http://romainfrancois.blog.free.fr/index.php?post/2012/01/15/Crawling-facebook-with-R 4 | # and other functions to create initials, etc. 5 | cat("Step 1: Create functions used by the demo","\n") 6 | source("Functions.R") 7 | 8 | # Step 2: Run the requirements script 9 | # This installs (if required) and loads the mandatory libaries 10 | cat("Step 2: Run the requirements script","\n") 11 | source("Requirements.R") 12 | --------------------------------------------------------------------------------