0

レーキスクリプト

desc "Update departments and classes listing"
task :update_classes => :environment do 
require 'rubygems'
require 'mechanize'
require 'nokogiri'
def getSubject(agent, subject)
  #subj_form.field_with(:name=>"SUBJ").options.first.unselect

  page_form = agent.get('https://was.nd.edu/reg/srch/ClassSearchServlet').form()
  select = page_form.field_with(:name => "SUBJ")

  select.select_none()

  pp subject

  #pp select.
  select.options_with(:value => "#{subject}").each do |field|
    field.select
  end

  response = agent.submit(page_form)
  #pp page.body
  results = response.body

  pp "Response submitted succesfully!"

  doc = Nokogiri::HTML(results, 'UTF-8')

  pp "Passed the doc"
  parseHTML(doc, subject)
end

def parseHTML(doc, subject)
  #Find nodes by XPATH
  pp "Enter the parseHTML function"
  rows = doc.xpath('//table[@id="resulttable"]/tbody/tr')

  pp "Trying to find department by key . . . "

  #@department = Department.where(:key => subject)

  pp "Success!"

  #cells = rows.css('td')
  details = rows.collect do |row|
          detail = {}
          [
              [:subject, subject],
                  [:course, 'td[1]/a[1]'],
                  [:section, 'td[1]/a[1]'],
                  [:title, 'td[2]'],
                  [:credits, 'td[3]'],
                  [:status, 'td[4]'],
                  [:max_spots, 'td[5]'],
                  [:open_spots, 'td[6]'],
                  [:xlst, 'td[7]'],
                  [:crn, 'td[8]'],
                  [:syl, 'td[9]'],
                  [:instructor, 'td[10]/a'],
                  [:when, 'td[11]'],
                  [:begin, 'td[12]'],
                  [:end, 'td[13]'],
                  [:location, 'td[14]'],
          ].each do |name, xpath|
              # Want to loop through each cell/detail, then perform actions accordingly
              if name == :course
                    detail[name] = row.xpath(xpath).text.strip.split('-').first.strip
                    #pp detail[name]
                elsif name == :section
                    detail[name] = row.xpath(xpath).text.strip.split('-').last.strip
                    #pp detail[name]
                  elsif name == :subject
                    detail[name] = subject
                else
                    detail[name] = row.xpath(xpath).text.strip
                    #pp detail[name]              
              end         
          end
          detail
  end

  #pp details
  # Let's build our Courses array!

  prev = ''
  #c = Course.new(nil, nil, nil)
  details.each do |d|
    #temp = Course.new(d[:title], d[:course], d[:credits])

    #pp prev
    #unless @courses.empty?

    #Check if the course number already exists in the array
    if d[:course] == prev
        #loc = @courses.find_index{|course| course.name == d[:title]}

      #@course = Course.where(:coursenum => d[:course]).first
     # s = Section.new(d[:section], d[:open_spots], d[:max_spots], d[:crn], d[:instructor], d[:when], d[:location])
      #c.add(s)
      #@section = Section.new(:num => d[:section], :opspots => d[:open_spots], :maxspots => d[:max_spots], :crn => d[:crn], :instructor => d[:instructor], :time => d[:when], :location => d[:location])

      @section = @course.sections.new(:num => d[:section], :opspots => d[:open_spots], :maxspots => d[:max_spots], :crn => d[:crn], :instructor => d[:instructor], :time => d[:when], :location => d[:location])
      @section.save
      @course.save
      @dept.save
      #@department.save
    else
      #c = Course.new( d[:title], d[:course], d[:credits])
      #s = Section.new(d[:section], d[:open_spots], d[:max_spots], d[:crn], d[:instructor], d[:when], d[:location])
      ##c.add(s)
      #add(c)

      #@course = Course.new( :name => d[:title], :coursenum => d[:course], :credit => d[:credits])
      #@dept.save
      @course = @dept.courses.new(:name => d[:title], :coursenum => d[:course], :credit => d[:credits])
      #@course.sections.build(:course_id => @course.id)
      #@section = Section.new(:num => d[:section], :opspots => d[:open_spots], :maxspots => d[:max_spots], :crn => d[:crn], :instructor => d[:instructor], :time => d[:when], :location => d[:location])
      #@section.save
      @section = @course.sections.new(:num => d[:section], :opspots => d[:open_spots], :maxspots => d[:max_spots], :crn => d[:crn], :instructor => d[:instructor], :time => d[:when], :location => d[:location])
      @section.save
      #@course.save
      @course.save
      @dept.save

    end

    unless d[:course] == nil
      prev = d[:course]
    end

  end
  #exportHash(subject, @courses)

  #@courses.each do |co|
  # pp co
  #end
end

#def exportHash(subject, courselist)
 # File.open("courses/#{subject}.yml", File::WRONLY|File::CREAT) do |file|
  #  file.write courselist.to_yaml
  #end
#end
#end

a = Mechanize.new
page = a.get('https://was.nd.edu/reg/srch/ClassSearchServlet')

subj_form = page.form()

options = subj_form.field_with(:name=>"SUBJ").options

options.each { |op|
  @dept = Department.new(:deptname => op.text.strip, :key => op.value.strip)
  @dept.save

  getSubject(a, op)
  #pp op.text.strip
  #getSubject(a, op)
}
end

モデル、部門、コース、およびセクションのすべて。

class Course < ActiveRecord::Base
  belongs_to :department
  attr_accessor :department_id
  attr_accessible :coursenum, :credit, :name, :course_id, :department

  validates :coursenum, :presence => true, :uniqueness => true
  validates :credit, :presence => true
  validates :name, :presence => true

  has_many :sections, :autosave => true
  accepts_nested_attributes_for :sections


end

class Department < ActiveRecord::Base
  attr_accessible :deptname, :key

  validates :key, :presence => true, :uniqueness => true
  validates :deptname, :presence => true, :uniqueness => true

  has_many :courses, :autosave => true
  accepts_nested_attributes_for :courses

end

class Section < ActiveRecord::Base
  attr_accessible :crn, :instructor, :location, :maxspots, :num, :opspots, :time

  validates :crn, :presence => true, :uniqueness => true
  validates :instructor, :presence => true
  validates :location, :presence => true
  validates :maxspots, :presence => true
  validates :num, :presence => true
  validates :opspots, :presence => true
  validates :time, :presence => true

  belongs_to :course
  attr_accessor :course_id

  #validates :num, :presence => true, :uniqueness => true
end

エラーはスローされませんが、データが保存されていない理由がわかりません。コードが示すように、Mechanize/Nokogiri を使用して、クラスとセクションのフォーム オートメーションの HTML ページを解析していますが、新しいコースが正しい部門に保存されていないようです。. . そして、私はこれに何時間も立ち往生しており、気が狂っています。

4

0 に答える 0