tags:

views:

232

answers:

4

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

+1  A: 

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.

Dirk Eddelbuettel
+1... but in the meantime I think you can use the emacs script (assuming you're using R with emacs): http://wiki.developers.facebook.com/index.php/User:Emacs_Lisp
richardh
@Dirk Eddelbuettel With all due respect - I don't want to sound offensive in any way - but how an answer like this helps someone? I can't imagine why someone upvoted this answer.
Vitor Py
@Vitor: IMO, advising someone to study the API and create a package is excellent advice. This is open source. Sometimes you have to do it yourself. And the question was "is it possible".
Shane
@Dirk @Vitor I suppose the answer will always be "read the documentation" or "write it yourself" if someone asks a question asking what is 'possible' with a language. English is my 3rd language, but I try my best, however I do not think that it is a helpful response.
Clair Crossupton
@Clair: He answered the question: the answer appears to be "no, a package does not exist" (so far as we can tell), which is a helpful response. Once the answer is "no", then the question becomes what next? To me, the next logical step is "let us create one", where "us" is anyone who would want to use this (I don't include myself in that set).
Shane
Usually you'll get answers to the questions you ask, regardless of what language you ask them in. If you're looking for a package to do `XYZ` then it's best to actually ask about the package, because as a whole, programmers are a snarky bunch, and sometimes we just like to emulate our computers.
Wayne Werner
As a new R user I found Dirk's comment helpful. I am learning that R is extensible not only because of the command line interface, but because of a user's ability to write packages and get feedback/help from the community.
richardh
Ok, I apologise to anyone I offended by not being more specific with my question. I have added an edit on my post to hopefully make things nicer...
Clair Crossupton
No apologies necessary -- you simply got what you asked for :) More specific questions get more specific answers.
Dirk Eddelbuettel
+3  A: 

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.

Harlan
A script could update its progess to you (and all your friends), possbily useful if you set one going and not sure when to return...
James
+3  A: 

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/

Vitor Py
Someone just asked about POST from R, and one can do that with RCurl. So maybe this really turns into a one-liner...
Dirk Eddelbuettel
+8  A: 

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&amp;amp;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.

Tony Breyal

related questions