使用Rails和Koala从站点检索注释

时间:2012-01-04 17:58:47

标签: ruby-on-rails facebook comments koala

我正在尝试从不同的新闻网站收集评论并使用它进行一些处理。例如,https://graph.facebook.com/comments/?ids=http://techcrunch.com/2012/01/04/mechanical-engineering-community-grabcad-grabs-4-million/

我有评论,但我不知道如何使用rails和考拉将其存储到mysql数据库(或任何其他数据库)。

非常感谢任何帮助/提示

2 个答案:

答案 0 :(得分:0)

查看以下代码:

url_commented_on = "http://techcrunch.com/2012/01/04/mechanical-engineering-community-grabcad-grabs-4-million/"
fb_comments_url = "https://graph.facebook.com/comments/?ids="+url_commented_on
fb_comments_json = open(fb_comments_url).read
fb_comments_data = ActiveSupport::JSON.decode(fb_comments_json)

# If you want to play around, this gets you the first comment
fb_comments_data[url_commented_on]["comments"]["data"].first
# and this gets you the first comment's message
fb_comments_data[url_commented_on]["comments"]["data"].first["message"]

# You could save them in your db like this:
fb_comments = fb_comments_data[url_commented_on]["comments"]["data"]
fb_comments.each do |fb_comment|
  # Your comment Model here
  fb_commenter_name = fb_comment["from"]["name"]
  fb_comment_message = fb_comment["message"]
  fb_comment_creation_date = fb_comment["created_time"]
  # Your comment Model
  comment = CommentModel.new
  comment.author = fb_commenter_name
  comment.comment_text = fb_comment_message
  # Don't overwrite your own "created_at" attribute
  # Instead, use an additional column in the db for storing the date fb gives you
  comment.fb_created_at = fb_comment_creation_date 
  comment.save!
end

答案 1 :(得分:-1)

您可以使用Ruby

解析JSON
data = ActiveSupport::JSON.decode(json_data)

然后简单地迭代数据并使用ActiveRecord

将其存储在数据库中
data["data"].each do |comment|
  Comment.create!(:message => comment.message, ...)
end