forked from yorkulibraries/branchhours
-
Notifications
You must be signed in to change notification settings - Fork 1
/
libcal_hours_daily.rb
150 lines (125 loc) · 4.01 KB
/
libcal_hours_daily.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/ruby -w
#
# Show today's branch opening hours on current date, taken from Libcal Calendars
#
# Original Author: Ali Sadaqain <[email protected]>
# https://github.com/asadaqain/branchhours
require 'json'
require 'date'
require 'time'
require 'yaml'
require 'optparse'
require 'open-uri'
require 'fileutils'
require_relative 'libcal_hours_functions.rb'
options = {}
options[:verbose] = false
options[:config] = 'libcal_hours.yml'
OptionParser.new do |opts|
opts.banner = "Usage: libcal_hours [options]"
opts.on("-c", "--config configFile", "specify configuration file") do |configFile|
options[:config] = configFile
end
opts.on("-v", "--verbose", "be verbose") { options[:verbose] = true }
end.parse!
# Hash to store opening hours, by date, then library
# schedule = Hash.new do |hash, key|
# hash[key] = {}
# end
begin
config = YAML.load_file(options[:config])
rescue Exception => e
puts e
exit 1
end
STDERR.puts "Creating Output Directories if needed ..."
begin
FileUtils.mkdir_p config['dailyDir']
rescue Exception => e
puts e
exit 1
end
STDERR.puts "Getting LibCal Calendar ..." if options[:verbose]
calendarUrl = config['calendarDailyURL']
begin
# Had issues with Net::HTTP throwing exception of "end of file reached" on production.
# Not sure why but open-uri worked better.
#calendarJSON = Net::HTTP.get_response(URI.parse(calendarUrl)).body
calendarJSON = URI.open(calendarUrl).read
rescue Exception => e
STDERR.puts "Couldn't get #{library} calendar from Google"
STDERR.puts "Can't connect to Google!? Aborting"
STDERR.puts e
exit 1
end
begin
calendar = JSON.parse(calendarJSON)
rescue Exception => e
STDERR.puts "Can't parse the JSON for the #{library} calendar!? Aborting"
STDERR.puts e
exit 1
end
all_locations_file = "all_locations_daily_hours.html"
begin
wholedayfile = File.new("#{config['dailyDir']}/all_locations_daily_hours.html", "w+")
wholedayfile.chmod(0664)
rescue Exception => e
STDERR.puts "Problem creating #{config['dailyDir']}/#{all_locations_file}. Aborting"
STDERR.puts e
exit 1
end
wholedayfile.write("<div class='row'>")
calendar['locations'].each_with_index do |location, i|
location_name = location['name']
location_url = location['url']
location_name_sanitized = sanitize_filename(location_name)
dayline = ''
begin
dayfile = File.new("#{config['dailyDir']}/#{location_name_sanitized}_daily_hours.html", "w+")
dayfile.chmod(0664)
rescue Exception => e
STDERR.puts "Problem creating #{config['dailyDir']}/#{location_name_sanitized}_daily_hours.html. Aborting"
STDERR.puts e
exit 1
end
STDERR.puts location_name if options[:verbose]
STDERR.puts "CARD LAYOUT" if options[:verbose]
dayline = "
<div class='card border border-1'>\n
<div class='card-body text-center p-2'>\n
<p class='card-title fw-bold text-primary'><a href='#{location_url}'>#{location_name}</a></p>\n
<p class='card-text small fw-bolder'>
<i class='far fa-clock'></i> <span class='text-muted'>#{location['day']}\n
</span>#{location['rendered']}
</p>\n
</div>\n
</div>"
STDERR.puts dayline if options[:verbose]
dayfile.write(dayline)
if (i+1)%3 == 0
wholedayfile.write("</div><div class='row'>")
wholedayfile.write(dayline)
else
wholedayfile.write(dayline)
end
end
wholedayfile.write('</div>')
begin
dayfile = File.new("#{config['dailyDir']}/more_daily_hours.html", "w+")
dayfile.chmod(0664)
rescue Exception => e
STDERR.puts "Problem creating #{config['dailyDir']}/more_daily_hours.html. Aborting"
STDERR.puts e
exit 1
end
dayline = "
<div class='card justify-content-center text-center border border-1'>\n
<div class='card-body text-center p-2'>\n
<p class='card-title fw-bold text-dark'><a href='https://www.library.yorku.ca//web/hours-and-locations'>See All Hours</a></p>\n
<p class='card-text small fw-bolder'>
<i class='far fa-clock'></i>
</p>\n
</div>\n
</div>"
STDERR.puts dayline if options[:verbose]
dayfile.write(dayline)