Challenge to create time axis list report with Toggl API and Python

Toggl API & Pyhon -> Monthly Timeline Report

Ki: Beginning

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.

Accepted: Used

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

Transfer: What I made

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


Conclusion: What was done

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.

Toggl-API_Monthly_Report.png

I: From now on

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

Challenge to create time axis list report with Toggl API and Python
[Python] Create a date and time list for a specified period
Create Awaitable with Python / C API
Create folders from '01' to '12' with python
Convert list to DataFrame with python
[Python] Create API to send Gmail
Operate Jupyter with REST API to extract and save Python code
Get conversions and revenue with Google Analytics API and report to Slack
I tried to create a list of prime numbers with python
Create REST API that returns the current time with Python3 + Falcon
Create Cognito user list in S3 with SQS Deploy queue function and API to Lambda with SAM
Automatically create Python API documentation with Sphinx
Fractal to make and play with Python
Create and decrypt Caesar cipher with python
[Python] Quickly create an API with Flask
How to get the date and time difference in seconds with python
Try to make foldl and foldr with Python: lambda. Also time measurement
[Outlook] I tried to automatically create a daily report email with Python
[Python] How to create a dictionary type list, add / change / delete elements, and extract with a for statement
[Python] List Comprehension Various ways to create a list
Scraping tabelog with python and outputting to CSV
MessagePack-Try to link Java and Python with RPC
[Python] Create a list of date and time (datetime type) for a certain period
Create an API to convert PDF files to TIF images with FastAPI and Docker
I tried to automate internal operations with Docker, Python and Twitter API + bonus
How to use Service Account OAuth and API with Google API Client for python
Steps to create a Twitter bot with python
How to measure execution time with Python Part 1
Get Gmail subject and body with Python and Gmail API
List of Python code to move and remember
[Python] Mention to multiple people with Slack API
Create an API server quickly with Python + Falcon
To represent date, time, time, and seconds in Python
How to measure execution time with Python Part 2
Convert strings to character-by-character list format with python
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
Reading, summarizing, visualizing, and exporting time series data to an Excel file with Python
[Python] I tried to automatically create a daily report of YWT with Outlook mail
Quickly create a Python data analysis dashboard with Streamlit and deploy it to AWS
Create a simple API just to input and output JSON files ~ Python / Flask edition ~
Memo to create your own Box with Pepper's Python
Procedure to load MNIST with python and output to png
[First API] Try to get Qiita articles with Python
I want to handle optimization with python and cplex
Try to operate DB with Python and visualize with d3
How to measure mp3 file playback time with python
To avoid spending time coloring shapes drawn with python
Crawling with Python and Twitter API 1-Simple search function
[Python] How to sort dict in list and instance in list
[Python] How to create Correlation Matrix and Heat Map
Try to automate pdf format report creation with Python
[Python] How to create a 2D histogram with Matplotlib
[Python] Get user information and article information with Qiita API
Something to enjoy with Prim Pro (X-Play) and Python
How to operate Discord API with Python (bot registration)
[LINE Messaging API] Create parrot return BOT with Python
OpenCV3 Python API list
Initialize list with python
I tried to automatically post to ChatWork at the time of deployment with fabric and ChatWork Api
Recursively get the Excel list in a specific folder with python and write it to Excel.
Parse the Researchmap API in Python and automatically create a Word file for the achievement list