1 |
#!/usr/local/bin/python |
2 |
# Credit Card |
3 |
# |
4 |
# Douglas Thrift |
5 |
# |
6 |
# $Id$ |
7 |
|
8 |
import atom |
9 |
import common |
10 |
from datetime import date, timedelta |
11 |
import gdata.calendar |
12 |
import gdata.calendar.service |
13 |
|
14 |
if __name__ == '__main__': |
15 |
parser = common.OptionParser() |
16 |
options = parser.parse_args()[0] |
17 |
|
18 |
config, banks = common.config(parser, options, '.creditcard') |
19 |
service = gdata.calendar.service.CalendarService() |
20 |
|
21 |
service.ClientLogin(config.get('google', 'username'), common.decode(config.get('google', 'password'))) |
22 |
|
23 |
for bank, accounts in banks.iteritems(): |
24 |
bank = common.bank(bank, options, config) |
25 |
|
26 |
for account in accounts: |
27 |
name, account = account.rsplit(' ', 1) |
28 |
due = bank.due(account) |
29 |
today = date.today() |
30 |
|
31 |
if due <= today: |
32 |
continue |
33 |
|
34 |
title = '%s Due' % name |
35 |
query = gdata.calendar.service.CalendarEventQuery('default', 'private', 'full', '"%s"' % title) |
36 |
query.start_min = str(today) |
37 |
events = service.CalendarQuery(query).entry |
38 |
|
39 |
if events == []: |
40 |
event = gdata.calendar.CalendarEventEntry() |
41 |
event.title = atom.Title(text = title) |
42 |
event.transparency = gdata.calendar.Transparency() |
43 |
event.transparency.value = 'TRANSPARENT' |
44 |
event.visibility = gdata.calendar.Visibility() |
45 |
event.visibility.value = 'PRIVATE' |
46 |
|
47 |
event.when.append(gdata.calendar.When(start_time = str(due), end_time = str(due + timedelta(1)))) |
48 |
event.when[0].reminder.append(gdata.calendar.Reminder(minutes = 10)) |
49 |
|
50 |
service.InsertEvent(event, '/calendar/feeds/default/private/full') |
51 |
else: |
52 |
for index, event in enumerate(filter(lambda event: event.title.text == title, events)): |
53 |
if index == 0: |
54 |
event.transparency.value = 'TRANSPARENT' |
55 |
event.visibility.value = 'PRIVATE' |
56 |
event.when[0].start_time = str(due) |
57 |
event.when[0].end_time = str(due + timedelta(1)) |
58 |
|
59 |
if event.when[0].reminder != []: |
60 |
event.when[0].reminder[0].minutes = str(10) |
61 |
else: |
62 |
event.when[0].reminder.append(gdata.calendar.Reminder(minutes = 10)) |
63 |
|
64 |
service.UpdateEvent(event.GetEditLink().href, event) |
65 |
else: |
66 |
service.DeleteEvent(event.GetEditLink().href) |