Created
April 30, 2016 19:11
-
-
Save hejmsdz/36563a9d80ffb6b438c5d561027403a5 to your computer and use it in GitHub Desktop.
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 "json" | |
require "mechanize" | |
def results | |
agent = Mechanize.new | |
credentials = { | |
pesel: ENV['PESEL'], | |
password: ENV['CKE_PASSWORD'] | |
} | |
# subject names simplified | |
subjects = { | |
"Język angielski część ustna" => :angU, | |
"Język angielski część pisemna poziom podstawowy" => :angPP, | |
"Język polski część ustna" => :polU, | |
"Język polski część pisemna poziom podstawowy" => :polPP, | |
"Matematyka część pisemna poziom podstawowy" => :matPP, | |
"Fizyka część pisemna poziom rozszerzony" => :fiz, | |
"Informatyka część pisemna poziom rozszerzony" => :inf, | |
"Język angielski część pisemna poziom rozszerzony" => :angPR, | |
"Matematyka część pisemna poziom rozszerzony" => :matPR | |
} | |
results = {} | |
page = agent.get("http://wyniki.oke.gda.pl/") | |
puts "Connected to OKE" | |
form = page.form | |
form.uzytkownik = credentials[:pesel] | |
form.pwd = credentials[:password] | |
page = agent.submit(form) | |
div = page.search(".sesja_opis") | |
raise "no divs" unless div.any? | |
div = div[0].next | |
puts "Loaded the result page" | |
# scrap the page | |
while div = div.next | |
break if div.attr("class") == "sesja_opis" | |
spans = div.search("span") | |
next unless spans.any? | |
subj = spans[0].content | |
res = spans[1].content | |
res = if res.empty? then nil else res.to_i end | |
subj_sym = subjects[subj] | |
if results.has_key?(subj_sym) | |
if results[subj_sym].kind_of?(Array) | |
results[subj_sym] << res | |
else | |
results[subj_sym] = [results[subj_sym], res] | |
end | |
else | |
results[subj_sym] = res | |
end | |
end | |
results | |
end | |
def sms(phone, message) | |
agent = Mechanize.new | |
agent.post("https://ssl.smsapi.pl/sms.do", { | |
"username" => ENV['SMSAPI_USER'], | |
"password" => ENV['SMSAPI_PASS'], | |
"from" => "ECO", | |
"to" => phone, | |
"message" => results | |
}) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment