Skip to content

Instantly share code, notes, and snippets.

@cb109
Created August 21, 2017 08:53
Show Gist options
  • Select an option

  • Save cb109/55c919a5f8b17a920d9048f2492ec25f to your computer and use it in GitHub Desktop.

Select an option

Save cb109/55c919a5f8b17a920d9048f2492ec25f to your computer and use it in GitHub Desktop.
Batch create Toggl time entries for vacation
# -*- coding: utf-8 -*-
"""
A simple script to batch-create Toggl time entries for a vacation.
Note: Time zones and offsets are not handled here.
# Prerequisites
You need to install requests:
$ pip install requests
# Usage
Fill in your Toggl API details and the timerange to track, then execute
this script like:
$ python toggl_vacation.py
"""
import json
from datetime import date, timedelta
import requests
# Adapt these values as needed.
API_TOKEN = "274648g7375983e48593ßd0a4f2719e1"
WORKSPACE_NAME = "John Doe's workspace"
PROJECT_NAME = "holiday"
VACATION_START_DATE = "07.08.2017"
VACATION_STOP_DATE = "18.08.2017"
TIME_ENTRY_DESCRIPTION = "summer vacation"
TIME_ENTRY_SECONDS = 60 * 60 * 8 # 8 hours
IGNORE_DAYS = ["Saturday", "Sunday"]
AUTH = (API_TOKEN, "api_token")
def _str_to_date(date_str):
"""Convert a 'DD.MM.YYYY' string to a date object."""
day, month, year = [int(val) for val in date_str.split(".")]
return date(year, month, day)
def get_dates(start, stop, ignore=IGNORE_DAYS):
"""Return a list of date objects based on start- and stopdate.
Args:
start (str): A date string in DD.MM.YYYY format.
stop (str): A date string in DD.MM.YYYY format.
ignore (list[str]): A list of day names to ignore.
Returns:
list[datetime.date]
"""
dates = []
startdate = _str_to_date(start)
stopdate = _str_to_date(stop)
delta = stopdate - startdate
for index in range(delta.days + 1):
date = startdate + timedelta(days=index)
dayname = date.strftime("%A")
if dayname in ignore:
continue
dates.append(date)
return dates
def get_workspaces(auth):
url = "https://www.toggl.com/api/v8/workspaces"
response = requests.get(url, auth=auth)
return response.json()
def get_workspace_by_name(name, auth):
workspaces = get_workspaces(auth)
for workspace in workspaces:
if workspace["name"] == name:
return workspace
return None
def get_workspace_projects(workspace_id, auth):
url = "https://www.toggl.com/api/v8/workspaces/{}/projects"
response = requests.get(url.format(workspace_id), auth=auth)
return response.json()
def get_project_by_name(workspace, project_name, auth):
projects = get_workspace_projects(workspace["id"], auth)
for project in projects:
if project["name"] == project_name:
return project
return None
def add_time_entry(project, startdate, duration, description, auth,
tags=None, created_with="script"):
url = "https://www.toggl.com/api/v8/time_entries"
payload = {
"time_entry": {
"description": description,
"duration": duration,
"start": startdate.strftime("%Y-%m-%d") + "T09:00:00+00:00",
"pid": project["id"],
"tags": [] if tags is None else tags,
"created_with": created_with,
}
}
payload = json.dumps(payload)
response = requests.post(url, data=payload, auth=auth)
print(response)
def main():
workspace = get_workspace_by_name(WORKSPACE_NAME, auth=AUTH)
project = get_project_by_name(workspace, PROJECT_NAME, auth=AUTH)
dates = get_dates(VACATION_START_DATE, VACATION_STOP_DATE)
for startdate in dates:
add_time_entry(project, startdate, TIME_ENTRY_SECONDS,
TIME_ENTRY_DESCRIPTION, AUTH)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment