Created
April 10, 2020 11:02
-
-
Save JohnMertz/bf4ce8fc9b7cd03a8341925f400eab5e to your computer and use it in GitHub Desktop.
Script to fetch Holds, Checkouts and Fines for an account with the Ottawa 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
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use WWW::Mechanize; | |
use JSON::XS; | |
use HTML::TreeBuilder; | |
my $pretty = 0; | |
foreach (@ARGV) { | |
if ($_ eq '-p') { | |
$pretty = 1; | |
} else { | |
die "Invalid argument: $_\n"; | |
} | |
} | |
my $credentials = { | |
'user' => 'user_id', | |
'pass' => '1234' | |
}; | |
my $pages = { | |
'checkouts' => 'checkedout', | |
'holds' => 'holds', | |
'fines' => 'fines' | |
}; | |
my $data = { | |
"checkouts" => {}, | |
"holds" => {}, | |
"fines" => {} | |
}; | |
my $json = JSON::XS->new(); | |
foreach my $page (keys %$pages) { | |
my $mech = WWW::Mechanize->new(autocheck => 1, cookie_jar => {}, agent => 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; MDDRJS; rv:11.0) like Gecko'); | |
my $url = 'https://ottawa.bibliocommons.com/user/login?destination=%2Fv2%2F' . $pages->{$page}; | |
my $res = $mech->post($url, [ 'name' => $credentials->{user}, 'user_pin' => $credentials->{pass} ]); | |
my $html = HTML::TreeBuilder->new_from_content($res->decoded_content()); | |
my $raw_json = $html->find_by_attribute('type','application/json')->{_content}->[0]; | |
my $content = $json->decode($raw_json); | |
foreach my $item (keys %{$content->{entities}->{$page}}) { | |
my $bibId = $content->{entities}->{$page}->{$item}->{metadataId}; | |
my @fields; | |
if ($page eq 'holds') { | |
@fields = qw| status pickupByDate |; | |
} elsif ($page eq 'checkouts') { | |
@fields = qw| status dueDate |; | |
} elsif ($page eq 'fines') { | |
@fields = qw| status billId bibTitle bibSubtitle payable message messageDate amount |; | |
} else { | |
die "Invalid page: $page\n"; | |
} | |
foreach (@fields) { | |
$data->{$page}->{$item}->{$_} = $content->{entities}->{$page}->{$item}->{$_}; | |
} | |
unless ($page eq 'fines') { | |
my $bibs = $content->{entities}->{bibs}; | |
foreach (qw| authors callNumber description edition format isbns primaryLanguage publicationDate rating series subjectHeadings subtitle superFormats title |) { | |
$data->{$page}->{$item}->{$_} = $content->{entities}->{bibs}->{$bibId}->{briefInfo}->{$_}; | |
} | |
foreach (qw| large medium small local_url |) { | |
if (defined $content->{entities}->{bibs}->{$bibId}->{briefInfo}->{jacket}->{$_}) { | |
$data->{$page}->{$item}->{cover} = $content->{entities}->{bibs}->{$bibId}->{briefInfo}->{jacket}->{$_}; | |
last; | |
} | |
} | |
} | |
} | |
} | |
print $json->pretty($pretty)->encode($data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment