Skip to content

Instantly share code, notes, and snippets.

@Koronen
Last active February 27, 2016 10:50
Show Gist options
  • Save Koronen/90990eb76ec64f78d1ba to your computer and use it in GitHub Desktop.
Save Koronen/90990eb76ec64f78d1ba to your computer and use it in GitHub Desktop.
KTH-snittet
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'nokogiri', '~> 1.6'
end
require 'csv'
require 'date'
require 'nokogiri'
Result = Struct.new(:code, :name, :credits, :grade, :date)
File.open('resultat.html') do |f|
doc = Nokogiri::HTML(f)
clr = doc.at_css('table#courselistresults')
results = clr.css('tr:not(.grayout):not(.listingHeader)').map do |r|
next unless r.at_css('td:nth-child(4)').text.empty?
code = r.at_css('td:nth-child(1)').text
name = r.at_css('td:nth-child(2)').text
credits = r.at_css('td:nth-child(3)').text
grade = r.at_css('td:nth-child(5)').text
date = r.at_css('td:nth-child(7)').text
Result.new(code, name, credits.to_f, grade, Date.parse(date))
end
results = results.compact.sort_by(&:date)
CSV.open('snitt.csv', 'w') do |csv|
csv << ['date', 'gpa']
numerator = denominator = 0
results.each do |r|
next if r.grade == 'P'
p = case r.grade
when 'A'
5
when 'B'
4.5
when 'C'
4
when 'D'
3.5
when 'E'
3
else
raise ArgumentError.new("Unexpected grade \"#{r.grade}\"")
end
numerator += p * r.credits
denominator += r.credits
gpa = numerator / denominator
csv << [r.date, gpa]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment