-
Notifications
You must be signed in to change notification settings - Fork 0
/
tpl-rss.rb
executable file
·93 lines (73 loc) · 3.81 KB
/
tpl-rss.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env ruby
#
# == TPL-RSS ==
# <http://github.com/gabrielmansour/tpl-rss>
# Provides an RSS (Atom) Feed of books checked out from the Toronto Public Library
# created by Gabriel Mansour
# based on the Perl script by Sacha Chua <http://sachachua.com/wp/2009/03/29/new-library-reminder-script/>
#
require 'rubygems'
require 'mechanize'
# automatically load personal account credentials from the yaml file
[ [File.dirname(__FILE__), 'tpl.yml'], [ENV['HOME'], '.tpl'] ].each do |path|
if File.exists?( file = File.join(*path) )
creds = YAML::load_file(file) and (@LIBRARY_CARD, @PIN = creds['library_card'], creds['pin']) and break
end
end
# you can also just enter your credentials here if you'd rather keep everything in one file
@LIBRARY_CARD ||= '0000000000000'
@PIN ||= '0000'
@LIBRARY_CARD, @PIN = *ARGV unless ARGV.empty? # ..or optionally, you can pass in your credentials when executing this script from the command line
OUTPUT_FILE = ARGV[2] || 'books.atom' # specify a filename here if you want to save the generated atom feed as a file
LOGIN_URL = 'https://ezproxy.torontopubliclibrary.ca/login'
ACCOUNT_URL = 'http://ezproxy.torontopubliclibrary.ca/sso/myacct'
CATALOGUE_URL = 'http://catalogue.torontopubliclibrary.ca'
ua = WWW::Mechanize.new
ua.read_timeout = 30
response = ua.post(LOGIN_URL, :user => @LIBRARY_CARD, :pass => @PIN, :url => ACCOUNT_URL)
raise "Login Error: Incorrect library card number or PIN." if response.body.match(/Incorrect library card number or PIN. Please try again./)
feed = Nokogiri::XML::Builder.new do
feed( :xmlns => "http://www.w3.org/2005/Atom", :"xml:lang" => "en" ) do |f|
f.title "Library books"
f.subtitle "From the Toronto Public Library"
f.updated Time.now.iso8601
f.link :rel => 'self', :href => ''
f.link :rel => 'alternate', :type => 'text/html', :href => CATALOGUE_URL
f.generator(:uri => 'http://github.com/gabrielmansour/tpl-rss'){ text "TPL-RSS" }
f.id_ "tag:torontopubliclibrary.ca,2008:tpl-rss"
f.icon ""
ua.get(CATALOGUE_URL) do |page|
# Find the Your Account page
# We need to click the link twice because apparently its @href isn't set properly the first time
2.times { page = page.links_with(:text => 'Your Account').first.click }
(page/"tbody#renewcharge tr").each do |book|
book_link = book % "a"
checkbox = book % "input"
unless book_link.nil?
# RENEW^39100049582508^658.84 SWE^1^Sweeney, Susan, 1956-^101 Internet businesses you can start from home : how to choose and build your own successful e-business^
command, catno, callno, status, author, title = *checkbox['name'].to_s.split(/\^/) if checkbox
renew_count = (book/"td:nth-child(3)").text.strip
due = (book/"td:nth-child(4)").text.strip
due_date = Date.strptime(due, '%d/%m/%Y')
due_in = due_date - Date.today
clean_href = book_link['href'].sub(/\/uhtbin\/cgisirsi\/.*\?/, "/uhtbin/cgisirsi/x/0/0/5/3?") # strip out session information from URL
f.entry do
title title
author author
id_ "tag:torontopubliclibrary.ca,2008;#{catno}"
updated Time.now.iso8601
link :rel => 'alternate', :href => "#{CATALOGUE_URL}#{clean_href}#catno#{catno}"
summary(:type => 'html') do
text "Due on <strong>#{due_date.strftime('%d %B %Y')}</strong>"
text " <em>in #{due_in} days</em>"
text "<br /> (renewed #{renew_count} time#{'s' unless renew_count.to_i==1})" unless renew_count.empty?
text "<br /> <small>№ #{catno}</small>"
end
end # ENTRY
end
end
end
end # FEED
end
puts feed.to_xml
File.open(OUTPUT_FILE, 'w') {|f| f.write feed.to_xml } if OUTPUT_FILE