ITKGrades.rb: Difference between revisions
From ICO wiki
Jump to navigationJump to search
Created page with 'Tegemist on Ruby klassiga, mis tagastab http://ruby-doc.org/core-1.9.3/Hash.html Hash'i õpilase hinnetega. = Kasutus = = Kood = Vaata koodi GitHub keskkonnas: https://gis…' |
No edit summary |
||
Line 1: | Line 1: | ||
Tegemist on Ruby klassiga, mis tagastab [[http://ruby-doc.org/core-1.9.3/Hash.html Hash]]'i | Tegemist on Ruby klassiga, mis tagastab [[http://ruby-doc.org/core-1.9.3/Hash.html Hash]]'i üliõpilase hinnetega. Hinnete saamiseks logib skript mechanizer library abiga ÕISi ning otsib hinded hinnetelehe HTML-st välja. | ||
= Kasutus = | = Kasutus = | ||
<source lang="ruby"> | |||
# ITKGrade usage example | |||
#!/usr/bin/env ruby | |||
require "itk_grades" | |||
# Create instance and save login data | |||
ITK = ITKGrades.new("myusername", "mypass", my_student_code) | |||
# Fetch & scrape grades from ÕIS. | |||
ITK.fetch | |||
# Go over each grade | |||
ITK.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 | |||
} | |||
</source> | |||
= Kood = | = Kood = | ||
Line 117: | Line 145: | ||
= Autor = | = Autor = | ||
Koodi kirjutas [[User:Anroots]] | Koodi kirjutas [[User:Anroots]] 2012 veebruaris, sest ta arvas, et uutest hinnetest oleks tore näiteks [https://pood.tele2.ee/et/services/545 SMS teavitusi] saada. |
Revision as of 00:15, 1 March 2012
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("myusername", "mypass", my_student_code)
# Fetch & scrape grades from ÕIS.
ITK.fetch
# Go over each grade
ITK.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
}
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.0
# 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", MY_STUDENT_CODE) # Instance and login
# ITK.fetch # Actually fetch and scrape the grades
# pp ITK.grades # Print out the hash of my grades
#
class ITKGrades
@@login_url = "https://itcollege.ois.ee/et/"
@username = nil
@password = nil
@student_id = nil
@grades_url = nil
@grades = nil
# Accessor for grades
def grades
@grades
end
# Save login data
def initialize(username, password, student_id)
@username = username
@password = password
@student_id = student_id
@grades_url = "https://itcollege.ois.ee/et/grade?student_id=" + @student_id.to_s
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.get(@grades_url)
end
# Scrape grades into grades hash from the grades page
def scrape_grades(page)
@grades = {}
grade_rows = page.search('#mainWindow table.data tr')
grade_rows.each do |row|
if row.at_css('th') then
@grades[row.search('th').text] = {}
next
end
paper = row.search('td:first').text.gsub(/\t/, '').gsub("-","").strip
grade = row.search('td:last').text.gsub(/\t/, '').gsub("-","").strip
if paper.empty? || grade.empty? then
next
end
subjects = @grades.keys
current_subject = subjects[subjects.length-1]
@grades[current_subject][paper] = grade
end
end
# Fetch grades. Call this first!
def fetch
scrape_grades(get_grades_page)
return @grades
end
end
Autor
Koodi kirjutas User:Anroots 2012 veebruaris, sest ta arvas, et uutest hinnetest oleks tore näiteks SMS teavitusi saada.