Skip to content

Instantly share code, notes, and snippets.

@wenkokke
Created February 19, 2017 00:34
Show Gist options
  • Save wenkokke/083dc74dce85ec20f42ffbb07349847a to your computer and use it in GitHub Desktop.
Save wenkokke/083dc74dce85ec20f42ffbb07349847a to your computer and use it in GitHub Desktop.
Convert CSV files exported from the SleepBot app to the CSV format used by toggl.
#! /usr/bin/env python3
from csv import reader, writer
from datetime import datetime, timedelta
from optparse import OptionParser
from sys import stderr
# parse command line options
parser = OptionParser()
parser.add_option('-i',dest='input_file',
help="file containing the SleepBot CSV data",
metavar='FILE')
parser.add_option('-o',dest='output_file',
help="file to which to write Toggl csv data",
metavar='FILE')
parser.add_option('--email',dest='email',
help="toggl email address",
metavar='EMAIL_ADDRESS')
parser.add_option('--project',dest='project',
help="toggl project",
metavar='STRING',
default='Sleep')
parser.add_option('--tags',dest='tags',
help="toggl tags",
metavar='STRING',
default='SleepBot')
(options,args) = parser.parse_args()
with open(options.output_file, 'w', newline='') as ofile:
togglwriter = writer(ofile)
togglwriter.writerow(
['Email', 'Project', 'Tags', 'Start date', 'Start time', 'Duration'])
with open(options.input_file, newline='') as ifile:
sleepbotreader = reader(ifile, quotechar="'")
sleepbotreader.__next__() # first line contains column names
for startdate, starttime, _, duration, _ in sleepbotreader:
try:
# parse SleepBot data
start = datetime.strptime('%s %s' % (startdate, starttime),
'%d-%m-%Y %H:%M')
hours, minutes = map(int,duration.split(' ')[0::2])
# format Toggl data
startdate = start.strftime('%Y-%m-%d')
starttime = start.strftime('%H:%M:%S')
duration = "%02d:%02d:00" % (hours, minutes)
togglwriter.writerow([
options.email, options.project, options.tags,
startdate, starttime, duration])
except ValueError as e:
stderr.write(str(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment