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 |
||
(3 intermediate revisions by the same user not shown) | |||
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 = | ||
[[File: Menu 012.png|Näide ühest võimalikust implementatsioonist]] | |||
<source lang="ruby"> | |||
# 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 | |||
</source> | |||
= Kood = | = Kood = | ||
Line 14: | Line 34: | ||
# Maybe prettify in the future. | # Maybe prettify in the future. | ||
# | # | ||
# Version 1. | # Version 1.1 | ||
# Author "Ando Roots" <anroots@itcollege.ee> | # Author "Ando Roots" <anroots@itcollege.ee> | ||
# Created 29.02.2012 | # Created 29.02.2012 | ||
Line 33: | Line 53: | ||
# Example usage: | # Example usage: | ||
# | # | ||
# ITK = ITKGrades.new("MY_USERNAME", "MY_PASSWORD" | # ITK = ITKGrades.new("MY_USERNAME", "MY_PASSWORD") # Instance and login | ||
# ITK.fetch # Actually fetch and scrape the grades | # ITK.fetch # Actually fetch and scrape the grades | ||
# | # ITK.print # Print out the hash of my grades | ||
# | # | ||
class ITKGrades | class ITKGrades | ||
Line 41: | Line 61: | ||
@@login_url = "https://itcollege.ois.ee/et/" | @@login_url = "https://itcollege.ois.ee/et/" | ||
@username | @username | ||
@password | @password | ||
@grades | |||
@grades | |||
# Accessor for grades | # Accessor for grades | ||
Line 55: | Line 72: | ||
# Save login data | # Save login data | ||
def initialize(username, password | def initialize(username, password) | ||
@username = username | @username = username | ||
@password = password | @password = password | ||
end | end | ||
Line 75: | Line 90: | ||
page = agent.submit(login_form, login_form.button) | page = agent.submit(login_form, login_form.button) | ||
return agent. | return agent.page.link_with(:text => 'Minu hinded').click | ||
end | end | ||
Line 83: | Line 98: | ||
@grades = {} | @grades = {} | ||
grade_rows = page.search('#mainWindow table.data tr') | grade_rows = page.search('#mainWindow table.data tr') | ||
current_subject = nil; | |||
grade_rows.each do |row| | grade_rows.each do |row| | ||
if row.at_css('th') then | if row.at_css('th') then | ||
current_subject = row.search('th').text | |||
@grades[current_subject] = {} | |||
next | next | ||
end | end | ||
paper = row.search('td:first').text.gsub(/\t/, '' | paper = row.search('td:first').text.gsub(/[-\t]/, '').strip | ||
grade = row.search('td:last').text.gsub(/\t/, '' | grade = row.search('td:last').text.gsub(/[-\t]/, '').strip | ||
if paper.empty? || grade.empty? then | if paper.empty? || grade.empty? then | ||
next | next | ||
end | end | ||
@grades[current_subject][paper] = grade | @grades[current_subject][paper] = grade | ||
Line 110: | Line 123: | ||
scrape_grades(get_grades_page) | scrape_grades(get_grades_page) | ||
return @grades | 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 | ||
Line 117: | Line 152: | ||
= 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. |
Latest revision as of 00:01, 3 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("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.