Skip to content

Commit 7b552c2

Browse files
Fixes:
- Get completed tasks in Todoist to be completed in Habitica. Clean up. - Modify code to work better for testing. - Fix issue with date not syncing. Also partially fix #3 issue. - Improve error handling. Add sleep for rate limiting. - Clean-up code and warnings. - Add check for data dumped to pickle file.
1 parent 29ba29d commit 7b552c2

File tree

7 files changed

+307
-240
lines changed

7 files changed

+307
-240
lines changed
File renamed without changes.

source/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import configparser
44

5+
56
def getTodoistToken(configfile):
67
logging.debug('Loading todoist auth data from %s' % configfile)
78

source/habitica.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
11
'''Habitica related functions'''
2+
# TODO: convert to class that makes all
3+
# calls to Habitica
24
import requests
35
from hab_task import HabTask
46

57
def get_all_habtasks(auth):
6-
#Todoist tasks are, I think, classes. Let's make Habitica tasks classes, too.
8+
# Todoist tasks are, I think, classes. Let's make Habitica tasks classes, too.
79
url = 'https://habitica.com/api/v3/tasks/user/'
8-
response = requests.get(url,headers=auth)
9-
hab_raw = response.json()
10-
hab_tasklist = hab_raw['data'] #FINALLY getting something I can work with... this will be a list of dicts I want to turn into a list of objects with class hab_tasks. Hrm. Weeeelll, if I make a class elsewhere....
11-
12-
#keeping records of all our tasks
13-
hab_tasks = []
14-
15-
#No habits right now, I'm afraid, in hab_tasks--Todoist gets upset. So we're going to make a list of dailies and todos instead...
16-
for task in hab_tasklist:
10+
# TODO: handle error cases for response
11+
response = requests.get(url, headers=auth)
12+
if response.ok:
13+
hab_raw = response.json()
14+
# FINALLY getting something I can work with... this will be a list of
15+
# dicts I want to turn into a list of objects with class hab_tasks.
16+
# Hrm. Weeeelll, if I make a class elsewhere....
17+
hab_tasklist = hab_raw['data']
18+
else:
19+
hab_tasklist = []
20+
print(response.reason)
21+
22+
# keeping records of all our tasks
23+
hab_tasks = []
24+
25+
# No habits right now, I'm afraid, in hab_tasks-Todoist gets upset. So we're going to make a list of dailies and
26+
# todos instead...
27+
for task in hab_tasklist:
1728
item = HabTask(task)
1829
if item.category == 'reward':
1930
pass
20-
elif item.category == 'habit':
31+
elif item.category == 'habit':
2132
pass
2233
else:
2334
hab_tasks.append(item)
24-
return(hab_tasks, response)
35+
return hab_tasks

source/main.py

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
11
#!/usr/bin/env python
22

3-
"""
4-
Main.py overdue for an overhaul! Let's see.
5-
"""
6-
7-
"""Here's where we import stuff we need..."""
8-
#import todoist
9-
import requests
10-
import json
11-
from hab_task import HabTask
12-
from todo_task import TodTask
3+
# import json
4+
# from datetime import datetime
5+
# import todoist
6+
# import re
137
import os
148
import logging
159
import configparser
16-
17-
from datetime import datetime
10+
import sys
11+
import requests
1812
from dateutil import parser
19-
import re
20-
21-
22-
"""
23-
Version control, basic paths
24-
"""
13+
from hab_task import HabTask
14+
from todo_task import TodTask
2515

16+
# TODO: Main.py overdue for an overhaul! Let's see.
17+
# Version control, basic paths
2618
VERSION = 'Project_Hype-Berry version 2.1.0'
2719
TASK_VALUE_BASE = 0.9747 # http://habitica.wikia.com/wiki/Task_Value
2820
HABITICA_REQUEST_WAIT_TIME = 0.5 # time to pause between concurrent requests
@@ -128,8 +120,9 @@ def check_matchDict(matchDict):
128120
else:
129121
print("something is weird check tod %s" % t)
130122

131-
def check_newMatches(matchDict,tod_uniq,hab_uniq):
132-
#from main import add_hab_id
123+
124+
def check_newMatches(matchDict, tod_uniq, hab_uniq):
125+
# from main import add_hab_id
133126
matchesHab = []
134127
matchesTod = []
135128
for tod in tod_uniq:
@@ -174,6 +167,7 @@ def clean_matchDict(matchDict):
174167
tod = matchDict[tid]['tod']
175168
return matchDict
176169

170+
177171
def complete_hab(hab):
178172
import requests
179173
import json
@@ -197,16 +191,18 @@ def delete_hab(hab):
197191
return r
198192

199193
def get_all_habtasks(auth):
200-
#Todoist tasks are, I think, classes. Let's make Habitica tasks classes, too.
194+
# Todoist tasks are, I think, classes. Let's make Habitica tasks classes, too.
201195
url = 'https://habitica.com/api/v3/tasks/user/'
202-
response = requests.get(url,headers=auth)
196+
response = requests.get(url, headers=auth)
203197
hab_raw = response.json()
204-
hab_tasklist = hab_raw['data'] #FINALLY getting something I can work with... this will be a list of dicts I want to turn into a list of objects with class hab_tasks. Hrm. Weeeelll, if I make a class elsewhere....
198+
# FINALLY getting something I can work with... this will be a list of dicts I want to turn into a list of objects
199+
# with class hab_tasks. Hrm. Weeeelll, if I make a class elsewhere....
200+
hab_tasklist = hab_raw['data']
205201

206-
#keeping records of all our tasks
202+
# keeping records of all our tasks
207203
hab_tasks = []
208204

209-
#No habits right now, I'm afraid, in hab_tasks--Todoist gets upset. So we're going to make a list of dailies and todos instead...
205+
# No habits right now, I'm afraid, in hab_tasks--Todoist gets upset. So we're going to make a list of dailies and todos instead...
210206
for task in hab_tasklist:
211207
item = HabTask(task)
212208
if item.category == 'reward':
@@ -215,19 +211,25 @@ def get_all_habtasks(auth):
215211
pass
216212
else:
217213
hab_tasks.append(item)
218-
return(hab_tasks, response)
214+
return (hab_tasks, response)
215+
219216

220217
def get_hab_fromID(tid):
221218
import requests
222219
import json
223220
auth = get_started('auth.cfg')
224221
url = 'https://habitica.com/api/v3/tasks/'
225222
url += str(tid)
226-
r = requests.get(headers=auth, url=url)
227-
task = r.json()
228-
hab = HabTask(task['data'])
223+
response = requests.get(headers=auth, url=url)
224+
if response.ok:
225+
task = response.json()
226+
hab = HabTask(task['data'])
227+
else:
228+
# TODO: log error
229+
hab = HabTask()
229230
return hab
230231

232+
231233
def get_started(configfile):
232234
"""Get Habitica authentication data from the AUTH_CONF file."""
233235

@@ -263,13 +265,15 @@ def get_started(configfile):
263265
# Return auth data as a dictionnary
264266
return rv
265267

266-
def get_uniqs(matchDict,tod_tasks,hab_tasks):
268+
269+
def get_uniqs(matchDict, tod_tasks, hab_tasks):
270+
# TODO: Rename this function
267271
tod_uniq = []
268272
hab_uniq = []
269273

270274
for tod in tod_tasks:
271275
tid = tod.id
272-
if tod.complete:
276+
if tod.is_completed:
273277
if tid not in matchDict.keys():
274278
tod_uniq.append(tod)
275279

@@ -280,7 +284,8 @@ def get_uniqs(matchDict,tod_tasks,hab_tasks):
280284

281285
return tod_uniq, hab_uniq
282286

283-
def getNewTodoTasks(matchDict,tod_tasks,hab_tasks):
287+
288+
def getNewTodoTasks(matchDict, tod_tasks, hab_tasks):
284289
tod_uniq = []
285290
hab_uniq = []
286291

@@ -362,6 +367,8 @@ def make_daily_from_tod(tod):
362367
finished_hab = HabTask(new_hab)
363368
return finished_hab
364369
'''
370+
371+
365372
def make_hab_from_tod(tod_task):
366373
new_hab = {'type':'todo'}
367374
new_hab['text'] = tod_task.name
@@ -425,10 +432,11 @@ def make_tod_from_hab(hab):
425432
rList.append(r,hab.name)
426433
'''
427434

435+
428436
def openMatchDict():
429437
import pickle
430438
try:
431-
pkl_file = open('oneWay_matchDict.pkl','rb')
439+
pkl_file = open('oneWay_matchDict.pkl', 'rb')
432440
pkl_load = pickle.Unpickler(pkl_file)
433441
matchDict = pkl_load.load()
434442
pkl_file.close()
@@ -469,21 +477,24 @@ def purge_habs(hab_uniq, matchDict):
469477

470478
return hab_uniqest
471479

480+
472481
def sync_hab2todo(hab, tod):
473482
if hab.category == 'daily':
474-
new_hab = sync_hab2todo_daily(hab,tod)
483+
new_hab = sync_hab2todo_daily(hab, tod)
475484
return new_hab
476485
elif hab.category == 'todo':
477-
new_hab = sync_hab2todo_todo(hab,tod)
486+
new_hab = sync_hab2todo_todo(hab, tod)
478487
return new_hab
479488
else:
480489
print("Error! Hab of incorrect type!")
490+
sys.exit(1)
491+
481492

482493
def sync_hab2todo_daily(hab, tod):
483494
from dates import parse_date_utc
484-
from datetime import datetime
495+
# from datetime import datetime
485496
from datetime import timedelta
486-
import pytz
497+
# import pytz
487498
habDict = hab.task_dict
488499
if tod.priority == 4:
489500
habDict['priority'] = 2
@@ -492,7 +503,7 @@ def sync_hab2todo_daily(hab, tod):
492503
else:
493504
habDict['priority'] = 1
494505

495-
now = datetime.now().replace(tzinfo=pytz.utc).date()
506+
# now = datetime.now().replace(tzinfo=pytz.utc).date()
496507

497508
if hab.due.date() != (tod.due.date() - timedelta(days=1)):
498509
habDict['startDate'] = str(tod.due.date() - timedelta(days=1))
@@ -593,7 +604,9 @@ def syncHistories(matchDict):
593604
return matchDict
594605
'''
595606

607+
596608
def update_hab(hab):
609+
# TODO: Only update when there are actual changes
597610
import requests
598611
import json
599612
from datetime import datetime
@@ -612,6 +625,7 @@ def update_hab(hab):
612625
print(r.text)
613626
return r
614627

628+
615629
def update_hab_matchDict(hab_tasks, matchDict):
616630
from main import delete_hab
617631
from main import sync_hab2todo
@@ -678,6 +692,7 @@ def update_hab_matchDict(hab_tasks, matchDict):
678692

679693
return matchDict
680694

695+
681696
def update_tod_matchDict(tod_tasks, matchDict):
682697
tid_list = []
683698
for tod in tod_tasks:
@@ -690,6 +705,7 @@ def update_tod_matchDict(tod_tasks, matchDict):
690705

691706
return matchDict
692707

708+
693709
def write_hab_task(task):
694710
"""
695711
writes a task, if inserted, to Habitica API as a todo.

0 commit comments

Comments
 (0)