Toggl API & Pyhon -> Monthly Timeline Report
I use Toggl for self-analysis of business efficiency. Project is created appropriately based on Getting Things Done, and the client classification associated with it is Eisenhower Matrix. / eisenhower-matrix /) is classified in the 1st to 4th quadrants.
The report of the head family is also good, but I wanted to see the results by day and hour in a monthly list. And I've just started studying Python.
The environment is as follows.
Now, try to create a report with Toggl API and Python.
I searched on Google and mainly referred to this article and reference.
Reference: -Story of acquiring API of accumulated data of time management application Toggl and analyzing it with Python
It's hard work based on the reference code.
Toggl_API+Python.py
# -*- coding: utf-8 -*-
import requests
from requests.auth import HTTPBasicAuth
import json
import datetime
from datetime import datetime as dt
from dateutil.parser import parse
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
#Preparation of Dataframe
pd.set_option('display.max_colwidth', 20)
df = pd.DataFrame([[0], [0], [0], [0], [0], [0]]).T
df.columns = ["description", "category", "client", "date", "month", "duration"]
_api_token = '{Key}' # ここにAPIKeyを入れる
for pagenumber in range(1000):
pn = np.str(pagenumber + 1)
_params = {
'user_agent': '{mail address}', # ここにTogglに登録しているmail addressを入れる
'workspace_id': '{ID}', #Enter your workspace id here
'since': '2016-11-01', #First date
'until': '2016-11-30', #Last date
'page': pn}
r = requests.get('https://toggl.com/reports/api/v2/details', auth=HTTPBasicAuth(_api_token, 'api_token'), params=_params)
#Data acquisition
data = r.json()
Data = data['data']
if len(Data) == 0:
break
row = 0
for i in Data:
dataset = Data[row]
data01 = dataset['description']
data02 = dataset['project']
data03 = dataset['client']
start = dataset['start']
sd = start[0:10] # Start Date
st = start[11:19] # Start Time
s = sd + ' ' + st
tdatetime = dt.strptime(s, '%Y-%m-%d %H:%M:%S')
date_s = dt.strptime(s, '%Y-%m-%d %H:%M:%S')
end = dataset['end']
ed = end[0:10] # End Date
et = end[11:19] # End Time
e = ed + ' ' + et
date_e = dt.strptime(e, '%Y-%m-%d %H:%M:%S')
dur = date_e - date_s # Duration
data04 = sd
data05 = tdatetime.month
data06 = dur
#Quantify start and end times
data_s_h = datetime.datetime.strptime(str(date_s.hour), '%H')
data_s_m = datetime.datetime.strptime(str(date_s.minute), '%M')
data_e_h = datetime.datetime.strptime(str(date_e.hour), '%H')
data_e_m = datetime.datetime.strptime(str(date_e.minute), '%M')
#Convert to minutes
TimeVal_s = (data_s_h.hour * 60) + data_s_m.minute
TimeVal_e = (data_e_h.hour * 60) + data_e_m.minute
ClientV = dataset['client']
#Quantify the date
DayV = datetime.datetime.strptime(str(tdatetime.day), '%d')
y = DayV.day
#Separate colors with Client
for i in range(TimeVal_s, TimeVal_e):
if ClientV == 'Quadrant_4': #Quadrant 4(Not Importand & Not Urgent)Is blue
ColorV = 'b'
elif ClientV == 'Quadrant_3': #Third quadrant(Not Importand & Urgent)Is green
ColorV = 'g'
elif ClientV == 'Quadrant_2': #Second quadrant(Importand & Not Urgent)Is yellow
ColorV = 'y'
elif ClientV == 'Quadrant_1': #First quadrant(Importand & Urgent)Is red
ColorV = 'r'
else:
Client = 'k' #Black without setting
#drawing
plt.plot(i, y)
plt.scatter(i, y, c=ColorV, alpha = 0.2, marker = "s")
TMPtime1 = (str(tdatetime.hour) + ":" + str(tdatetime.minute) + ":" + str(tdatetime.second))
TMPtime2 = datetime.datetime.strptime(TMPtime1, '%H:%M:%S')
series_add = pd.Series([data01, data02, data03, data04, data05, data06], index=["description", "category", "client", "date", "month", "duration"], name=row + 1)
df = df.append(series_add)
row += 1
#Delete dummy line
df = df.drop(0)
plt.grid(True)
#x axis is 1 day(0~60*24)Label every hour
plt.xlim([0,1440])
plt.xticks([60,120,180,240,300,360,420,480,540,600,660,720,780,840,900,960,1020,1080,1140,1200,1260,1320,1380,1440], ["1:00","2:00","3:00","4:00","5:00","6:00","7:00","8:00","9:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00","24:00"],rotation =90,fontsize ="small")
#The y-axis is labeled daily for one month
plt.ylim([0,30])
plt.yticks([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31], ["31","30","29","28","27","26","25","24","23","22","21","20","19","18","17","16","15","14","13","12","11","10","9","8","7","6","5","4","3","2","1"], fontsize="small")
plt.style.use('ggplot')
plt.title("Monthly Timeline from Toggl")
plt.show()
#Output summary
monthly_sheet = df.pivot_table("duration", aggfunc="sum", fill_value=0, index="category", columns="date").T
monthly_sheet.to_csv("Summary.csv", sep=",")
print monthly_sheet
For the time being, I got the result I had imagined. From the color, you can see that there is no room. Or rather, I'm skipping the record from the middle.
For the time being, the groundwork for what I wanted to do was completed. However, the contents and appearance are still dirty. It is not beautiful because it has a lot of waste and inefficiency. It's taking too long to run. (Probably because the data is drawn one by one in a scatter plot) Will study.
If you know a cooler and smarter method, I would appreciate it if you could teach me.
Thank you for your cooperation.
[END]
Recommended Posts