Download your Facebook photos using R

[This article was first published on Stats raving mad » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

So tonight I wanted to download all my Facebook pictures. For some reason the zip file was corrupted each of the 3 times I downloaded, so I remembered that some time ago I was playing around with an R project named Facebook Data-Mining. The project is conveniently located at github and you can access it here. Looking at the  project description you realize that you can do much more with the project.

First of all you need an Access Token that you can get from https://developers.facebook.com/tools/explorer. After you get it navigate to the AccessToken.R file.Copy and paste the following as I added here the Facebook ID definition

# Get a Facebook Graph API Explorer Access Token
# Go to 'https://developers.facebook.com/tools/explorer', 
# login and click "Get access token"
# Your ID is located first in the output of the Graph Explorer,it's all numbers
# Store your access token here:

me <- "PasteYourFacebookIDHere"
access.token <- "PasteYourAccessTokenHere"

 Now go and source the Setup.R script

# Step 1: Create functions used by the demo
# This creates the 'facebook' function as described at
# http://romainfrancois.blog.free.fr/index.php?post/2012/01/15/Crawling-facebook-with-R
# and other functions to create initials, etc.
cat("Step 1: Create functions used by the demo","\n")
source("Functions.R")

# Step 2: Run the requirements script
# This installs (if required) and loads the mandatory libaries
cat("Step 2: Run the requirements script","\n")
source("Requirements.R")

Finally, run the following excerpt from the Photos.R script

# Create a "photos" directory (Warnings ignored so if it 
# already exists it will safely continue)

# Create a "photos" directory or call it as you like it
dir.create( "photos", showWarnings = FALSE )

# Create a directory for the individual's photos
dir.create( paste("photos",individual.id, sep="/"), showWarnings = FALSE )

# Download each of the individuals photos
for (i in 1:length(individual.photos.url)) {
download.file(individual.photos.url[i], individual.photos.file[i])
}

Then R will output a bunch of download streams, like this and inside the working directory you will find a photos folder and a Facebook subfolder

Note : It is likely that you will get the known error when a connection to Facebook via R is made

Error in function (type, msg, asError = TRUE)  : 
  SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

The solutions to this are outlined in the A tiny RCurl headache post.



To leave a comment for the author, please follow the link and comment on their blog: Stats raving mad » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)