-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
166 lines (145 loc) · 4.17 KB
/
app.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
153
154
155
156
157
158
159
160
161
162
163
164
165
# coding: utf-8
require "rubygems"
require "bundler/setup"
require "google/api_client"
require "yaml"
require "time"
# authorization
oauth_yaml = YAML.load_file('.google-api.yaml')
client = Google::APIClient.new
client.authorization.client_id = oauth_yaml["client_id"]
client.authorization.client_secret = oauth_yaml["client_secret"]
client.authorization.scope = oauth_yaml["scope"]
client.authorization.refresh_token = oauth_yaml["refresh_token"]
client.authorization.access_token = oauth_yaml["access_token"]
# client.authorization.expired? が常にfalseを返すので意味ない?
#if client.authorization.refresh_token && client.authorization.expired?
# client.authorization.fetch_access_token!
#end
service = client.discovered_api('calendar', 'v3')
#p service
page_token = nil
result = client.execute(:api_method => service.calendar_list.list)
#p result.data
i = 0
if result.data["error"]
if i == 3
puts "error with #{result.data["error"]["errors"][0]["message"]}"
exit
end
if client.authorization.refresh_token
client.authorization.fetch_access_token!
else
puts "Get refresh token!"
exit
end
result = client.execute(:api_method => service.calendar_list.list)
i += 1
end
entries = []
while true
result.data.items.each do |item|
entries << item
end
if !(page_token = result.data.next_page_token)
break
end
result = client.execute(:api_method => service.calendar_list.list,
:parameters => {'pageToken' => page_token})
end
# カレンダー一覧の出力
entries.each_with_index do |cal, index|
#p cal
puts (index + 1).to_s + ". " + cal.summary + "\n"
end
# ユーザーにカレンダーの番号を選択させる
print "please input number you want to get events \n> "
cal_num = gets.strip.to_i - 1
exit if cal_num < 0 || cal_num > entries.size - 1
#p cal_num
# ユーザーにカレンダーのイベントを取得する期間を指定させる
print "what year? \n> "
year = gets.strip.to_i
print "what month? (1-12) \n> "
month = gets.strip.to_i
# time_minとtime_maxはUTCで指定する(+9しない)
time_min = Time.utc(year, month, 1, 0).iso8601
time_max = Time.utc(year, month, 31, 0).iso8601
#exit
# 特定のカレンダーのイベントを取得する
params = {"calendarId" => entries[cal_num].id,
"timeMax" => time_max,
"timeMin" => time_min}
page_token = nil
result = client.execute(:api_method => service.events.list,
:parameters => params)
i = 0
if result.data["error"]
if i == 3
puts "error with #{result.data["error"]["errors"][0]["message"]}"
exit
end
if client.authorization.refresh_token
client.authorization.fetch_access_token!
else
puts "Get refresh token!"
exit
end
result = client.execute(:api_method => service.events.list,
:parameters => params)
i += 1
end
# カレンダーのイベントを格納する
events = []
while true
result.data.items.each do |item|
events << item
end
if !(page_token = result.data.next_page_token)
break
end
params["pageToken"] = page_token
result = client.execute(:api_method => service.events.list,
:parameters => params)
end
def event_start_date(event)
if event.start.date
event.start.date
else
event.start.dateTime.to_s.match(/^([\d]{4}-[\d]{2}-[\d]{2})\s([\d]{2}:[\d]{2})/)
$1
end
end
def event_start_time(event)
if event.start.date
nil
else
event.start.dateTime.to_s.match(/^([\d]{4}-[\d]{2}-[\d]{2})\s([\d]{2}:[\d]{2})/)
$2
end
end
def event_end_date(event)
if event.end.date
event.end.date
else
event.end.dateTime.to_s.match(/^([\d]{4}-[\d]{2}-[\d]{2})\s([\d]{2}:[\d]{2})/)
$1
end
end
def event_end_time(event)
if event.end.date
nil
else
event.end.dateTime.to_s.match(/^([\d]{4}-[\d]{2}-[\d]{2})\s([\d]{2}:[\d]{2})/)
$2
end
end
# イベント一覧をCSVに書き出す
require "csv"
month = sprintf("%02d", month)
output_file = "data/event_#{year}#{month}.csv"
CSV.open(output_file, "wb:Shift_JIS:UTF-8") do |csv|
events.each do |event|
csv << [event_start_date(event), event_start_time(event), event_end_date(event), event_end_time(event), event.summary]
end
end