我正在尝试从不同的新闻网站上获取评论并对其进行一些处理。例如,https://graph.facebook.com/comments/?ids=http://techcrunch.com/2012/01/04/mechanical-engineering-community-grabcad-grabs-4-million/
我有意见,但我不知道如何使用 rails 和 koala 将其存储到 mysql 数据库(或任何其他数据库)。
非常感谢任何帮助/提示
我正在尝试从不同的新闻网站上获取评论并对其进行一些处理。例如,https://graph.facebook.com/comments/?ids=http://techcrunch.com/2012/01/04/mechanical-engineering-community-grabcad-grabs-4-million/
我有意见,但我不知道如何使用 rails 和 koala 将其存储到 mysql 数据库(或任何其他数据库)。
非常感谢任何帮助/提示
查看下面的代码:
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
您可以使用 Ruby 解析 JSON
data = ActiveSupport::JSON.decode(json_data)
然后简单地遍历数据并使用 ActiveRecord 将其存储在数据库中
data["data"].each do |comment|
Comment.create!(:message => comment.message, ...)
end