Skip to content

Commit

Permalink
improved 'push to gcal'
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Dec 1, 2008
1 parent 854e7d4 commit ec98917
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions tools/itog.rb
Expand Up @@ -37,10 +37,8 @@
# [x] timezone stuff
# [x] recurrence
# [x] use optparser
# [x] check for stuff removed on the g side
# [ ] all day events (OK, but 1 day late)
# [ ] check for stuff removed on the g side
# (well, by deleting the itog.yaml and flushing the calendar the user
# can trigger a 'reload all'... well...)
# [ ] package in gem:bin/ or something like that
#

Expand Down Expand Up @@ -111,12 +109,16 @@
#
# :( what if there's a shitload of them ?

#GCAL_EVENTS = GCAL.events.inject({}) { |h, e| h[
GCAL_EVENTS = GCAL.events
query = {} # TODO : restrict to a given (opt) timeframe

puts " . found #{GCAL_EVENTS.size} events in the '#{TARGET_GCAL}' gcal"
GCAL_EVENTS = GCAL.events(query).inject({}) { |h, e|
h[e.entry.id.split('/').last] = e; h
} # is the event id unique ??

puts " . found #{GCAL_EVENTS.size} events in the '#{TARGET_GCAL}' gcal"

# ical datetime to UTC
#
def adjust_dt (dt)
return nil unless dt
dt - DateTime.now.offset
Expand Down Expand Up @@ -154,14 +156,21 @@ def gpost! (ical_event)
end
end

# Returns the entry corresponding to the given gcal URI
#
def lookup_entry (gcal_uri)
GCAL_EVENTS[gcal_uri.split('/').last]
end

# Deletes gcal event with given uri.
# Deletes gcal event with given entry.
# Returns false if deletion failed
#
def gdelete! (gcal_uri)

gcal_entry = gcal_uri.is_a?(String) ? lookup_entry(gcal_uri) : gcal_uri

begin
GCAL.delete!(gcal_uri)
GCAL.delete!(gcal_entry)
true
rescue Exception => e
puts " ! #{e}"
Expand Down Expand Up @@ -208,7 +217,7 @@ def gdelete! (gcal_uri)

mtime, cal = File.open(events_path + ics) { |f|

[ f.mtime.iso8601, Icalendar.parse(f, true) ]
[ f.mtime.gmtime.iso8601, Icalendar.parse(f, true) ]
# 'true' : assuming one cal per .ics file
}

Expand All @@ -220,22 +229,25 @@ def gdelete! (gcal_uri)

cached = cache[e.uid]

on_gcal = cached ? lookup_entry(cached[1]) : false
gcal_ok = if on_gcal
pt = on_gcal.entry.published.iso8601
mt = on_gcal.entry.edited.iso8601
(pt == mt)
else
false
end

seen << e.uid

if cached and cached[0] == mtime
#
# already posted to g, but has it changed there meanwhile ?
# or vanished ?
#
# TODO : check if in GCAL_EVENTS or if gcal_event.mtime matches...
#
if cached and cached[0] == mtime and gcal_ok
puts " . #{summary}"
next
end

if cached
if cached and on_gcal
puts " - #{e.summary}"
unless gdelete!(cached[1])
unless gdelete!(on_gcal)
puts ' ! failed to delete in gcal... skipping for now'
next
end
Expand Down Expand Up @@ -264,7 +276,7 @@ def gdelete! (gcal_uri)
not_seen.each do |uid|
info = cache[uid]
puts " - #{info[2]}"
#gcal.delete!(info[1])
gdelete!(info[1])
cache.delete(uid)
end
end
Expand Down

0 comments on commit ec98917

Please sign in to comment.