require 'mongo' require 'open-uri' require 'json' require 'date' @db = Mongo::Connection.new("localhost", 27017).db("fgu_orbit") @coll_bulletin = @db["bulletins"] @coll_cat = @db["bulletin_categories"] @categories = @coll_cat.find().to_a.inject({}) do |categories, category| categories[category['key']] = category['_id'] categories end def get_remote_bulletins_json(url) data = "" open(url){|f| f.each_line {|line| data << line} } bulletins = JSON.parse data bulletins.each do |b| bulletin = { _type: "Bulletin", postdate: Time.now, created_at: Time.now, updated_at: Time.now, is_checked: true, is_pending: false, is_rejected: false, bulletin_category_id: @categories["#{b[7]}"], title: {:zh_tw => b[2]}, text: {:zh_tw => b[5]}, available_for_zh_tw: true, available_for_en: false, create_user_id: "", public: true, sn: b[0], site: b[9], is_top: false, is_hot: false, is_hidden: false } unless @coll_bulletin.find_one(:$and => [{ :sn => bulletin[:sn]},{ :site => bulletin[:site]}]) @coll_bulletin.save(bulletin) # puts "Inserted" else # puts "Exist" end end end get_remote_bulletins_json("http://president.fgu.edu.tw/remote_annoucement/get_news.php") get_remote_bulletins_json("http://car.fgu.edu.tw/remote_annoucement/get_news.php")