我的Rails(3.1)应用程序中有以下Nokogiri rake任务:
desc "Import incoming calls"
task :fetch_incomingcalls => :environment do
# Logs into manage.dial9.co.uk and retrieved list of incoming calls.
require 'rubygems'
require 'mechanize'
require 'logger'
# Create a new mechanize object
agent = Mechanize.new
# Load the dial9 website
page = agent.get("https://manage.dial9.co.uk/login")
# Select the first form
form = agent.page.forms.first
form.username = 'username
form.password = 'password'
# Submit the form
page = form.submit form.buttons.first
# Click on link called Call Logs
page = agent.page.link_with(:text => "Call Logs").click
# Click on link called Incoming Calls
page = agent.page.link_with(:text => "Incoming Calls").click
# Output results to file
# output = File.open("output.html", "w") { |file| file << page.search("tbody td").text.strip }
# Add each row to a new call record
page = agent.page.search("table tbody tr").each do |row|
next if (!row.at('td'))
time, source, destination, duration = row.search('td').map{ |td| td.text.strip }
Call.create!(:time => time, :source => source, :destination => destination, :duration => duration)
end
end
时间值是表中的第一行,每次调用都是唯一的(因为我们一次只能接收一个调用)。
我想做的是使用时间值作为我的呼叫日志的唯一标识符。
因此,当抓取屏幕时,它会"更新"现有的调用(这不会改变,但这是我唯一能想到的只导入新调用的方法)。
如果我将其设置为:
Call.find_all_by_time(nil).each do |call|
然后:
call.update_attribute(:time, time)
然后它将更新现有记录,但我希望它根据时间值导入数据库中尚未存在的记录。
感谢您的帮助!
你是这个意思吗?
# Add each row to a new call record
page = agent.page.search("table tbody tr").each do |row|
next if (!row.at('td'))
time, source, destination, duration = row.search('td').map{ |td| td.text.strip }
call = Call.find_or_create_by_time(time)
call.update_attributes({:time => time, :source => source, :destination => destination, :duration => duration})
end