Skip to content

Instantly share code, notes, and snippets.

@rochefort
Created August 31, 2018 00:30
Show Gist options
  • Save rochefort/d3f5222c8185abc90f2954e199a9a42d to your computer and use it in GitHub Desktop.
Save rochefort/d3f5222c8185abc90f2954e199a9a42d to your computer and use it in GitHub Desktop.
grn
#!/usr/bin/env ruby -W0
require "date"
require "time"
require "garoon-cat"
require_relative "./schedule"
class GaroonScheduler
WSDL_URI = "https://info2.nri-net.com/cgi-bin/cbgrn/grn.cgi?WSDL"
def initialize
garoon = GaroonCat.setup(
uri: WSDL_URI,
username: ENV["PROXY_USER"],
password: ENV["PROXY_PASS"]
)
@service = garoon.service(:schedule)
end
def show
params = {
"@start": "#{Date.today}T00:00:00Z",
"@end": "#{(Date.today + 5)}T00:00:00Z"
}
response = @service.get_events(params)
schedules = extract_schdules(response)
print_schedules(schedules)
end
private
def extract_schdules(response)
schedules = []
response.dig("schedule_event").each do |event|
next if event["event_type"] == "banner"
detail = event&.dig("detail")
repeat_info = event.dig("repeat_info", 0)
if repeat_info
condition = repeat_info.dig("condition", 0)
week = condition["week"].to_i # 0が日曜日?
day = (Date.today..(Date.today + 5)).find { |d| d.wday == week }
start_at = Time.parse("#{day} #{condition['start_time']}")
end_at = Time.parse("#{day} #{condition['end_date']}")
else
event_at = event.dig("when", 0, "datetime", 0)
start_at = localtime(event_at&.dig("start"))
end_at = localtime(event_at&.dig("end"))
end
schedules << Schedule.new(detail, start_at, end_at)
end
schedules
end
def print_schedules(schedules)
schedules.sort { |x, y| x.start_at <=> y.start_at }
.each { |s| s.print_schedule }
end
def localtime(t)
if t
t = Time.parse(t)
t.localtime
end
t
end
end
if __FILE__ == $0
GaroonScheduler.new.show
end
class Schedule < Struct.new(:title, :start_at, :end_at)
def print_schedule
mark = is_today(start_at) ? " *" : " "
day = start_at&.strftime("%m/%d")
start_time = start_at&.strftime("%H:%M")
end_time = end_at&.strftime("%H:%M")
puts "#{mark} #{day} #{start_time} - #{end_time} : #{title}"
end
private
def is_today(time)
Time.now.strftime("%Y-%m-%d") == time.strftime("%Y-%m-%d")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment