Last active
August 29, 2015 14:07
-
-
Save debug-ito/92f54e7e100ba2eb561f to your computer and use it in GitHub Desktop.
Load Google Calendar events and convert them into BusyBird statuses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# coding: utf-8 | |
## See also: http://qiita.com/mechamogera/items/bf2ed20e332dc31d2352 | |
require 'google/api_client' | |
require 'json' | |
require 'date' | |
require 'optparse' | |
FORMAT = '%a %b %d %H:%M:%S %z %Y' | |
$utc_offset = '+09:00' | |
$count = 100 | |
def get_time(item) | |
time = item['start']['dateTime'] | |
if !time | |
date = item['start']['date'] | |
if !date | |
return nil | |
end | |
date =~ /^(\d{4})-(\d{2})-(\d{2})/ | |
time = Time.new($~[1].to_i, $~[2].to_i, $~[3].to_i, 0, 0, 0, $utc_offset) | |
end | |
return time | |
end | |
OptionParser.new do |opt| | |
opt.version = "0.02" | |
opt.on("--service-key-file FILE", "Path to the service key file") {|v| | |
$service_key_file = v | |
} | |
opt.on("--service-password PASSWORD", "Password for the service key file") {|v| | |
$service_password = v | |
} | |
opt.on("--service-email EMAIL", "Email address for the service account") {|v| | |
$service_email = v | |
} | |
opt.on("--calendar-id ID", "Calendar ID to fetch") {|v| | |
$calendar_id = v | |
} | |
opt.on("--utc-offset [OFFSET]", | |
"Timezone offset from UTC. This is used when timezone is not specified in events. (default: '+09:00')") {|v| | |
$utc_offset = v | |
} | |
opt.on("--count [COUNT]", "Number of events to load (default: 100)") {|v| | |
$count = v | |
} | |
opt.parse! ARGV | |
end | |
client = Google::APIClient.new(:application_name => '') | |
# Authorization | |
key = Google::APIClient::KeyUtils.load_from_pkcs12($service_key_file, $service_password) | |
client.authorization = Signet::OAuth2::Client.new( | |
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token', | |
:audience => 'https://accounts.google.com/o/oauth2/token', | |
:scope => 'https://www.googleapis.com/auth/calendar.readonly', | |
:issuer => $service_email, | |
:signing_key => key) | |
client.authorization.fetch_access_token! | |
# Fetching Calendar events | |
cal = client.discovered_api('calendar', 'v3') | |
params = { | |
'calendarId' => $calendar_id, | |
'orderBy' => 'startTime', | |
'timeMin' => Time.now.getgm.iso8601, | |
'singleEvents' => 'True', | |
'maxResults' => $count, | |
} | |
result = client.execute(:api_method => cal.events.list, | |
:parameters => params) | |
bb_result = result.data.items.collect do |item| | |
time = get_time item | |
icon_str = time ? sprintf('http://calendar.google.com/googlecalendar/images/favicon_v2014_%d.ico', | |
time.day) : nil | |
{ | |
"id" => sprintf('%d|%s', (item.updated ? item.updated.to_i : | |
item.created ? item.created.to_i : 0), | |
item["htmlLink"]), | |
"created_at" => time ? time.strftime(FORMAT) : nil, | |
"user" => { | |
"screen_name" => result.data.summary, | |
"profile_image_url" => icon_str, | |
}, | |
"busybird" => { "status_permalink" => item["htmlLink"] }, | |
"text" => item["summary"], | |
} | |
end | |
print JSON.generate bb_result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment