-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttc.py
88 lines (70 loc) · 2.93 KB
/
ttc.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
# Load prerequisites
import requests
import os.path
# Timesaving
todoistURL = 'https://www.todoist.com/API'
usedLabelIDs = []
removeLabelIDs = []
confirm = []
# This function saves time printing JSON nicely
def pj(str):
print(json.dumps(json.loads(str.content), indent=4))
return
# Set login parameters (old, insecure) and obtain token
file = open('token.txt', 'r')
usertoken = file.read().strip()
token = {'token': usertoken}
# Get a list of projects and their details by API request and using token obtained above
print("Getting a list of projects...")
projectList = requests.post(todoistURL+"/getProjects", params=token).json()
# Put just the project IDs into projectIDs
projectIDs = [project['id'] for project in projectList]
# Get a list of all labels
print("Getting list of all labels...")
labelList = requests.post(todoistURL+"/getLabels", params=token).json()
# Put just the label IDs into labelIDs
labelIDs = [labelList[label]['id'] for label in labelList]
# Get a list of incomplete tasks and the labels in use on them
print("Getting a list of all incomplete tasks and any labels in use...")
for projectid in projectIDs:
token.update({'project_id' : projectid})
taskList = requests.post(todoistURL+"/getUncompletedItems", params=token).json()
del token['project_id']
# Check if any content in each labels field and where there is, for each item append to a separate list
for label in taskList:
if label['labels']:
newlabel = label['labels']
for id in newlabel:
usedLabelIDs.append(id)
# Remove the used labels from the overall list
removeLabelIDs = labelIDs
print("Checking for unused labels...")
for label in usedLabelIDs:
if label in removeLabelIDs:
removeLabelIDs.remove(label)
# TODO: Add a check to see if there actually is anything to remove; if not, exit.
# Print list of labels to remove as a check
#for x in labelList:
# if labelList[x]['id'] in removeLabelIDs:
# print(labelList[x]['name'])
#confirm = input("Press Y to remove the above: ")
#if "Y" in confirm:
print("Removing unused label(s)...")
# Remove the remainders from Todoist
for x in labelList:
if labelList[x]['id'] in removeLabelIDs:
token.update({'name' : labelList[x]['name']})
requests.post(todoistURL+"/deleteLabel", params=token)
'''
TODO
- input appeared to be broken when tested on Win 7
- Create new dicts for the tokens for each request instead of using del on lingering project_id
- Possible rewrite...
- to get the labels in use you need to get the unfinished tasks
- to get the unfinished tasks you need a project ID
- so I did have to get the project IDs
- however, not sure I needed to get the label IDs, just the label names from the incomplete tasks
- this is because actually, the /deleteLabel command requires the label names
- the lesson here is "read the syntax of the final, vital command first and work back"
'''