使用R更新Facebook状态?

Cla*_*ton 11 facebook r

是否可以从R会话更新我的Facebook状态?

编辑1:阅读到目前为止的回复,我想指出,我只是感兴趣,如果已经存在提供此功能的包,类似于可爱的twitteR包为twitter做的.此外,为了"有趣",某些东西不一定是"有用的",这是我喜欢学习的方式.

编辑2:对于任何被我冒犯的人抱歉,因为我没有更具体地提出我的问题.我已经非正式使用了R 2个月,并被告知SO是一个提问的好地方(是的,我已经阅读了介绍指南).

Ton*_*yal 10

注意:以下只能成功登录到facebook.我不知道为什么最后的状态更新不起作用,但也许它仍然有一些价值.它基于3月份在Baratttalo的一篇博客文章,我认为这将在周五下午消磨时间.

我不打算回答这个问题,但看看其他一些回复并看到你在mathoverflow上帮助我,我想我会试一试.

你需要安装来自http://www.omegahat.org/的RCurl和XML软件包(这是一个非常酷的网站,我认为它只是为了好玩).

无论如何复制并粘贴这个:

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)
}
Run Code Online (Sandbox Code Playgroud)

以下是您使用上述功能的方法(将ID值更改为您的日志详细信息)

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)
Run Code Online (Sandbox Code Playgroud)

希望有所帮助,也许它会给你一个想法.另外,我认为你问的问题很好,下次可能会更具体一点,所以也许你会避免一些你在这里得到的评论:-)

Tony Breyal

PS我觉得这个地方有一个api,但如果你感兴趣的是更新状态,我非常喜欢使用twitteR包并将更新链接到facebook.


Dir*_*tel 1

当然,研究 API 并创建一个包。

如果您的问题确实是“有人已经为我完成了这项工作吗?” 那么答案可能是否定的。

针对该评论,经典的“这就是 R。没有如果。只有如何。 ”仍然适用。引用fortunes包中的内容:

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

> 
Run Code Online (Sandbox Code Playgroud)

简而言之,下载twitteR包,看看它如何使用RCurl包访问 Web API,并对 Facebook 的 API 执行同样的操作。或者花钱找人帮你做。

  • @Dirk Eddelbuettel 恕我直言 - 我不想以任何方式听起来冒犯 - 但这样的答案如何帮助某人?我无法想象为什么有人对这个答案投赞成票。 (8认同)
  • @Vitor:IMO,建议某人研究 API 并创建一个包是很好的建议。这是开源的。有时你必须自己做。问题是“这可能吗”。 (3认同)
  • @Clair:他回答了这个问题:答案似乎是“不,包不存在”(据我们所知),这是一个有用的回应。一旦答案是“不”,那么问题就变成了下一步是什么?对我来说,下一个逻辑步骤是“让我们创建一个”,其中“我们”是任何想要使用它的人(我不将自己包括在该集合中)。 (3认同)
  • 通常,无论您用什么语言提出问题,您都会得到答案。如果您正在寻找一个包来执行“XYZ”,那么最好实际询问该包,因为作为一个整体,程序员是一群尖酸刻薄的人,有时我们只是喜欢模仿我们的计算机。 (3认同)
  • @Dirk @Vitor 我想如果有人问一个问题,询问某种语言“可能”的情况,答案总是“阅读文档”或“自己写”。英语是我的第三语言,但我尽了最大努力,但我不认为这是一个有用的回应。 (2认同)
  • 作为一个新的 R 用户,我发现 Dirk 的评论很有帮助。我了解到 R 的可扩展性不仅是因为命令行界面,还因为用户能够编写包并从社区获取反馈/帮助。 (2认同)