-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbranchhours-fr
executable file
·235 lines (197 loc) · 7.57 KB
/
branchhours-fr
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/ruby -w
# branchhours
#
# Show today's branch opening hours, taken from Google Calendars
#
# William Denton <[email protected]>
# http://github.com/wdenton/branchhours/
require 'net/http'
require 'optparse'
require 'json'
require 'date'
require 'time'
require 'yaml'
require 'open-uri'
options = {}
options[:verbose] = false
options[:config] = 'branchhours.yml'
OptionParser.new do |opts|
opts.banner = "Usage: branchhours [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!
begin
config = YAML.load_file(options[:config])
rescue Exception => e
puts e
exit 1
end
def prettytime (time)
# '9 am' and 'noon' are better than '09:00 AM' and '12:00 PM'
nice = time.strftime('%I')
nice = nice.slice(1,2) if (nice.to_i < 10)
nice = nice + ":" + time.strftime('%M') unless (time.strftime('%M').to_i == 0)
nice = nice + " " + time.strftime('%p')
nice.downcase!
nice = "noon" if nice == '12 PM'
nice =time.strftime('%kh%M') #French Request, override above
return nice
end
# Get ready to give Google the start and end times for the daysInAdvance
# range of days.
# You may need to change the time zone.
# startmin = (Time.now - config['daysInAdvance']*24*60*60).strftime("%Y-%m-%dT06:00:00-05:00")
timeMin = Time.now.strftime("%Y-%m-%dT06:00:00-05:00")
timeMax = (Time.now + config['daysInAdvance']*24*60*60).strftime("%Y-%m-%dT06:00:00-05:00")
# Hash to store opening hours, by date, then library
schedule = Hash.new do |hash, key|
# Ruby doesn't create a hash of hashes on the fly like Perl, so
# I'm using this from http://www.ruby-forum.com/topic/140570
hash[key] = {}
end
STDERR.puts "Getting calendars from Google ..." if options[:verbose]
config['libraries'].each do |library|
# The schedule hash will have YYYYMMDD dates as keys, and the
# values will be hashes where the key is the library name and
# the value is a human-readable string showing the opening hours
# (or that the library is closed).
calendarUrl = config['googleUrl'].gsub(/::ID::/, config[library]['id'])
calendarUrl = calendarUrl.gsub(/::KEY::/, config['apikey'])
calendarUrl << "&timeMin=" + timeMin
calendarUrl << "&timeMax=" + timeMax
STDERR.puts " " + library if options[:verbose]
STDERR.puts " " + calendarUrl if options[:verbose]
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 = 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
# puts calendar.to_json
calendar['items'].each do |item|
date = ""
open = ""
close = ""
if item['start']['date'] # All-day event (will be (must be?) a closing), so ignore hours
date = Time.parse(item['start']['date']).strftime("%Y%m%d")
elsif item['start']['dateTime'] # Delimited by hours
# STDERR.puts item['start']['dateTime']
date = Time.parse(item['start']['dateTime']).strftime("%Y%m%d").to_s
# STDERR.puts date
open = prettytime(Time.parse(item['start']['dateTime']))
# STDERR.puts open
close = prettytime(Time.parse(item['end']['dateTime']))
end
hours = "#{open} - #{close}"
hours = "closed" if item['summary'] =~ /closed/i
if library == 'scott_study_space'
# Special case. For the Scott study space we
# always want to show the title of the all-day event.
schedule[date][library] = item['summary']
else
schedule[date][library] = hours
end
end
end
# STDERR.puts schedule
STDERR.puts "Writing calendars ..." if options[:verbose]
schedule.keys.sort {|a,b| a <=> b}.each do |day|
# Go through each day for which we collected calendar information,
# and dump out fragments of HTML to disk, suitable for including
# in a web page.
STDERR.puts day if options[:verbose]
prettydate = Date.parse(day).strftime("%A, %B %e, %Y")
dayhtml = ''
dayhtml << config['template']
mdayhtml = ''
mdayhtml << config['mtemplate']
# Needless duplication, but it works
begin
dayfile = File.new("#{config['outputDir']}/#{day}.html", "w")
dayfile.chmod(0664)
rescue Exception => e
STDERR.puts "Problem creating #{config['outputDir']}/#{day}.html. Aborting"
STDERR.puts e
exit 1
end
begin
mdayfile = File.new("#{config['outputDir']}/m-#{day}.html", "w")
mdayfile.chmod(0664)
rescue Exception => e
STDERR.puts "Problem creating #{config['outputDir']}/m-#{day}.html. Aborting"
STDERR.puts e
exit 1
end
config['libraries'].each do |library|
if schedule[day][library].nil?
# If a branch has no information for a day, show a blank.
# That at least shows something is missing without alarming people.
STDERR.puts " " + library + ": no information. Update your calendar."
schedule[day][library] = ''
end
STDERR.puts " " + library + ": " + schedule[day][library] if options[:verbose]
# Replace ::LIBRARY:: in the template with some linked name and hours.
libraryLine = "<a class=\"library-branch\" title=\"#{config[library]['title']}\" href=\"#{config[library]['url']}\">#{config[library]['name']}</a> <span class=\"hours\">#{schedule[day][library]}</span>"
dayhtml.gsub!(/::#{library.upcase}::/, libraryLine)
# agendaUrl = config['googleAgendaUrl'].gsub(/::ID::/, config[library]['id']) + "&ctz=America/Toronto&mode=AGENDA"
# mlibraryLine = "<a href=\"#{agendaUrl}\">#{config[library]['name']}: #{schedule[day][library]}</a>"
mlibraryLine = "#{config[library]['mname']}: #{schedule[day][library]}"
mdayhtml.gsub!(/::#{library.upcase}::/, mlibraryLine)
end
dayhtml.gsub!(/::TODAY::/, prettydate)
dayfile.write(dayhtml)
mdayhtml.gsub!(/::TODAY::/, prettydate)
mdayfile.write(mdayhtml)
end
##
# Create the weekly hours file for each library
#
##
config['libraries'].each do |library|
begin
weekfile = File.new("#{config['weeklyDir']}/#{library}_hours.html", "w")
weekfile.chmod(0664)
rescue Exception => e
STDERR.puts "Problem creating #{config['weeklyDir']}/#{library}_hours.html. Aborting"
STDERR.puts e
exit 1
end
STDERR.puts library.upcase if options[:verbose]
weekhtml = ''
weekhtml << config['weektemplate']
for i in 0..6
# day_to_get = now - now.wday + i
day_to_get = Time.now + (i*24*60*60)
day_hours = schedule[day_to_get.strftime('%Y%m%d')][library]
if (day_hours)
weekline = "<span class=\"day\">#{day_to_get.strftime('%A')}</span><span class=\"hours\">#{day_hours.upcase}</span>"
weekhtml.gsub!(/::#{day_to_get.strftime('%A').upcase}::/, weekline)
weekhtml.gsub!(/Monday/, 'LUNDI')
weekhtml.gsub!(/Tuesday/, 'MARDI')
weekhtml.gsub!(/Wednesday/, 'MERCREDI')
weekhtml.gsub!(/Thursday/, 'JEUDI')
weekhtml.gsub!(/Friday/, 'VENDREDI')
weekhtml.gsub!(/Saturday/, 'SAMEDI')
weekhtml.gsub!(/Sunday/, 'DIMANCHE')
weekhtml.gsub!(/closed/, 'FERME')
STDERR.puts day_to_get.strftime('%A') + " " + day_hours if options[:verbose]
end
end
STDERR.puts weekhtml if options[:verbose]
weekfile.write(weekhtml)
STDERR.puts '=============================' if options[:verbose]
end