Created
March 7, 2012 00:36
-
-
Save benfulton/1990123 to your computer and use it in GitHub Desktop.
Create an iCal file for your books checked out of Monroe County Public Library
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 "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