Skip to content

Instantly share code, notes, and snippets.

@robinsloan
Created March 11, 2015 17:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robinsloan/b9ccbf02395dd4462b3e to your computer and use it in GitHub Desktop.
Save robinsloan/b9ccbf02395dd4462b3e to your computer and use it in GitHub Desktop.
Add entries for prime-numbered weekdays to your Google calendar. Useful to literally no one but me.
require "rubygems"
require "google_calendar"
require "date"
require "prime"
# Create an instance of the calendar.
cal = Google::Calendar.new(:client_id => "foo"
:client_secret => "bar",
:calendar => "blee",
:redirect_url => "baz",
:refresh_token => "oof"
)
# only need this commented section for first run
=begin
puts "Do you already have a refresh token? (y/n)"
has_token = $stdin.gets.chomp
#TOKEN = "4/u-yuF6pWS_mlZQEMgVwQBQ6hF3efCYTden0CcxNdfFY.okp1KclVcwEfEnp6UAPFm0FUBx0wlQI"
#REFRESH_TOKEN = "1/7Zd44gJxKnCWYh-MNkJ8MG-e5yUtxGI7en6Ldi6sPIEMEudVrK5jSpoR30zcRFq6"
if has_token.downcase != 'y'
# A user needs to approve access in order to work with their calendars.
puts "Visit the following web page in your browser and approve access."
puts cal.authorize_url
puts "\nCopy the code that Google returned and paste it here:"
# Pass the ONE TIME USE access code here to login and get a refresh token that you can use for access from now on.
refresh_token = cal.login_with_auth_code( $stdin.gets.chomp )
puts "\nMake sure you SAVE YOUR REFRESH TOKEN so you don't have to prompt the user to approve access again."
puts "your refresh token is:\n\t#{refresh_token}\n"
puts "Press return to continue"
$stdin.gets.chomp
else
puts "Enter your refresh token"
refresh_token = $stdin.gets.chomp
cal.login_with_refresh_token(refresh_token)
# Note: You can also pass your refresh_token to the constructor and it will login at that time.
end
=end
puts "deleting all extant events..."
cal.events.each do |event|
event.delete
end
def make_prime_event(cal,date,index,yday_prime)
event = cal.create_event do |e|
str = "PRIMES email"
if (yday_prime != 0) then
str += " (also yday prime: #{yday_prime})"
end
e.title = str
e.start_time = date.to_s
e.end_time = (date + 1).to_s
end
end
primes = [2,3,5,7,11,13,17,19,23,29,31]
year = 2015
num_days = 0
(1..3).each do |month|
primes.each do |prime|
begin
unless ((month == 2) && (prime > 23))
date = Date.new(year,month,prime)
if ((date.wday > 0) && (date.wday < 6)) then
num_days += 1
puts "#{date.strftime("%a %b")} #{prime}"
yday_prime = 0
if date.yday.prime? then
yday_prime = date.yday
end
make_prime_event(cal,date,num_days,yday_prime)
end
end
end
end
end
puts "#{num_days} days total"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment