Created
January 17, 2011 20:45
-
-
Save mrts/783448 to your computer and use it in GitHub Desktop.
Script that uses the Google Calendar API for automated time reports.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.pyc | |
*.swp | |
timetrack_conf.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
""" | |
Script that uses the Google Calendar API for automated time reports. | |
First, install the Google Data APIs Python Client Library with | |
pip install http://gdata-python-client.googlecode.com/files/gdata-2.0.13.tar.gz | |
Provide the following configuration in timetrack_conf.py: | |
* EMAIL: Google Calendar user email | |
* CALENDAR_ID: The Calendar ID is available on the calendar | |
settings page, on the Calendar Address row. | |
E.g. '[email protected]' | |
* PASSWORD: either '' for a prompt or the Google account password | |
* TIMEZONE: pytz-compatible timezone string, e.g. 'Europe/Tallinn' | |
""" | |
from __future__ import print_function | |
import datetime, warnings | |
from optparse import OptionParser | |
from getpass import getpass | |
import gdata.calendar.service | |
import pytz | |
try: | |
import timetrack_conf as conf | |
except ImportError: | |
import sys | |
print("ERROR: timetrack_conf.py is missing or invalid") | |
print(__doc__) | |
sys.exit(1) | |
TODAY = datetime.date.today() | |
TIMEZONE = pytz.timezone(conf.TIMEZONE) | |
def to_datetime(evt, attr): | |
datestring = getattr(evt.when[0], attr + '_time') | |
parts = datestring.split('.') | |
fmt = '%Y-%m-%dT%H:%M:%S' | |
if len(parts) == 1: | |
warnings.warn("Full-day event, will not affect total") | |
fmt = '%Y-%m-%d' | |
return datetime.datetime.strptime(parts[0], fmt) | |
def to_localtime(utc_datetime): | |
return TIMEZONE.fromutc(utc_datetime) | |
def parse_options(): | |
parser = OptionParser(usage=__doc__) | |
parser.add_option('-p', '--period', | |
help='Use events from the given period. ' | |
'Period is one of %s.' % ", ".join(PERIOD_HANDLERS.iterkeys())) | |
parser.add_option('-l', '--label', | |
help='Use events that start with the given label.') | |
return parser.parse_args()[0] | |
def thismonth(): | |
start = datetime.date(TODAY.year, TODAY.month, 1) | |
stop = TODAY | |
return str(start), str(stop) | |
def lastmonth(): | |
stop = datetime.date(TODAY.year, TODAY.month, 1) - datetime.timedelta(1) | |
start = datetime.date(stop.year, stop.month, 1) | |
return str(start), str(stop) | |
PERIOD_HANDLERS = { | |
'thismonth': thismonth, | |
'lastmonth': lastmonth, | |
# 'thisweek': thisweek, | |
# 'lastweek': lastweek, | |
} | |
class Event(object): | |
def __init__(self, remote_event): | |
self._event = remote_event | |
self.start = to_datetime(remote_event, 'start') | |
self.stop = to_datetime(remote_event, 'end') | |
def __str__(self): | |
return "%s | %s hours | %s -- %s" % (self._event.title.text, | |
self.duration.seconds / 3600.0, | |
to_localtime(self.start), to_localtime(self.stop)) | |
def __cmp__(self, other): | |
return (self.start - other.start).days | |
@property | |
def duration(self): | |
return self.stop - self.start | |
def main(): | |
options = parse_options() | |
calendar_service = gdata.calendar.service.CalendarService() | |
calendar_service.email = conf.EMAIL | |
calendar_service.password = (conf.PASSWORD if conf.PASSWORD | |
else getpass("%s password? " % conf.EMAIL)) | |
calendar_service.source = 'MrtsTimeTrack-v1.0' | |
calendar_service.ProgrammaticLogin() | |
query = gdata.calendar.service.CalendarEventQuery(conf.CALENDAR_ID, | |
'private', 'full') | |
period = (options.period if options.period else 'thismonth') | |
query.start_min, query.start_max = PERIOD_HANDLERS[period]() | |
events = calendar_service.CalendarQuery(query).entry | |
if options.label: | |
events = [Event(e) for e in events | |
if e.title.text.startswith(options.label)] | |
else: | |
events = [Event(e) for e in events] | |
events.sort() | |
time_total = datetime.timedelta(0) | |
for event in events: | |
print(event) | |
time_total += event.duration | |
print("------------------------------------") | |
print("Total time spent:", time_total) | |
print("Total in hours:", time_total.days * 24 + time_total.seconds / 3600.0) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment