Last active
December 25, 2015 21:59
-
-
Save timrogers/7045975 to your computer and use it in GitHub Desktop.
Automatically renew library books from the London School of Economics (LSE) library from a Ruby script with the LSE Renew-o-matic
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
# LSE Renew-o-matic | |
# Renew books you've taken out from the library at LSE (the British Library of Political | |
# and Economic Science) just by running a Ruby script | |
# | |
# Step 1: Install the gem dependencies | |
# $ gem install terminal-table mechanize | |
# Step 2: Run, and you'll get a beautiful table at the end with the results! | |
require 'terminal-table' | |
require 'mechanize' | |
require 'json' | |
USERNAME = "username" | |
PASSWORD = "password" | |
agent = Mechanize.new | |
puts "* Heading to the login page" | |
page = agent.get("https://catalogue.lse.ac.uk/MyResearch/Home") | |
form = page.form('loginForm') | |
form.username = USERNAME | |
form.password = PASSWORD | |
puts "* Logging in with provided username and password" | |
agent.submit(form) | |
puts "* Renewing books" | |
json = agent.get("https://catalogue.lse.ac.uk/MyResearch/Renew").body | |
puts "* Parsing renewal response" | |
results = JSON.parse(json) | |
rows = [] | |
puts "* Building results output" | |
results["items"].each do |result| | |
renewal_status = result["wasRenewed"] == true ? "Yes" : "No" | |
due_date = "#{result["dueDate"]["DD-MM-YY"]} #{result["dueDate"]["HH:MM"]}" | |
rows << [result["barcode"], renewal_status, due_date] | |
end | |
puts "* Ta da!" | |
puts "" | |
table = Terminal::Table.new headings: %w{Barcode Renewed? Due}, rows: rows | |
puts table |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment