--- /dev/null
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+use LWP::UserAgent;
+use JSON::XS;
+use DateTime;
+use Data::Dumper;
+use DBI;
+
+my %hours;
+
+foreach my $lib ('JWT', 'UoS', 'JND', 'SoA') {
+ foreach my $day (0..6) {
+ $hours{$lib}{$day}{'open'} = '00:00:00';
+ $hours{$lib}{$day}{'close'} = '00:00:00';
+ }
+}
+
+#foreach my $lib ('UoS') {
+# foreach my $day (0..4) {
+# $hours{$lib}{$day}{'open'} = '08:00:00';
+# $hours{$lib}{$day}{'close'} = '17:00:00';
+# }
+#}
+
+sub parse_entry {
+ my ($entry, $day, $dow) = @_;
+ # print $entry->{'id'} . "\t" . $entry->{'start'} . "\t" . $entry->{'title'} . "\t" . $day . "\t" . $dow . "\n";
+ if ($entry->{'start'} =~ m/$day/) {
+ my $lib = $entry->{'id'};
+ my ($open, $close) = parse_open_close($entry->{'title'});
+ $hours{$lib}{$dow}{'open'} = $open;
+ $hours{$lib}{$dow}{'close'} = $close;
+ }
+}
+
+sub determine_date {
+ my ($day, $dow) = @_;
+ my $days_to_add = 7 + $day - $dow;
+ if ($days_to_add > 7) {
+ $days_to_add -= 7;
+ }
+ return $days_to_add;
+}
+
+sub parse_open_close {
+ my $string = shift || 'Closed';
+ if ($string eq 'Closed') {
+ return ('00:00:00', '00:00:00');
+ }
+
+ my ($open_hour, $open_min, $open_time, $close_hour, $close_min, $close_time) = $string =~ m/^(\d+):(\d+)(.m) - (\d+):(\d+)(.m)$/;
+ if ($open_time eq 'pm') {
+ $open_hour += 12;
+ }
+ if ($close_time eq 'pm') {
+ $close_hour += 12;
+ }
+ if ("$close_hour:$close_min$close_time" eq '12:00am') {
+ $close_hour = 23;
+ $close_min = 59;
+ }
+
+ return ("$open_hour:$open_min", "$close_hour:$close_min");
+}
+
+my $json = new JSON::XS;
+my $ua = LWP::UserAgent->new;
+$ua->timeout(20);
+
+# In DateTime->dow land, 1 = Monday
+my $dow = DateTime->now( time_zone => 'local' )->set_time_zone('floating')->dow;
+
+my $mon = DateTime->now( time_zone => 'local' )->set_time_zone('floating')->add(days => determine_date(1, $dow));
+
+# Ensure the start date includes the requested start date. Geez.
+my $start = DateTime->new(
+ year => $mon->year,
+ month => $mon->month,
+ day => $mon->day
+)->subtract(days => 1)->ymd;
+
+my $tue = DateTime->new(
+ year => $mon->year,
+ month => $mon->month,
+ day => $mon->day
+)->add(days => 1)->ymd;
+
+my $wed = DateTime->new(
+ year => $mon->year,
+ month => $mon->month,
+ day => $mon->day
+)->add(days => 2)->ymd;
+
+my $thu = DateTime->new(
+ year => $mon->year,
+ month => $mon->month,
+ day => $mon->day
+)->add(days => 3)->ymd;
+
+my $fri = DateTime->new(
+ year => $mon->year,
+ month => $mon->month,
+ day => $mon->day
+)->add(days => 4)->ymd;
+
+my $sat = DateTime->new(
+ year => $mon->year,
+ month => $mon->month,
+ day => $mon->day
+)->add(days => 5)->ymd;
+
+my $sun = DateTime->new(
+ year => $mon->year,
+ month => $mon->month,
+ day => $mon->day
+)->add(days => 6)->ymd;
+
+$mon = $mon->ymd;
+
+my $baseUrl = 'https://laurentian.ca/sites/all/themes/lul/templates/includes/feeds/json.librarycal.php';
+$baseUrl .= "?lang=en&start=$start&end=$sun";
+
+print "$baseUrl\n";
+my $response = $ua->get($baseUrl);
+
+if ($response->is_success) {
+ my $entries = $json->decode($response->decoded_content);
+ foreach my $entry (@$entries) {
+ parse_entry($entry, $mon, 0);
+ parse_entry($entry, $tue, 1);
+ parse_entry($entry, $wed, 2);
+ parse_entry($entry, $thu, 3);
+ parse_entry($entry, $fri, 4);
+ parse_entry($entry, $sat, 5);
+ parse_entry($entry, $sun, 6);
+ }
+}
+
+# Now we can go through and update the hours for the libraries we care about
+# print Data::Dumper->Dump([\%hours]);
+
+my %libs = (
+ JND => 103,
+ UoS => 107,
+ JWT => 104,
+ SoA => 150
+);
+
+my $dbh = DBI->connect('dbi:Pg:dbname=;host=;port=5432', 'evergreen');
+foreach my $lib (sort keys %libs) {
+ my $stmt = $dbh->prepare("UPDATE actor.hours_of_operation SET dow_0_open = ?, dow_0_close = ?, dow_1_open = ?, dow_1_close = ?, dow_2_open = ?, dow_2_close = ?, dow_3_open = ?, dow_3_close = ?, dow_4_open = ?, dow_4_close = ?, dow_5_open = ?, dow_5_close = ?, dow_6_open = ?, dow_6_close = ? WHERE id = ?");
+ $stmt->execute((
+ $hours{$lib}{0}{'open'}, $hours{$lib}{0}{'close'},
+ $hours{$lib}{1}{'open'}, $hours{$lib}{1}{'close'},
+ $hours{$lib}{2}{'open'}, $hours{$lib}{2}{'close'},
+ $hours{$lib}{3}{'open'}, $hours{$lib}{3}{'close'},
+ $hours{$lib}{4}{'open'}, $hours{$lib}{4}{'close'},
+ $hours{$lib}{5}{'open'}, $hours{$lib}{5}{'close'},
+ $hours{$lib}{6}{'open'}, $hours{$lib}{6}{'close'},
+ $libs{$lib}
+ ));
+
+ if ($dbh->err) {
+ print STDERR "Failed to update library hours. ";
+ print STDERR "Error was " . $dbh->errstr . "\n";
+ }
+}