15 September 2012

Download your Facebook photos using R

by manos_parzakonis

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")

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

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.

tags: API - facebook - pictures - R