-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaroon2ical.rb
153 lines (101 loc) · 2.88 KB
/
garoon2ical.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
151
152
# -*- encoding: utf-8 -*-
require 'rubygems'
require 'httpclient'
require 'date'
# iCalデータ編集
def make_ics(str, dt, first)
y = dt.year
m = dt.month
flg = true
cnt = 0
ics = ""
if first then
ics = <<EOF
BEGIN:VCALENDARa
PRODID:Cybozu Web Calendar
VERSION:2.0
EOF
end
dtstart = "DTSTART:#{sprintf("%4d%02d",y,m)}"
flg_desc = false
str.each_line do |line|
if cnt < 3 then
# 初めの3行は読込まない
elsif flg_desc then
if /^END:/ =~ line then
# ENDデータの読込
flg_desc = false
ics += "\n" + line
else
# DESCRIPTIONでは改行コードを文字列に変換
ics += line.gsub("\n","\\n")
end
elsif /^DESC/ =~ line && flg then
# DESCRIPTIONデータの加工
flg_desc = true
ics += line.gsub("\n","\\n")
elsif /[A-Z]/ =~ line then
if line == "BEGIN:VEVENT\n" || line == "END:VCALENDAR" then
# データ開始行、データ終了行を読込まない
flg = false
elsif line[0..(dtstart.length-1)] == dtstart then
# 日付が指定月のデータであれば読込開始
flg = true
ics += "BEGIN:VEVENT\n" + line
else
# データ読込
if flg then
ics += line
end
end
end
cnt += 1
end
return ics
end
# ガルーンにログインする
loginURL = 'http://[your garoon url]/cgi-bin/cbgrn/grn.cgi/'
loginParams = {
'_system' => '1',
'_account' => '[your account]',
'_password' => '[your password]'
}
client = HTTPClient.new()
client.post(loginURL, loginParams)
# 当月と翌月を取得する
now = Time.new
thisMonth = Date.new(now.year, now.month, 1)
nextMonth = thisMonth >> 1
next2Month = thisMonth >> 2
# iCalデータ取得用URL
iCalURL = "#{loginURL}schedule/command_personal_month_icalexport"
# ユーザー情報の設定
users = [
{
"name" => "[user name]",
"uid" => "000",
"gid" => "000"
},
{
"name" => "[user name]",
"uid" => "000",
"gid" => "000"
}
]
users.each do |row|
str = ""
open("./#{row["name"]}.ics","w") do |f|
puts row["name"]
# 1ヶ月目のiCalデータ
tmp = client.get_content("#{iCalURL}?uid=#{row["uid"]}&gid=#{row["gid"]}&bdate=#{thisMonth.to_s}")
str += make_ics(tmp, thisMonth, true)
# 2ヶ月目のiCalデータ
tmp = client.get_content("#{iCalURL}?uid=#{row["uid"]}&gid=#{row["gid"]}&bdate=#{nextMonth.to_s}")
str += make_ics(tmp, nextMonth, false)
# 3ヶ月目のiCalデータ
tmp = client.get_content("#{iCalURL}?uid=#{row["uid"]}&gid=#{row["gid"]}&bdate=#{next2Month.to_s}")
str += make_ics(tmp, next2Month, false)
str += "END:VCALENDAR\n"
f.write str
end
end