Created
April 8, 2014 23:16
-
-
Save gaurish/10207446 to your computer and use it in GitHub Desktop.
rtu result analysis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'student.rb' | |
require 'pry' | |
require 'httparty' | |
require 'logger' | |
require 'csv' | |
URL = 'URL_HERE' | |
rollno_prefix, = "11CIIXX" | |
rollno_range = 600..656 | |
subjects = [] | |
options = { | |
headers: { | |
"User-Agent" => "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.60 Safari/537.17" | |
}, | |
body: { | |
opflag: '', | |
prtext: '1T1bg1g1U1e1b1h1T1Ug1U1b1Z1R1c1N1T1R', | |
clscode: '50003', | |
} | |
} | |
log = Logger.new 'activity.log', 'weekly' | |
log.level = Logger::DEBUG | |
rollno_range.each do |i| | |
rollno = rollno_prefix + i.to_s | |
options[:body][:rollno] = rollno | |
log.debug "making network request to #{URL} with following options" | |
log.debug options.inspect | |
response = HTTParty.post(URL, options) | |
log.info "Got #{response.code} - #{response.message} for #{rollno}" | |
student = Student.new(response.body) | |
student.parse(subjects) | |
result = { | |
name: student.name, | |
rollno: student.rollno, | |
marks: student.marks, | |
grand_total: student.grand_total, | |
result: student.result | |
} | |
log.debug " result: #{result.inspect}" | |
CSV.open("resultdata.csv", "a") do |csv| | |
csv << result.values | |
end | |
# sleep(2) | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'nokogiri' | |
class Student | |
attr_accessor :name, :marks, :grand_total, | |
:rollno, :html, :result, :doc | |
def initialize(html) | |
@html = html | |
end | |
def parse(subjects) | |
set_details | |
set_marks(subjects) | |
rescue Exception | |
STDERR.puts "Failed to parse html: #{$!}" | |
end | |
private | |
def select_details(path) | |
@doc = Nokogiri::HTML(@html) | |
els = doc.search("[text()*='#{path}']") | |
el = els.first | |
el.parent.parent.next_element.text.strip | |
end | |
def set_details | |
@name = select_details 'Name' | |
@grand_total = select_details 'Grand Total' | |
@result = select_details 'RESULT' | |
@rollno = select_details 'Roll No.' | |
end | |
def set_marks(subjects) | |
@marks = {} | |
subjects.each do |subject| | |
@doc = Nokogiri::HTML(@html) | |
els = doc.search("[text()*='#{subject}']") | |
el = els.first | |
print el.nil? | |
puts subject | |
@marks["#{subject.strip}"] = { | |
theory: el.parent.next_element.text.strip.to_i, | |
practical: el.parent.next_element.next_element.text.strip.to_i, | |
total: el.parent.next_element.next_element.next_element.text.strip.to_i | |
} | |
# binding.pry | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment