0

さまざまなニュース サイトからコメントを取得し、それを処理しようとしています。たとえば、https://graph.facebook.com/comments/?ids=http://techcrunch.com/2012/01/04/mechanical-engineering-community-grabcad-grabs-4-million/

コメントはありますが、レールとコアラを使用して mysql データベース (または他のデータベース) に保存する方法がわかりません。

ヘルプ/ヒントは大歓迎です

4

2 に答える 2

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
于 2012-05-19T05:36:46.117 に答える
-1

Rubyを使用してJSONを解析できます

data = ActiveSupport::JSON.decode(json_data)

次に、データを繰り返し処理し、ActiveRecordを使用してデータベースに保存します。

data["data"].each do |comment|
  Comment.create!(:message => comment.message, ...)
end
于 2012-01-04T18:14:12.577 に答える