-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathical-from-sent-email.rb
66 lines (56 loc) · 2.05 KB
/
ical-from-sent-email.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
require 'rubygems'
require 'highline/import'
require 'icalendar'
require 'date'
require 'net/imap'
require 'trollop'
require 'date'
require_relative 'lib/ical-patch'
# commandline option parsing, trollop is much more concise than optparse
options = Trollop::options do
banner "Usage: #{$0} [options]"
opt :month, 'Month to filter results with', :type => :int
opt :year, 'Year to filter results with', :type => :int
opt :out, 'Output file to put the resulting ics', :type => :string
end
start_date = Date.civil(options[:year], options[:month])
imap_since = start_date.strftime("%d-%B-%Y")
end_date = start_date>>1
imap_before = end_date.strftime("%d-%B-%Y")
# "/Users/scytacki/Documents/CCProjects/Timesheets/june-2010/sent-mail.ics"
ics_file = options[:out]
def get_password(prompt="Enter Password")
ask(prompt) {|q| q.echo = false}
end
imap = Net::IMAP.new('imap.gmail.com', 993, true, nil, false)
imap.login('[email protected]', get_password())
imap.examine('[Gmail]/Sent Mail')
msgs = imap.search(["BEFORE", imap_before, "SINCE", imap_since])
headers = imap.fetch(msgs, "BODY[HEADER.FIELDS (TO CC SUBJECT)]")
puts 'downloaded headers'
dates = imap.fetch(msgs, "INTERNALDATE")
puts 'downloaded dates'
bodies = imap.fetch(msgs, "BODY[TEXT]")
puts 'downloaded bodies'
cal = Icalendar::Calendar.new
msgs.each_with_index{|id, idx|
start_date = DateTime.parse(dates[idx].attr["INTERNALDATE"])
# without the next line the start_date will be on GMT time
# but the end date will be on local time and the ical library
# doesn't include the timezone when serializing the datetime
start_date = start_date.to_time.to_datetime
end_date = (start_date.to_time + 8*60).to_datetime
putc '.'
# puts "start #{start_date} end #{end_date}"
cal.event{
dtstart start_date
dtend end_date
msg_headers = (headers[idx].attr["BODY[HEADER.FIELDS (TO CC SUBJECT)]"]).strip.sub('Subject:', '')
summary msg_headers
description msg_headers + (bodies[idx].attr["BODY[TEXT]"]).strip
}
}
puts "finished"
File.open(ics_file, 'w') {|f|
cal.to_ical_file(f)
}