Update Facebook status using R?

后端 未结 5 1458
慢半拍i
慢半拍i 2021-02-20 06:21

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

相关标签:
5条回答
  • 2021-02-20 06:59

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

    0 讨论(0)
  • 2021-02-20 07:07

    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.

    0 讨论(0)
  • 2021-02-20 07:07

    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)
    
    0 讨论(0)
  • 2021-02-20 07:14

    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/

    0 讨论(0)
  • 2021-02-20 07:18

    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.

    0 讨论(0)
提交回复
热议问题