Is it possible to update my facebook status from an R session?
EDIT 1: Reading the responses thus far, I would like to point out that I'm simply interested if a package already exists which provides this functionality, similar to how the lovely twitteR package does for twitter. Also, something doesn't have to be 'useful' in order to be 'fun', which is how I prefer to learn.
Edit 2: Sorry to anyone offended by me by not being more specific in how I asked my question. I have used R informally for 2 months and was told that SO was a nice place to ask questions (yes i have read the intro guide).
NB: The following only successfully logs you into facebook. I don't know why the status update at the end doesn't work, but maybe it is still of some value. It is based on a blog post over at Baratttalo back in March and which I thought would pass time on a friday afternoon.
I wasn't going to reply to this, but looking at some of the other responses and seeing as you helped me over at mathoverflow, I figured I'd give it a shot.
you'll need to install the RCurl and XML packages from http://www.omegahat.org/ (it's a pretty cool website to look at even just for fun i think).
Anyway copy and paste this:
library(RCurl)
library(XML)
log.into.facebook <- function(curl, id) {
curlSetOpt( .opts = list(postfields = paste('email=', URLencode(id$login.email), '&pass=', URLencode(id$login.password), '&login=', URLencode('"Login"'), sep=''),
post = TRUE,
header = FALSE,
followlocation = TRUE,
ssl.verifypeer = FALSE,
cookiejar = 'my_cookies.txt',
cookiefile = 'my_cookies.txt',
useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3'), curl = curl)
u <- "https://login.facebook.com/login.php?m&next=http%3A%2F%2Fm.facebook.com%2Fhome.php"
doc <- getURL(u, curl = curl)
return(doc)
}
get.update.stutus.form.id <- function(curl, doc) {
curlSetOpt( .opts = list(post = FALSE), curl = curl)
doc <- getURL("http://m.facebook.com/home.php" , curl = curl)
html <- htmlTreeParse(doc, useInternal = TRUE)
# this gets the post_form_id value
form.id.node <- getNodeSet(html, '//input[@name="post_form_id"]')
form.id <- sapply(form.id.node, function(x) x <- xmlAttrs(x)[[3]])
# we'll also need the exact name of the form processor page
form.num.node <- getNodeSet(html, '//form[@method="post"]')
form.num <- sapply(form.num.node, function(x) x <- xmlAttrs(x)[[1]])
form.num <- strsplit(form.num, "/")[[1]][3]
return(list(form.id = form.id, form.num = form.num))
}
# This function doesn't work. I would love to know why though as it 'looks' right to me
update.status <- function(doc, curl, id) {
form <- get.update.stutus.form.id (curl, doc)
curlSetOpt( .opts = list(post = TRUE,
postfields = paste('post_form_id=', form$form.id, '&status=', URLencode(id$status), '&update=', URLencode('"Update status"'), sep = '')),
curl = curl)
u <- paste("http://m.facebook.com", form$form.num, sep = "/")
doc <- getURL(u, curl = curl)
return(doc)
}
and here's how you use the functions above (change id values to your log in details)
id <- list()
id$status <- "Hello world!"
id$login.email <- "YOUR LOGIN EMAIL"
id$login.password <- "YOUR LOGIN PASSWORD"
# log into facebook, seems to work fine
curl <- getCurlHandle()
doc <- log.into.facebook(curl, id)
# this is the bit that doesn't work, no idea why though.
update.status(doc, curl, id)
Hope that helps a little bit, maybe it will give you an idea. Also, I think the question you asked is fine, maybe just be a bit more specific next time and so maybe you'll avoid some of the comments you've gotten here :-)
Tony Breyal
P.S. I think there IS an api for all this somewhere, but if all you're interested in is updating the status, I quite like the idea of using the twitteR package and linking the updates to facebook.
I don't think so. It would require building a package to support the Facebook API, and nobody's done that for R. (And, really, why would they? It's not the best tool for the job! And it's not like you can pull large amounts of data from Facebook to do data analysis...)
What you could do is to use the twitteR package, update your status on Twitter, then connect your Twitter and Facebook accounts to get the update into Facebook.
I must admit I would never imagine someone would ask a question like this but.. :)
Use the httpRequest package (http://cran.fiocruz.br/web/packages/httpRequest/index.html) to update your status. It's just a POST. I can't find an example in R but here is an example in PHP - it's not difficult to see what being done: http://fbcookbook.ofhas.in/2009/02/07/facebook-reveals-status-api-how-to-use-it/
Right now (December 2013) it is possible to update Facebook status using R. You only need to use RFacebook package (http://cran.r-project.org/web/packages/Rfacebook/). All You need is to set up everything (here you have tutorial - http://thinktostart.wordpress.com/2013/11/19/analyzing-facebook-with-r/) and after that there is updateStatus function, for example:
updateStatus("Here is my new status", token)
Sure, study the API and create a package.
If your question really was "has anybody already done the work for me?" then the answer may be no.
In response to the comment, the classic "This is R. There is no if. Only how." still applies. Quoting from the fortunes package:
> library(fortunes)
> fortune("Yoda")
Evelyn Hall: I would like to know how (if) I can extract some of the
information from the summary of my nlme.
Simon Blomberg: This is R. There is no if. Only how.
-- Evelyn Hall and Simon 'Yoda' Blomberg
R-help (April 2005)
>
So in short, download the twitteR package, see how it uses the RCurl package to access the Web API and do likewise for Facebook's API. Or pay someone to do it for you.