ITKGrades.rb
From ICO wiki
Jump to navigationJump to search
Tegemist on Ruby klassiga, mis tagastab [Hash]'i üliõpilase hinnetega. Hinnete saamiseks logib skript mechanizer library abiga ÕISi ning otsib hinded hinnetelehe HTML-st välja.
Kasutus
# ITKGrade usage example
#!/usr/bin/env ruby
require "itk_grades"
# Create instance and save login data
ITK = ITKGrades.new("user", "pass")
# Fetch & scrape grades from ÕIS.
ITK.fetch
#ITK.grades # Returns a hash of all grades
# Print all grades
ITK.print
Kood
Vaata koodi GitHub keskkonnas: https://gist.github.com/1944799
#!/usr/bin/env ruby
# Estonian IT College grade sheet scraper
# A dirty version for just getting the job done.
# Maybe prettify in the future.
#
# Version 1.1
# Author "Ando Roots" <anroots@itcollege.ee>
# Created 29.02.2012
# Licence Apache v2 http://www.apache.org/licenses/LICENSE-2.0.html
require "rubygems"
require "mechanize"
# Scrapes student grades from ITK ÕIS
# and returns a hash in the format
# {
# subject_name => {
# grade_name => grade
# },
# }
#
# Example usage:
#
# ITK = ITKGrades.new("MY_USERNAME", "MY_PASSWORD") # Instance and login
# ITK.fetch # Actually fetch and scrape the grades
# ITK.print # Print out the hash of my grades
#
class ITKGrades
@@login_url = "https://itcollege.ois.ee/et/"
@username
@password
@grades
# Accessor for grades
def grades
@grades
end
# Save login data
def initialize(username, password)
@username = username
@password = password
end
# Get HTML for the grades page
def get_grades_page
agent = Mechanize.new
page = agent.get(@@login_url)
login_form = page.form
login_form.username = @username
login_form.pw = @password
page = agent.submit(login_form, login_form.button)
return agent.page.link_with(:text => 'Minu hinded').click
end
# Scrape grades into grades hash from the grades page
def scrape_grades(page)
@grades = {}
grade_rows = page.search('#mainWindow table.data tr')
current_subject = nil;
grade_rows.each do |row|
if row.at_css('th') then
current_subject = row.search('th').text
@grades[current_subject] = {}
next
end
paper = row.search('td:first').text.gsub(/[-\t]/, '').strip
grade = row.search('td:last').text.gsub(/[-\t]/, '').strip
if paper.empty? || grade.empty? then
next
end
@grades[current_subject][paper] = grade
end
end
# Fetch grades. Call this first!
def fetch
scrape_grades(get_grades_page)
return @grades
end
# Print fetched grades
def print
if (@grades.length == 0)
print "No grades"
end
# Go over each grade
@grades.each {|subj, grades|
# Print out only subjects that have at least one grade
if grades.length > 0 then
puts "== " + subj + " ==\n" # Print subj name
# For each grade in that subject...
grades.each {|task,grade|
puts "\t#{task}: #{grade}\n"
}
end
}
end
end
Autor
Koodi kirjutas User:Anroots 2012 veebruaris, sest ta arvas, et uutest hinnetest oleks tore näiteks SMS teavitusi saada.