ITKGrades.rb: Difference between revisions

From ICO wiki
Jump to navigationJump to search
Anroots (talk | contribs)
No edit summary
Anroots (talk | contribs)
Line 5: Line 5:
<source lang="ruby">
<source lang="ruby">
# ITKGrade usage example
# ITKGrade usage example
#!/usr/bin/env ruby
#!/usr/bin/env ruby


Line 11: Line 10:


# Create instance and save login data
# Create instance and save login data
ITK = ITKGrades.new("myusername", "mypass", my_student_code)
ITK = ITKGrades.new("user", "pass")


# Fetch & scrape grades from ÕIS.
# Fetch & scrape grades from ÕIS.
ITK.fetch
ITK.fetch


# Go over each grade
#ITK.grades # Returns a hash of all grades
ITK.grades.each {|subj, grades|
 
# Print all grades
# Print out only subjects that have at least one grade
ITK.print
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>
</source>



Revision as of 13:02, 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("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.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.