forked from nomlab/nomrat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCal.rb
executable file
·96 lines (88 loc) · 3.08 KB
/
GCal.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
class GCal
def initialize(name)
oauth_yaml = YAML.load_file(File.dirname(__FILE__) + '/.google-api.yaml')
@client = Google::APIClient.new("application_name" => name)
@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"]
if @client.authorization.refresh_token && @client.authorization.expired?
@client.authorization.fetch_access_token!
end
@service = @client.discovered_api('calendar', 'v3')
end
def event_list_find_by_name(calendar_id, keywords)
ex_event_list = event_list(calendar_id)
event_list = []
ex_event_list.each do |ev|
keywords.each do |keyword|
if ev.summary =~ /.*#{keyword}.*/
event_list << ev
end
end
end
return event_list
end
def event_list(calendar_id)
page_token = nil
result = @client.execute(:api_method => @service.events.list,
:parameters => {'calendarId' => calendar_id})
event_list = []
while true
events = result.data.items
event_list += events
# events.each do |e|
# print e.summary + "\n" if e.summary != nil
# end
if !(page_token = result.data.next_page_token)
break
end
result = @client.execute(:api_method => @service.events.list,
:parameters => {'calendarId' => calendar_id,
'pageToken' => page_token})
end
return event_list
end
def events(calendar_id)
page_token = nil
result = @client.execute(:api_method => @service.events.list,
:parameters => {'calendarId' => calendar_id})
now = Time.now
open("#{File.dirname(__FILE__)}/data/#{now.strftime("%Y%m%d%H%M")}.json","w") do |f|
while true
events = result.data.items
events.each do |e|
puts e
end
JSON.dump(JSON.parse(result.data.to_json), f)
if !(page_token = result.data.next_page_token)
break
end
result = @client.execute(:api_method => @service.events.list,
:parameters => {'calendarId' => 'primary',
'pageToken' => page_token})
end
end
end
def calendar_list
page_token = nil
result = @client.execute(:api_method => @service.calendar_list.list)
while true
entries = result.data.items
entries.each do |e|
print e.summary + "\n"
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
end
def calendars(calendar_id)
result = @client.execute(:api_method => @service.calendars.get,
:parameters => {'calendarId' => calendar_id})
print result.data
end
end