require "csv" require "will_paginate" class BulkVideoImport < ActiveRecord::Base # This script will import videos from a CSV file into the database. # Any fields present in the video object without a corresponding value # in the file will be filled with a NULL or 0 value. # # Usage: # - First modify the 'test' and 'filename' variables below to point to your # input file. If test is true, the database will not actually be changed, # the script will just validate your input file. Once the script passes, # change 'test' to false and run it again to push changes to the db # # root@foo video_index]# script/runner -e production ./app/BulkVideoImport.rb # # The format of the CSV file is as follows # filename,codec,name,high_def,bitrate,file_size,video_length test = true filename = "/tmp/vid_test.csv" if !File.exist?(filename) return end puts "Reading entries from ", filename results = CSV.read(filename) results.each do |r| vid = Video.new vid.filename = r[0] vid.codec = Codec.find_by_codec_name(r[2]) vid.name = r[3] vid.high_def = r[4] vid.bitrate = r[5].to_i vid.file_size = r[7].to_i vid.video_length = r[8].to_i if !vid.valid? puts "Error on entry ", vid.filename vid.errors.each_full { |msg| puts msg } puts "------","" else if !test if !vid.save puts "Error saving ", vid.filename else puts "+ Saving ", vid.filename end end end end end