Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create an iCal file for your books checked out of Monroe County Public Library
require "icalendar"
require "Mechanize"
include Icalendar
class Libry
def initialize
@agent = Mechanize.new
end
def due_date(row)
row.inner_text.split("Due:")[1].split("<img")[0][1..100]
end
def Logon( user, pw )
a = @agent
a.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
a.get('https://mcpl.monroe.lib.in.us/logon.aspx') do |page|
form = page.forms[0]
form['barcodeOrUsername'] = user
form['password'] = pw
form.submit
end
end
def GetBooks
@agent.get('https://mcpl.monroe.lib.in.us/Mobile/MyAccount/ItemsOut') do |page|
page.search('.list-table-item').each do |row|
anchor = row.at('a')
if (!anchor.nil?)
x = {:title => anchor.inner_text, :duedate => due_date(row)}
yield x
end
end
end
end
end
class Clndr
def initialize
@cal = Calendar.new
end
def Add( book, dueDate )
@cal.event do
dtstart Date.strptime(dueDate, '%m/%d/%Y').to_ical
summary "Library book due: " + book
end
# @cal.Publish
end
def Write()
puts @cal.to_ical
end
end
if ARGV.length != 2
puts "Usage: Book2Calendar username password >books.ics"
exit
end
lib = Libry.new
cal = Clndr.new
lib.Logon( ARGV[0], ARGV[1] )
lib.GetBooks do |book|
cal.Add( book[:title], book[:duedate])
end
cal.Write
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.