Update Facebook status using R?

后端 未结 5 2074
礼貌的吻别
礼貌的吻别 2021-02-20 06:27

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 07:06

    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.

提交回复
热议问题