2012-10-21 22:18:51 +02:00
|
|
|
#!/usr/bin/env python
|
2012-11-07 17:50:29 +01:00
|
|
|
import os
|
2012-10-21 22:18:51 +02:00
|
|
|
import sys
|
|
|
|
import time
|
2012-11-07 17:50:29 +01:00
|
|
|
import errno
|
2012-10-21 22:18:51 +02:00
|
|
|
import datetime
|
|
|
|
import optparse
|
|
|
|
import urlparse
|
|
|
|
import itertools
|
|
|
|
import traceback
|
|
|
|
from os import path
|
|
|
|
|
2013-01-31 20:09:01 +01:00
|
|
|
sys.path.append(path.join(path.dirname(__file__), '../api'))
|
2012-12-03 18:23:06 +01:00
|
|
|
import humbug
|
2012-10-21 22:18:51 +02:00
|
|
|
|
|
|
|
parser = optparse.OptionParser(r"""
|
|
|
|
|
|
|
|
%prog \
|
|
|
|
--user foo@humbughq.com \
|
|
|
|
--calendar http://www.google.com/calendar/feeds/foo%40humbughq.com/private-fedcba9876543210fedcba9876543210/basic
|
|
|
|
|
|
|
|
Send yourself reminders on Humbug of Google Calendar events.
|
|
|
|
|
|
|
|
To get the calendar URL:
|
|
|
|
- Load Google Calendar in a web browser
|
|
|
|
- Find your calendar in the "My calendars" list on the left
|
|
|
|
- Click the down-wedge icon that appears on mouseover, and select "Calendar settings"
|
|
|
|
- Copy the link address for the "XML" button under "Private Address"
|
|
|
|
|
|
|
|
Run this on your personal machine. Your API key and calendar URL are revealed to local
|
|
|
|
users through the command line.
|
|
|
|
|
|
|
|
Depends on: python-gdata
|
|
|
|
""")
|
|
|
|
|
|
|
|
parser.add_option('--user',
|
|
|
|
dest='user',
|
|
|
|
action='store',
|
|
|
|
help='Humbug user email address',
|
|
|
|
metavar='EMAIL')
|
|
|
|
parser.add_option('--api-key',
|
|
|
|
dest='api_key',
|
|
|
|
action='store',
|
2012-11-07 17:50:29 +01:00
|
|
|
help='API key for that user [default: read ~/.humbug-api-key]')
|
2012-10-21 22:18:51 +02:00
|
|
|
parser.add_option('--calendar',
|
|
|
|
dest='calendar',
|
|
|
|
action='store',
|
|
|
|
help='Google Calendar XML "Private Address"',
|
|
|
|
metavar='URL')
|
|
|
|
parser.add_option('--site',
|
|
|
|
dest='site',
|
2012-10-27 17:36:55 +02:00
|
|
|
default="https://humbughq.com",
|
2012-10-21 22:18:51 +02:00
|
|
|
action='store',
|
2012-10-27 17:36:55 +02:00
|
|
|
help='Humbug site [default: https://humbughq.com]',
|
2012-10-21 22:18:51 +02:00
|
|
|
metavar='URL')
|
|
|
|
parser.add_option('--interval',
|
|
|
|
dest='interval',
|
|
|
|
default=10,
|
|
|
|
type=int,
|
|
|
|
action='store',
|
|
|
|
help='Minutes before event for reminder [default: 10]',
|
|
|
|
metavar='MINUTES')
|
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2012-12-03 19:50:24 +01:00
|
|
|
if not (options.user and options.calendar):
|
|
|
|
parser.error('You must specify --user and --calendar')
|
2012-10-21 22:18:51 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
from gdata.calendar.client import CalendarClient
|
|
|
|
except ImportError:
|
|
|
|
parser.error('Install python-gdata')
|
|
|
|
|
|
|
|
def get_calendar_url():
|
|
|
|
parts = urlparse.urlparse(options.calendar)
|
|
|
|
pat = path.split(parts.path)
|
|
|
|
if pat[1] != 'basic':
|
|
|
|
parser.error('The --calendar URL should be the XML "Private Address" ' +
|
|
|
|
'from your calendar settings')
|
|
|
|
return urlparse.urlunparse((parts.scheme, parts.netloc, pat[0] + '/full',
|
|
|
|
'', 'futureevents=true&orderby=startdate', ''))
|
|
|
|
|
|
|
|
calendar_url = get_calendar_url()
|
|
|
|
|
2012-12-03 22:13:00 +01:00
|
|
|
client = humbug.Client(
|
2012-12-03 18:23:06 +01:00
|
|
|
email=options.user,
|
|
|
|
api_key=options.api_key,
|
|
|
|
site=options.site,
|
|
|
|
verbose=True)
|
2012-10-21 22:18:51 +02:00
|
|
|
|
|
|
|
def get_events():
|
|
|
|
feed = CalendarClient().GetCalendarEventFeed(uri=calendar_url)
|
|
|
|
|
|
|
|
for event in feed.entry:
|
|
|
|
start = event.when[0].start.split('.')[0]
|
2013-01-16 19:51:32 +01:00
|
|
|
# All-day events can have only a date
|
|
|
|
fmt = '%Y-%m-%dT%H:%M:%S' if 'T' in start else '%Y-%m-%d'
|
|
|
|
start = datetime.datetime.strptime(start, fmt)
|
2012-10-21 22:18:51 +02:00
|
|
|
yield (event.uid.value, start, event.title.text)
|
|
|
|
|
|
|
|
# Our cached view of the calendar, updated periodically.
|
|
|
|
events = []
|
|
|
|
|
|
|
|
# Unique keys for events we've already sent, so we don't remind twice.
|
|
|
|
sent = set()
|
|
|
|
|
|
|
|
def send_reminders():
|
|
|
|
global sent
|
|
|
|
|
|
|
|
messages = []
|
|
|
|
keys = set()
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
|
|
|
|
for uid, start, title in events:
|
|
|
|
dt = start - now
|
|
|
|
if dt.days == 0 and dt.seconds < 60*options.interval:
|
|
|
|
# The unique key includes the start time, because of
|
|
|
|
# repeating events.
|
|
|
|
key = (uid, start)
|
|
|
|
if key not in sent:
|
|
|
|
line = '%s starts at %s' % (title, start.strftime('%H:%M'))
|
|
|
|
print 'Sending reminder:', line
|
|
|
|
messages.append(line)
|
|
|
|
keys.add(key)
|
|
|
|
|
|
|
|
if not messages:
|
|
|
|
return
|
|
|
|
|
|
|
|
if len(messages) == 1:
|
|
|
|
message = 'Reminder: ' + messages[0]
|
|
|
|
else:
|
|
|
|
message = 'Reminder:\n\n' + '\n'.join('* ' + m for m in messages)
|
|
|
|
|
2012-12-03 22:13:00 +01:00
|
|
|
client.send_message(dict(
|
|
|
|
type = 'private',
|
|
|
|
to = options.user,
|
|
|
|
content = message))
|
2012-10-21 22:18:51 +02:00
|
|
|
|
|
|
|
sent |= keys
|
|
|
|
|
|
|
|
# Loop forever
|
|
|
|
for i in itertools.count():
|
|
|
|
try:
|
|
|
|
# We check reminders every minute, but only
|
|
|
|
# download the calendar every 10 minutes.
|
|
|
|
if not i % 10:
|
|
|
|
events = list(get_events())
|
|
|
|
send_reminders()
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
time.sleep(60)
|